• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

garbage collector

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Michael Keisu
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic