last day (15 days later) » 

5:28 PM
1
Q: Preventing my Memory Leak

MMikeI have an activity which starts an asynctask(called WorkerClass) and from the asynctask i add an BaseAdapter to my GridView: I start the AsyncTask from my Activity: if(gallery == null || switchScaleMode == true) { myWorkerClass = new WorkerClass(myActivity.this, width, scaleButtonTe...

 
Are you disposing your drawables on rotation? Also when you rotate do you cancle or stop your asynctask?
 
@Nazgul Yes after rotate i fit them to the "new" available screensize. I don't cancel the asynctask, but it only takes about 1 second to finish. So if a start rotating the instance of asynctask should be finished!
@Nazgul I mean i fit them, but i don't resize them! If in Portrait 4 Images fit in one row, after resize i can place 5 Images in one row.
 
Working with drawables is tricky. They are like view extensions and hold on to memory if not disposed. I can see that workerClass i probably an instance level property in activity. Try to reduce coupling between activity and asynctask and also dispose of existing drawables in activity/fragment when rotation happens. Also take a heap dump when that 97% is hit. See whats i it.
 
@Nazgul Sorry, i don't understand your second sentence. Can you make it clear to me?. Yeah i tried, butas i mentioned in my question i get an error trying to make a heap dump at 97% ;/. Ok i will try, so you mean i should clear my Gallery and instanciate it new after rotate?
 
Yes. Try taking dumps at regular intervals. And clear your gallery, dispose drawables and then recreate again on rotation.
 
5:28 PM
@Nazgul i got it run! Please look my Update and see what MAT Shows me.
 
Okie the problem is in Gridview and associated EditTexts in the grids. Are you recycling views there? Do you use a Viewholder in the grid view adapter? Check the dominator tree for both the leak suspects in MAT.
 
@Nazgul there is no edittext in my grid, the edittext is outside of the grid and provides searching trough the grid. Yes i'm reusing and i have a holder class! I'v updated my question with the DominatorTree. Please MemoryLeaks and MAT are really new to me so i can't bring any know how inside our conversation.. please guide me ..thanks in adavance. If you want we can move this conversation to Chat?
 
ping me back when u are in
 
hey nazgul i'm in and i got updates
@Nazgul i'm in
 
yeah
try to dig inside the dominator tree to see which edit textx is it talking about
in MAt when you click a node the properties view on left side shows its variables and what values they hold.
 
5:39 PM
one momen nazgul
i found something new
 
use that to get an idea of which edit texts are these
okie tell me new
 
let me give you are short overview to find the solution as fast as possible
 
k
 
i got 3 classes: my Activity, my WorkerClass(AsyncTask), my CustomAdapter.Class
 
can u send me the dump file...
i can check on my end as well
 
5:40 PM
everytime i rotate i start a new instance of my asynctask
what i mentioned here is..that it is possible that the old instance of the asynctask is stillt alive because the Adapter is alive and is referencing to the asynctask(because that's where i set the Adapter to the gridvie, in the asynctask)
So everytime i create a new instance of the asynctask, i also get all the appinfo Icons from the device
and thats why he crashes at this line with OOM: myDrawableArray[position] = appInfos_.get(i).loadIcon(pm);
in the AsyncTask
 
dont you add a new adapter when rotate happens?
 
no! because i want that the user stays at the Location where he was before rotating.( when he scolled down it should stay down even after rotatin)
 
this probably means that the activity is not letting your async task go
 
exactly!
 
hmmm...maybe that is the issue here...
 
5:43 PM
that's what i'm thinking to
ok, so how can i prevent this. Is the only way to set a new Adapter?
 
with gridview you can scroll down to a position programatically also.
so if u can track where the user was when rotate happened mabe you can scroll down to that view when rotate finishes..
can u send me the dump file...
let me check what it shows....
by the way how many edit textx do you have in your app?
 
yeah seems to be the Problem. you mean the *.hprof file?
only one!
 
yes the hprof file
 
ok one moment
hm i already cleared it and it took me hours to get that run because of my error which i told you before.. listen what about i'll try to set a new Adapter and look again if the Memory-issue dissapears... and we let this Chat open
and if this was the Problem. you can post it as the answere to my question and i mark as accepted. because you gave me a lot of time from you.. really nice thank you
 
okie try that and ing me back if it works...
tracking the scroll position i grid view woudl not be a problem if you can clear the leak first.
 
5:52 PM
ok i tried. this seems not the work on the fast way. but i got the hprof run. i will send you the file, how can i send it to you?
@Nazgul ping
 
just upload it here or mail me at vireshwali@gmail.com
ping me back when done
 
6:10 PM
@Nazgul sent you the file
 
in mail?
did setting a new adapter help?
 
no it doesn't help. i always did set a new Adapter was confusing to me aswell... so the Problem must be something different. that's why i sent you the file :) yeah in mail
 
havent got the file yet..
can u upload it here...use the upload button on the right of the message box.
 
don't have it, low Reputation but wait ill try with my other account to join with my other account
 
k
 
6:18 PM
i'm in
@Nazgul yeah the E-Mail is in my outbox bexause the attachment is too big.. 11MB
also upload doesnt work with such a file size
 
ha..
 
any other ideas how to give you the file
 
zip or rar it and the try
 
one moment
@Nazgul now u got it...
 
yeah
 
6:33 PM
nice.. These dirty email Providers..
 
giveme some time to check it out
will ping back
 
yes ok
 
do u have a searchbar in workerclass?
work with me here
open the dump in MAT
i will tel you what to chek and where
 
6:49 PM
yes
ok one moment
@Nazgul i will open it one moment
is it ok when i create a new one. The important things must stay the same right? because i can't convert them to Java hprof -.-
i can only open them directly couldn't figure out how to get that work
@Nazgul got it open
 
okie open the dominator tree...
convert the android hprof to java hprof..
use the hprof-conv command
its in your platform-tools directory in android sdk
 
yeah i tried but he said he can't find that file -.-
i know the command
now i just created a new one with similar Rotation count
 
make sure there are not spaces in your android hprof file path that you give to the command
best is to copy the file in D: or C: and the run command on that path
 
ok one momen
 
from the dump it seems that the arraylist in your edittext is keeping a lot of stuff
 
6:58 PM
ERROR: failed to open input 'com.adndroid.myApp.hprof': No such file or director
y
but i think ist no Problem to use a similar one which i created from the same code
the dump must be nearly the same
 
okie
do you have any listeners attached to edittext?
 
yes
an onTextChangedListener
 
from dump i can see u have only 1 edit text
 
thats right
 
seems liek you are not recycling the listener
hwo are you workign with the listener when rotation happens?
creating a new onw?
or reusing existing one?
 
7:00 PM
iyes, because i create a new asynctask and in the asynctask i'm attaching a new onTextChangeListener
 
okie that seems to be the problem....
you dump shows u have 9 listeners on edittext..
 
that's 8 too much
 
so presuming you attach a single listener each time...and u rotate it 8 times...u end up with 9.
 
but what i now understand
if i rotate
 
before attaching a listener remove all listeners first
try this...
 
7:01 PM
whole activity gets destroyed. why is the listener still alive?
 
no activity is not destroyed....its just destroyed from android's perspective.....not GC instantaniously...
so maybe that is the problem..
try to remove all listeners before attaching new ones...
your listeners seem to keep a hold of the edittext back..
its a two way association....edittext keeps a ref of listener and listenere keeps a ref of edittext to which it reports
try removing all listeners and then adding the new one....rerun your app and see what happens..
 
ok what moment
 
ur listener is also holding a reference to a dialog..
it exponentially eating memory..
check the addTextChangedListener method in edittext.....its actually in TextView...
its an array list....
the listeners u add keep on appending in the list...so if u dont remove then on rotation they wil keep adding up.
 
ok
it tried a fast Manipulation to fix the Problem. now i adding only one onTextChangedListener
but Memory is keeping increasing when rotating
i just prevented this now by an if Statement, i didn't remove the listener but i also not adding anymore
it also crashes at this line nazgul:
myDrawableArray[position] = appInfos_.get(i).loadIcon(pm);
everytime
not at onTextChangeListener
 
well crash may happen at the place which need smemory but may not be contributing to the leak..
ur bitmaps hodl just 11% of heap..
your listeners , textwatchers actually hold 55% of heap...
so i thin the problem is your text watchers..
try a run with no text listeners...
 
7:14 PM
good idea
one moment
 
comment out that call too add a text watcher and hardcode some text to search for...
and rotate liek crazy
 
omg
you are right
after rotate it increases from 13,xxx kb to 16,xxx kb after garabce collect it falls back to 13,xx KB....
how did you detect that i got 9 instances of textwatcher
@Nazgul ping
 
it was there in the dump....
s now its workign fine?
so*
I guess you have your culprit there.....just remember that waters u add as listeners are added in an arraylist in the edittext.....so if you keep on adding all the time they will explode out. remove all watchers on orientation change and they add afresh again and you shoudl be good.
figure out a way to remove and add...
and give it a go again
 
7:30 PM
yes
now without the onTextChangeListener it works so i Need to Change my code
yes but how did you exlplored the fault in the dump file?
explored*
@Nazgul
 
in the dominator tree you can see the edittext o the top if you sort the tree on retained space column.
int he leak suspects view which u pasted in question...in you click on details link it will show u a new view where u cans ee the listeners..
correlating that with the dominator tree i saw u had 9 different listeners in the dump u gave me...
all of the same type...so i thought maybe you have only 1 and kept adding new ones on rotation...
 
ok i think i got it
 
55% of heap is edittext which actually is onely 1 obejct in your dump...so next logical answer is the arraylist of listeners..
rest of the story you know.
 
right
thank you nazgul
write this to my question and i mark as accepted :)
 
am glad I coudl help you....
 
7:35 PM
you really helped me appreciate that
 
thanks
done
 

last day (15 days later) »