File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes garbage collector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "garbage collector" Watch "garbage collector" New topic
Author

garbage collector

Michael Keisu
Ranch Hand

Joined: Oct 08, 2009
Posts: 30
do you have to set an object reference to null for the garbage collector to come and collect it(the object)? If not, would it help the garbage collector to do so?
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
there is the difference between java and other languages. You don't have to do memory management. JVM does that optimization and you have no control on it (garbage collector).
Aneesh Vijendran
Ranch Hand

Joined: Jun 29, 2008
Posts: 125
Well the short answer is "Not really". Even if you do so, there is not harm, but no magic is gonna happen.

The garbage collector thread, when it runs, it's intelligent enough to understand which Objects doesn't have any references anymore, and it garbage collects it. The present day jvm's are well writen to handle this.

Cheers
Aneesh

Cheers
Aneesh
Michael Keisu
Ranch Hand

Joined: Oct 08, 2009
Posts: 30
Ah, so there is nothing you can do as a programmer to speed it up, it just collects garbage when it decides to?

thanks for the help
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Michael:

There's really no reason to speed it up, since Java is supposed to take care of the messy stuff behind the scenes. However, you can use a tool like JConsole to manually force garbage collection.

John.
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

Consider the following stack implementation



There is nothing wrong with this program. It would pass every test but there is one problem. The stack maintains obsolete references to objects that were popped off the stack and will not be garbage collected. An obsolete reference is simply a reference that will never be dereferenced again. In extreme case, the program may fail with OutOfMemoryError.

The corrected version of the pop method looks like this:


Refer to Effective Java for more information.


http://muhammadkhojaye.blogspot.com/
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Michael Keisu wrote:it just collects garbage when it decides to?


That's right. When an object can no longer be reached within your program the GC will remove the object and free the memory it occupied. You don't control this process but the GC guarantees that before it throws an out-of-memory exception it will first make sure all objects you no long use are removed from memory.

There are two special situations you must pay attention to though. The first concerns objects which allocate OS resources. Such objects usually have a special "dispose" method you must call before you let go of them. If you don't the GC won't be able to remove such objects. You'll have to pay attention to the class documentation here.

The second concern is so called "involuntary object retention". This is when you keep a reference to an object but you really didn't mean to. An example (concerning a stack implementation) has been given in this thread.

Apart from this you just create objects when you need them and then stop using them when you're done with them.

http://www.ibm.com/developerworks/library/j-jtp01274.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbage collector