Android garbage collector, freeing objects inside objects?

If there is any object, that still has a path to root, containing a reference to another object, the referenced object will not be garbage collected. So if you create object A, that has a sub object A1. Then create object B and through what ever means pass a reference to A1 to object B.

Even when object A is GC'd A1 will not be because object B still holds a reference. (In your example, A & B will be gc'd... C will not).

If there is any object, that still has a path to root, containing a reference to another object, the referenced object will not be garbage collected. So if you create object A, that has a sub object A1. Then create object B and through what ever means pass a reference to A1 to object B.

Even when object A is GC'd A1 will not be because object B still holds a reference. (In your example, A & B will be gc'd... C will not) You can experiment with the mechanics behind garbage collection by telling the system explicitly to perform a collection at certain times with a System.gc(); call. Also you can override the finalize in object to see exactly when the given object is collected.

: @Override protected void finalize() throws Throwable { try { Log. D("Learning about GC", "My Object has been GC'd. " + this); } finally { super.finalize(); } } You can learn a lot through experimenting with objects, references, finalize and explicit calls for gc().

I would remove the overridden finalize call when you done testing.

Although Pixie's answer its correct too, I'm tending to choose Gregg's for such a great advice, really worth it. I just discovered how to correctly dispose of my objects to increase performance, thanks a lot for this! – ruhalde Aug 18 '11 at 17:54 @ruhalde: I hope you're not going to use finalization to increase performance because it can only decrease performance.

Here's a good article about GC: java.sun.com/docs/books/performance/1st_edition/html/…. – Pixie Aug 19 '11 at 5:02 I never suggested using finalize for anything other than a learning tool. It most definitely is not the place to perform any clean up or required functionality, but can be a great way to get some hands on learning about how GC works.As I mentioned in my original post, I would also recommend removing the finalize call when you are done experimenting/testing.

– Gregg Rivinius Aug 19 '11 at 13:34 I'm not forcing object destruction, I've just put a Log. D message there and tuned my signaling to GC, that is, I mark an object for destruction with an object=null assignment in some strategic places of my code, really works. – ruhalde Aug 19 '11 at 16:28.

The object of class A will be garbage collected during the next GC-cycle. List B will be GC'ed too. And List C will not be GC'ed because it can be reached from GC roots.

By default java creates an object by reference instead of by value. So, in this case if the garbage collector would pick up and empty all A B C and the then the other class that referenced C would be empty, since it didn't create a duplication of the class, it just referenced it.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions