Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 A garbage collection question.
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
facultyofmusic




PostPosted: Thu Dec 17, 2009 5:37 pm   Post subject: A garbage collection question.

So the java garbage collection is supposed to throw out no-reference objects that's in RAM. For example: if I say:

Java:
JFrame frame = new JFrame();


and then i say

Java:
frame = null;


the JFrame object will now be thrown out by the garbage collector right?

1. When does the garbage collector does it's work? (because calling it doesn't even mean it's gonna do it).

2. If I put something inside the finalize method (say System.out.println("I'm being destroyed")), will that work? Will I get a message saying "I'm being destroyed" after I give that object's reference a null pointer?

3. I've always been wondering, say you make a JFrame by doing:

Java:
new JFrame().setVisible();


technically that JFrame has nothing referencing to it, but it's being displayed onto the screen. Does the garbage collector collect that? If not, how does it know? Isn't it supposed to collect everything that has no-reference to it?

*P.S. how can i choose which button to work if they overlap?
*PPS how can i mark a question as solved? (i'm a noob to this great site...) Confused Crying or Very sad
Sponsor
Sponsor
Sponsor
sponsor
jbking




PostPosted: Thu Dec 17, 2009 5:57 pm   Post subject: Re: A garbage collection question.

Java HotSpot Garbage Collection may have some of the answers you are wanting, but to answer your specific questions:

Not necessarily as something else may have accessed that JFrame object so there is still a reference to it possibly. For example, if there was some collection of JFrames and that was added to it.

1. There may be different implementations of a garbage collector so this isn't easy to give a complete answer on I think.

2. You may get the message when the GC is run and there may be ways to explicitly invoke it.

3. I think there are some details about GC you may want to read up on to learn more about what is going on in some cases here.
Tony




PostPosted: Thu Dec 17, 2009 6:18 pm   Post subject: RE:A garbage collection question.

garbage collector frees and returns memory back to the heap, not "RAM". http://en.wikipedia.org/wiki/Java_Virtual_Machine_heap

Quote:

When does the garbage collector does it's work?

Whenever it feels like doing so. Perhaps never.

Quote:

Will I get a message saying "I'm being destroyed" after I give that object's reference a null pointer?

You will get a message when the garbage collector cleans up that space. Setting a pointer to null will not immediately trigger anything. It might never run; see point above.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
andrew.




PostPosted: Sat Dec 19, 2009 12:37 am   Post subject: RE:A garbage collection question.

So is there a way to clean garbage manually or invoke the garbage collector? Or do we just have to hope Java cleans itself?
[Gandalf]




PostPosted: Sat Dec 19, 2009 11:32 am   Post subject: RE:A garbage collection question.

Calling System.gc() gives the JVM a strong suggestion that it garbage collect now, however the JVM will always be in control of exactly what it garbage collects and when. The whole point (or a good chunk of the point Wink) of Java is that it garbage collects on its own... this is why there are different programming languages.
andrew.




PostPosted: Sat Dec 19, 2009 4:31 pm   Post subject: RE:A garbage collection question.

Okay, that sounds reasonable. I guess I can use System.gc() whenever I feel garbage should be collected and just have faith in the JVM that it will do it properly.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: