aspose file tools*
The moose likes Beginning Java and the fly likes Garbage collection.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Garbage collection...." Watch "Garbage collection...." New topic
Author

Garbage collection....

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Hi friends
As we know java has automatic memory management by the process name called garbage collector. the JVM will call this GC for releasing the memory of the objects thats has no more references. as we know objects are eligable for garbage collection by Nulling a Reference,Reassigning a Reference Variable and Isolating a Reference. So in my example

in this code the reference variable myRef is pointing to myclassObject as the main method runs out of its scope does the JVM make myRef eligable for GC.
suppose i mage a change

when could my reference variable myRef is eligable for GC ?


Creativity is nothing but Breaking Rules
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Santhosh, let me modify your code a bit



Now when line #10 is executed, then this is the object representation in the heap



So now in this object of myclass, the ref points to itself. So basically myRef in main method and myRef.ref point to the same object. Now when we set myRef to null, it no longer points to this object. And myRef.ref also cannot be accessed as myRef now points to null. So basically you cannot access this object.



Thus this object will be eligible for GC...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

This is called an island of isolation.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Does the object leave in heap even after the code run's by the JVM.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38392
    
  23
santhosh.R gowda wrote:Does the object leave in heap even after the code run's by the JVM.
Ankit has already given you the details. The correct answer is "don't know" because the garbage collector probably doesn't run during the execution of such a small application.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collection....