views:

55

answers:

2

My question are commented in the code

ImageView img = new ImageView();
this.layout.addView(img);
MyObject o = new Object(img);
// Do i need to set img to null?

ArrayList <MyObject> myArray = new ArrayList <MyObject>();
MyObject obj = new MyObject();
myArray.add(obj);
// Do i need to set obj to null?
+1  A: 

Why would you need to set it to null?

If you're worried about garbage collection, the reference ceases to exist as soon as the local variable is reassigned or goes out of scope. (It's not quite that simple, but that's the general idea.)

fadden
I just switched from objective c (iphone dev)In objective c in both cases if i don't release the objects i mentioned (or set them to null) I would get a memory leak.I just wanted to make sure, that doesn't apply to java.1 more thing, is it a good idea to call the garbage collector every 10 seconds?Thanks
aryaxt
No, you do not ever need to call the garbage collector. It works all on its own in the background.
Mayra
Couple of links you might find useful, http://www.ibm.com/developerworks/java/library/j-jtp01274.html has good explanation on why setting to null might be bad. http://chaoticjava.com/posts/how-does-garbage-collection-work/ Is a good, details post on how garbage collection works, if you are interested.
Mayra
A: 

You can find a lot of useful tips here (Designing for Performance.) regarding memory, performance in Android. And for sure, you don't need to set variables to null in your code

Andrey Chorniy