Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage Collection - Good or Bad?  RSS feed

 
Vivek Jain
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is garbage collection good or bad?
If good, what should we do to invoke garbage collector?
If bad, what care should be taken not to have it?

Thanks!
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the most part, do not worry excessively about it at the beginner level.

You may call the garbage collector whenever you think you need it at the beginner level.
 
Henry Wong
author
Sheriff
Posts: 22846
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO, the main benefit of a garbage collector is to remove memory management duties from developers. A duty that is prone to mistakes. This allows for more stable code, and faster development -- in most cases.

Having said that, developers should not "invoke garbage collector" or take any "care" to "not to have it". Good, bad, or indifferent, just let the GC do its job.

Henry
[ November 18, 2007: Message edited by: Henry Wong ]
 
Bert Bates
author
Sheriff
Posts: 8945
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recently, I was talking to an engineer as Sun about the GC. He said that in hos opinion there are about 10 people in the world qualified to override the GC, and that everyone else should just let it do it's job. That might be a little bit of an overstatement, but in general I agree with him.

However, it's really important to understand when objects become eligible for the GC. So, the most important thingyou can do as a developer to help yourself when it comes to memory management, is to be careful about freeing up references to objects so that the GC can, in fact, do its job
 
fred rosenberger
lowercase baba
Bartender
Posts: 12441
42
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's probably too late, but...

This sounds like a homework question to me. So, what do YOU think are the pros and cons?
 
Vivek Jain
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wells, guys, earlier I was thinking that closing the object connection, specifying null values after use, object pooling, using finalize method, etc will be useful in garbage collection.....but i read somewhere that its really not going to help in garbage collection....is it correct? Can anyone elaborate in details? Also, if possible, whot are the pros n cons of GC?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Closing resources like files and database connections is important. That often release resources that are outside the JVM and GC, like operating system resources or memory allocated in native code. finalize() is so unreliable and slightly weird as to pretty much be ignorable.

Don't think about setting variables to null just to encourage garbage collection. Learn to think about the life span an object needs and manage its scope appropriately. Local variables go away at the exit of a code block like a curly braces after an "if" or a loop or a method. If you find a block that you think is so long you'd like to null something out early, it's probably a hint to break up the block instead.

My most biggest worry is putting things in collections or a servlet session and forgetting about them. Again, it's careful scope and lifespan management, not thinking about GC.

Hope that helps!
[ November 19, 2007: Message edited by: Stan James ]
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Bert:]   Recently, I was talking to an engineer as Sun about the GC. He said that in hos opinion there are about 10 people in the world qualified to override the GC, and that everyone else should just let it do it's job. That might be a little bit of an overstatement, but in general I agree with him.

Well I was reading the JSR for real-time and reasonably early in the writing it is stated that there is a great deal of isolation between the theorists in R.T. vis-a-vis the practitioners, and that the gc is one area where attainability of Static Priority Bounds brings this contention to a investigatable design constraint. If we expose the gc calls to Java Beginners, and the calls are a reasonable area of concern for World-Class equipment makers, it may be that calls to gc.run() should be a null-stubb except for those ten people.

I therefore register an interest in being informed if those 10 people are theorists and not field engineers. There are known issues in   Industrial Engineering and Management   for which this knowledge is relevant. I have a Team Member degreed in the matter, for which I stubb my request here for later reference.
 
Henry Wong
author
Sheriff
Posts: 22846
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I was reading the JSR for real-time and reasonably early in the writing it is stated that there is a great deal of isolation between the theorists in R.T. vis-a-vis the practitioners, and that the gc is one area where attainability of Static Priority Bounds brings this contention to a investigatable design constraint.


Real Time Java is actually not a good basis for this debate. JSR 1 includes new types of memory models (immortal and scoped) whose purpose is to avoid GC altogether.

With scoped memory, you can write your program in such a way that the GC never has to run. In effect, you are writing your program with explicit directives that tells the JVM when the memory goes out of scope. The garbage collector knows what to collect, and when, without having to copy or mark/sweep.

IOWs, RT java is great for programmers who don't want to use the GC. The debate isn't whether GC is good or bad -- but more like the assumption is that it is bad, and here are the tools to avoid it.

IMHO, it is too early to tell if this is a good or bad thing. As I don't have a feel for the API, nor seen what the best practices to come from it yet.

Henry
[ November 25, 2007: Message edited by: Henry Wong ]
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Henry:]IOWs, RT java is great for programmers who don't want to use the GC. The debate isn't whether GC is good or bad -- but more like the assumption is that it is bad, and here are the tools to avoid it.

Well if it is bad in the opinion of people who can carry on a discussion like this, then it should not be in scope for beginners and the answer to the posters original question would be to not make any calls to gc and just let the JVM do it's work.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!