This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Suggestion for increasing the chance of Garbage Collection  RSS feed

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • 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
  • Mark post as helpful
  • send pies
  • 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: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!