• 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

Suggestion for increasing the chance of Garbage Collection

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To increase the chance of GC, I usually use the following approach to call GC to clean up the memory:

Test testObj = new testObj; // Suppose it is a very large object

/* after some complex processing */
testObj = null; // make the Object (memory location) unable to be referenced and then become a candidate for GC
System.gc(); // Call GC to clean up the memory


Is this approach always reclaim the memory used by testObj as I expected?

Any other good practices for GC in Java Coding (not JVM tunning)?
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember, Test is a reference to the object. It is not the object itself. The object will be GCd some time after the time when there are no longer any references to it. Setting Test to null removes that one reference. Only you can know whether there are other references.

Calling System.gc() is almost certainly pointless. As a beginner, you should NEVER call it. No, really - just forget about it.

Java will GC when necessary and often won't do so when you call System.gc(); it is only a suggestion to the GC that now might be a good time to GC.
[ August 14, 2007: Message edited by: Peter Chase ]
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Really good article regarding GC and fine tuning JVM Options
http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html

Similar artices for older JVM versions:
http://java.sun.com/docs/hotspot/gc1.4.2/

Just switch the numbers gc1.x.x according to your version
 
Peter Chase
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those articles are fine. However, I think that the type of question that the original poster is asking suggests he is not ready for them yet.

The first thing is to ensure that the concepts of object references in Java, and how they work with GC, are fully understood.

The second thing is to understand that GC works very well for most applications, without any special measures. GC tuning is only for big, or very performance-critical applications, not for those new to Java. And calling System.gc() is usually pointless, and never something those new to Java should do.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A critical bit of the basic understanding of object references is their scope or life span. The temptation to set a variable to null to indicate we're done with it tells us something about the code. If part 1 of a method uses some object and then sets it to null when it's done, and part 2 of the method doesn't use that object any more, then the method is probably doing two distinct things that could be broken out.

Now any local variables in partA will go out of scope when it returns. The object reference won't be visible in partB making it easier to read and reducing the chances of a silly mistake from trying to use it again.

Making something eligible for GC doesn't generally influence my thinking here. But reducing the scope to expose a variable only to code that really needs it is often on my mind. Earlier GC eligibility may be a happy side effect.

BTW: I showed separate methods as a way to control variable scope. As you read up on variable scope, find others.
 
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic