Is calling `System.gc()` a good way to manage memory in a Java application?
I have a java application that wants to invoke the 开发者_运维问答system.gc(). Is it a reasonable method to release memory? or any other advice? Really appreciate!
Just stop referencing the variable. You don't need to invoke System#gc()
yourself. If the JVM is on an edge of an OutOfMemoryError
, it will certainly run the GC.
If stopping referencing the variables is not an option because you really need them, then you need to profile your application to fix/clean any memory leaks and/or just give JVM more memory at startup.
In Java it is not necessary to explicitly invoke garbage collection. It is done automatically by the virtual machine.
Don't call System.gc()
. Running the garbage collector unnecessarily is a great way to ruin the performance of your program. The JVM will collect garbage when it needs to.
One of the biggest advantages of the Java platform is successful automated garbage collection. Use it.
Only under very specific circumstances and after being proven by rigorous profiling, would it be a good idea to explicitly invoke gc()
. It should never make a functional difference, but it might have perceived performance gains under very certain scenarios.
For example, say you were making a video game with Java and every few minutes you come to a break between levels. This might prove to be a good place to explicitly invoke the Garbage Collector if it reduces the chance that a GC cycle will occur while playing the next level (which could be disruptive to gameplay).
In short, it should be reserved for times where you know better than the runtime when the GC is most desirable, and again, it should only be used after rigorous profiling justifying its inclusion.
System.gc() will begin Full Collection, which is especially bad if you care about pause times and are using the concurrent or G1 collectors.
Unless you have isolated a specific reason that you that you need different behavior, you shouldn't play with garbage collection... you will more likely hurt performance than see any gains.
the book Head First Java says that each variable (instance or local variables) has an scope, when the scope is lost then the variable doesn't exist anymore, when talking about objects it says that an object is elegible for garbage collector when his last live reference disappears.
Soft & weak references are a great way to hold onto data that you'd like to be in memory but can do without if memory gets tight.
Normal references are strong, i.e. an object doesn't get finalized or garbage collected while someone holds a strong reference to it. If you hold a SoftReference to an object, the object will likely stay in memory until an OutOfMemory situation when it will be automatically flushed. A WeakReference is likely to be flushed at any time but it might still be useful for occasions where holding an object saves some disk overhead.
Due to these characteristics references are great for caching data without worrying about having to flush data if the memory starts getting tight.
Easiest way to create a reference is like this:
Reference<MyClass> r = new SoftReference<MyClass>(new MyClass())
Now you pass the reference around. When you wish to obtain the object you call Reference.get()
MyClass myClass = r.get();
if (myClass != null) {
// do something
}
else {
// Oh dear class isn't there, go to plan b
}
With gc() you just say to the JRE that it should call the garbage collector. Sometimes that suggestion can be useful but in the most cases just setting all the references to the null is the right way.
精彩评论