Reputation: 25768
I heard that assign a reference to null explicit will help gc to collect it.
Is that true?
If an object is out of scope, will it get gc quickly?
Upvotes: 3
Views: 228
Reputation: 109134
In general, if the reference is about to go out of scope, you don't need to explicitly null it as it will be gone soon any way. The only explicit nulling or removing of references I consciously use are:
Closing a resource associated with an object that has a longer lifetime than that resource. For example: a java.sql.Connection implementation usually has an associated physical connection (eg a Socket): when the java.sql.Connection is closed you can null this physical connection as you are no longer using it, while the actual java.sql.Connection will (might) still be held by the user for an indefinite time. (I use this example as I am a developer of a JDBC driver, in general this example does not occur for a Java developer, but similar situations exist)
Processing relatively large 'throw-away' objects in a list or array structure. For example: the JavaMail library provides methods to get messages; these return an array of messages. If you process the messages sequentially (and then no longer need them), nulling the array-entry after processing could reduce the memory footprint of your application (with IMAP it can 'on demand' load information from the server and store it in the message, increasing its size due to processing).
There are probably some other cases I will explicitly null a reference, but that is usually to signify the unavailability of something not out of concern about garbage collection or memory usage.
However as always: don't just null because you think it could help: null if you know it will help (so profile the code, measure memory usage etc). And if it is a one off hobby application or university assignment: don't bother.
Upvotes: 0
Reputation: 533720
If you have an array or reference which you intend to keep it can be worth null
ing it out.
If you have a long method with a large object which will not go out of scope immediately, refers to a large object it could be worth null
ing it out. However in this situation, it is better the break up the method at the point where the object is no longer needed so it goes out of scope.
Upvotes: 2
Reputation: 24667
If that is the only reference then the GC can free the space on the heap that the object was using. However, if there is another reference to the same object then setting the first reference to null will do nothing. The second reference will still keep the object alive - so the GC will not free the space.
Upvotes: 0
Reputation: 500743
if an object is out of scope, will it get gc quickly?
That is impossible to answer in general. However, if a reference is about to go out of scope, setting it to null
just before it does will almost certainly achieve nothing.
On the other hand, if the reference variable is long-lived, then setting it to null
may be useful if the referenced object is no longer needed.
Upvotes: 6
Reputation: 120268
Typically the JVM will do garbage collection when it needs to, so assigning a reference to null will not help it happen more quickly.
Upvotes: 3