1

I have a few EditTexts that do nothing. I skipped all listeners and filters to see the clear picture. EditText has the next structure:

EditText et1 = new EditText(this);
et1.setId(num+1); 
RelativeLayout.LayoutParams etp =  new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
etp.addRule(RelativeLayout.LEFT_OF,et2.getId());
etp.addRule(RelativeLayout.BELOW,et3.getId());
etp.setMargins(0, 0, 5, 5);
et1.setLayoutParams(etp);
et1.setBackgroundResource(R.drawable.someXMLDrawable);

RelativeLayout content = new RelativeLayout(this);
content.addView(et1);

When I'm clicking on EditText it runs garbage collector. If will click from one field to another I will get the next picture:

D/dalvikvm(387): GC_CONCURRENT freed 1513K, 43% free 10451K/18055K, paused 1ms+3ms
I/dalvikvm-heap(387): Grow heap (frag case) to 13.731MB for 1190416-byte allocation
D/dalvikvm(387): GC_CONCURRENT freed 1525K, 43% free 10451K/18055K, paused 2ms+2ms
I/dalvikvm-heap(387): Grow heap (frag case) to 13.729MB for 1190416-byte allocation
D/dalvikvm(387): GC_CONCURRENT freed 1438K, 42% free 10540K/18055K, paused 2ms+2ms
I/dalvikvm-heap(387): Grow heap (frag case) to 13.723MB for 1190416-byte allocation
D/dalvikvm(387): GC_CONCURRENT freed 1436K, 42% free 10540K/18055K, paused 2ms+3ms
I/dalvikvm-heap(387): Grow heap (frag case) to 13.720MB for 1190416-byte allocation
D/dalvikvm(387): GC_CONCURRENT freed 1432K, 42% free 10540K/18055K, paused 1ms+3ms
I/dalvikvm-heap(387): Grow heap (frag case) to 13.721MB for 1190416-byte allocation
D/dalvikvm(387): GC_CONCURRENT freed 1435K, 42% free 10540K/18055K, paused 2ms+3ms
D/dalvikvm(278): GC_CONCURRENT freed 1839K, 63% free 12684K/33863K, paused 7ms+3ms
D/dalvikvm(190): GC_CONCURRENT freed 1652K, 74% free 14590K/54343K, paused 5ms+10ms
D/dalvikvm(190): GC_CONCURRENT freed 234K, 71% free 16243K/54343K, paused 9ms+19ms
I/dalvikvm-heap(190): Grow heap (frag case) to 29.055MB for 6553616-byte allocation
D/dalvikvm(190): GC_CONCURRENT freed 22K, 55% free 24826K/54343K, paused 9ms+20ms

Is it a normal situation? Can I fix it somehow?

EDIT: I have a ViewPager. It contains Fragments. Fragments consists of empty RelativeLayout. When app starts it receives JSNO strings based on which buttons and edittexts are dynamically generated and added to RelativeLayout. As EditText or Button need a context it use "getActivity()" that returns the associated activity context. Sometimes I need to remove all Views from RelativeLayout and create new Views and as removed Views have reference to Activity it can be not cleaned by GC,I don't know exactly, it is another question (if someone know the answer, will be nice to hear).

So controls dynamically generated from JSON string and we do not know the exact amount of them. That what I want to do.

And the question is - Why GC starts every time when I click on EditText.

inc
  • 205
  • 4
  • 15
  • Seems like its a lot of allocation/deallocation due to the drawable you load. Maybe try to use a cache of your drawable? – mach Aug 12 '13 at 06:58
  • 1
    It could be because of the edittext alot of people have issues with edittext holding a activity context. I am facing the same issue cant find a solution, although it seems to be only with certain devices http://stackoverflow.com/questions/14069501/edittext-causing-memory-leak – MobDev Aug 21 '13 at 01:07
  • I'm holding an application context instead of activity context. But anyway it can be [ICS bug](http://stackoverflow.com/a/8498546/2146071). I don't have "Jelly Bean" version. So I can't to test if it is the same issue there. – inc Aug 21 '13 at 09:33

2 Answers2

0

This is the normal behavior of android garbage collector. It will collect and remove the resources form the memory as and when it will be no longer required in memory by itself to clearup the memory for loading the more other resources. You can not fix it. But you can increase of decrease the size the your heap area from the settings.

Growing heap on emulator indicates that at some point you have memory leak. Most of device can handle such leaks with no problem.

Another reason for heap to grow up: inefficient memory operations. That means at some time you are asking to much memmory ( e.g you selected 5M image from gallery, created inpuststream for it and keep in memory as bitmap, so you asking 15+M emulator will show just hight heap grow, but most of devices will show error).

If you see heap grows - analyse your memory usage and detect leaks.

If you don't detect anything strange you can almost safely ignore heap warning.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
0

you can use eclipse memory analyzer(MAT) in order to know what is really consuming your memory.

MAT tutorials:

1- Vegella

2- YouTube

MRefaat
  • 515
  • 2
  • 8
  • 22