Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
sanj duke
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,
Just a quick question.
Is it better to free old objects before allocating new ones? For example setting an object to null before re-allocating another object (with new).
Or is it better to specifically call System.gc().? Is it worth calling garabge collection because I have heard it doesn't take care of fragmentation.
Many Thanks

Sanj
 
Burk Hufnagel
Ranch Hand
Posts: 814
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For most versions of the JVM, calling System.gc() is considered a "hint" - it doesn't actually invoke the garbage collector, it just lets the VM know that you htink this would be a good time to run. It may or may not do anything basaed on your call.
As far as setting an object to null before allocating a new one, I believe that doing this:
obj1 = null;
obj1 = new MyObject();
is exactly the same as
obj1 = new MyObject1();
assuming that calling the constructor for MyObject doesn't throw an exception, that is.
I hope this helps,
Burk
Originally posted by sanj duke:
Hello Everyone,
Just a quick question.
Is it better to free old objects before allocating new ones? For example setting an object to null before re-allocating another object (with new).
Or is it better to specifically call System.gc().? Is it worth calling garabge collection because I have heard it doesn't take care of fragmentation.
Many Thanks

Sanj
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic