Marking an Object To Be Removed In The GC

2019-03-19 04:32发布

问题:

Is there away that I can mark an object, in java, to be garbage collected for the next time it cleans up?

I've heard that setting the object to null no longer works.

回答1:

I know this question has been answered, but there are ways you can manipulate the way that the Java Garbage collector looks at your references. You can do this through Soft References, Weak References, and Phantom References. Take a look at the java.lang.ref package for a better explanation.

http://download.oracle.com/javase/1.5.0/docs/api/java/lang/ref/package-summary.html

Also here's a nice sample to determine when a PhantomReference is about to be garbage collected.

http://exampledepot.8waytrips.com/egs/java.lang.ref/PhantomRef.html

Here's the main site it came from:

http://exampledepot.8waytrips.com/



回答2:

No, you can't. What would you expect to happen if another variable had a reference to it?

Note that you can't set an object to null - you can only set a variable to null. If another variable still has a reference to the object, it will still not be eligible for garbage collection.

If you think you need to do this, that probably means you're misinterpreting your data - or that you may have a leak somewhere in your code (e.g. a list which you only ever add entries to, referenced by a static variable - those entries will never be eligible for garbage collection while the classloader is alive).

Each JVM has its own GC, but in Hotspot an object will be garbage collected next time the GC runs over the generation that object currently "lives" in (assuming it doesn't have a finalizer, which complicates things). If the object is in a "young" generation, that will probably happen quite soon - if it's in an "old" generation it may well take longer.

You may want to see the Java 6 GC tuning documentation for more information, although of course things have moved on since then for OpenJDK 7 etc.



回答3:

The Java Garbage Collector works with a mark and sweep method. This means from object that are known to still be in used all references are followed and the objects that are visited in that way are marked. In that way objects with no references at all are not marked and should be sure to be deleted. So you could ensure that all references to this object are removed, so that in the next turn of the Garbage Collector, the item is deleted.

Additionally you could use

Runtime.getRuntime().gc();

to indicate that the Garbage Collector should run. Note: you can't be sure that it really runs.



回答4:

You heard wrong, but then again the description is wrong too.

You don't set an object to null, you set a variable to null. If the variable can be used to get to an object, then the variable has a "reference" to the object. Setting the variable to null is the same as the variable "losing the reference" to the object.

Once Java detects that an object, or a group of objects can't be reached by the running program, it will remove those objects from memory. It won't remove them from memory one moment sooner, because if it did, and some other part of the program tried to use a reference to the object, then the reference would fail in a way that's not permissible in Java.

The trick is not to set just one reference to null, you have to set all the references that might have been made to null. That's why it's important to consider each time you create a new reference, because you want to create them in such a manner that they will eventually be cleared (unless you want a memory leak).