• 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 collection

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have seen some examples where people set the local objects to null at the end of the method. But Is this neccessary, as we all know that the JVM will GC all unreferenced objects?

I have been asking this to myself many times and now it is time to make it clear.
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is some depth to this topic, so a simple "yes" or "no" answer would be misleading.

If we are talking about running in a single JVM (client application), then explicitely setting the object references to null would indeed be clutter. However, there are two exceptions:

1. If your code actively manages memory (as opposed to relying on the GC), you must make sure that you unreference the allocated space. Otherwise, a memory leak would be introduced. This comes in play if you do something with the System.arraycopy() method, for example. For an excellent treatment of this issue, see Effective Java.

2. Sometimes you may feel like writing expressive code, telling something to the fellow programmers. For example, by setting a reference to null in the middle of a long method, you are saying "I am really done with this object". This would be rare, though.

Now, let's move to the distributed system where we have two or more JVMs running, one (or more) on the client side(s), and one on the server side.

The RMI spec tells us that unreferenced() is called on the exported server object when no client (in the client JVM) holds any reference to this object (see Garbage Collection of Remote Objects for the precise specification). Note, however, that it's only one of the two conditions that must be met before the server object can be GCed. The other condition is that no server side object should hold any reference to the exported object in question. To make the object eligble for GC, you must set all the server side references to it to null, unless they are already out of scope. This is probably what you are addressing in your question.

If you are experimenting with the DGC (Distributed Garbage Collector), you may want to set the sun.rmi.dgc.checkInterval and the java.rmi.dgc.leaseValue properties to some small values, so that the DGC kicks in faster and more frequently.

Finally, if your object is bound to the RMI registry, it's always reachable and therefore can never be garbage collected (unless you unbind it from the naming space, of course).
[ June 13, 2005: Message edited by: John Smith ]
 
Muthaiah Ramanathan
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow. Thanks for your Answer - I really appreciate that.
 
Every time you till, you lose 30% of your organic matter. But this tiny ad is durable:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic