4

Someone suggested to me recently that to avoid hogging the memory, a Java application should always destroy all objects created in the method at the end of this method whenever it's possible (by setting it to null).

I haven't seen that done too often before, and from my point of you it seems to defeat the purpose of garbage collector in the first place. Are there any guidelines / reasoning when this strategy is useful? Would you ever face an application when this is necessary if you are careful about your objects?

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Nikita
  • 769
  • 2
  • 8
  • 18
  • possible duplicate of [Does assigning objects to null in Java impact garbage collection?](http://stackoverflow.com/questions/449409/does-assigning-objects-to-null-in-java-impact-garbage-collection) – Buhake Sindi Jun 29 '11 at 13:28
  • 4
    Those who told you this were C++ developers.... :) – Buhake Sindi Jun 29 '11 at 13:29
  • 1
    Everyone who describes setting variables to null as destroying objects should be prevented from giving future advice on garbage-collected languages. It staggers me how incredibly wrong this is. –  Jun 29 '11 at 13:34

3 Answers3

7

Setting references to null doesn't destroy the objects. It just eliminates one reference to them. In order for the memory to be freed, the garbage collector is still required. For locally scoped variables, this makes no difference at all.

recursive
  • 83,943
  • 34
  • 151
  • 241
  • Let me rephrase - I realize that nulling alone doesn't cut it but what if the variables were nulled *and* `Runtime.gc()` was called at the end of the method? – Nikita Jun 29 '11 at 13:26
  • 3
    Calling `Runtime.gc()` is usually pointless. The GC will start when the VM needs memory. Calling it at the end of each method will just waste time. – Mathias Schwarz Jun 29 '11 at 13:27
  • @Nikita, Runtime.gc() is just a hint to the JVM that gc'ing would be a good idea. The garbage collection is deliberately so vaguely described that the JVM implementors have almost free hands without being tied to some ancient behaviour. – Thorbjørn Ravn Andersen Jun 29 '11 at 13:33
  • 2
    @Mathias - Calling `Runtime.gc()` is usually pointless *and expensive*. – Stephen C Jun 29 '11 at 14:00
  • @Stephon C: The JVM is allowed to do nothing when this method is called (it is only considered a hint) and most likely it will. – Mathias Schwarz Jun 29 '11 at 14:04
  • @Mathias - but *by default* it runs the GC. – Stephen C Jun 29 '11 at 15:20
3

Many years ago this was recommended by some as a way to help the Garbage Collector identify which objects should be garbage collected. Today's GC is very sophisticated and generally needs no help from the developer. In fact, you're more likely to introduce bugs this way (e.g. accidentally nulling a variable prematurely).

This question was asked many times before. Here's one other thread: Does assigning objects to null in Java impact garbage collection?

Community
  • 1
  • 1
Steve Brisk
  • 2,503
  • 2
  • 16
  • 13
  • The garbage collector works by marking all **live** (those that should not be GC'ed) objects by following pointers from the stack. Assigning null will not help the GC since it will never read any of the objects that will be GC'ed. – Mathias Schwarz Jun 29 '11 at 13:29
0

It is not going to make a difference. The time it takes to garbage collect (newly allocated objects) is proportional to the number of objects that should not be garbage collected. To put it in another way: removing references between objects that should be garbage collected will not speed up garbage collection, it will just waste a little time setting variables to null...

Mathias Schwarz
  • 7,099
  • 23
  • 28