简体   繁体   中英

Java System.GC() and Memory Leakage

I have read up and searched through the forums about leaking memory and gradually increasing RAM. I tried to use the call of System.GC() method every 60 seconds in my program and it seems to be working given that my RAM usage drops every call. Why is it a good idea not to use this method? In every post I have read they seemed to vaguely explain why the method does not free up memory, yet my program seems to say otherwise. Some even said the method did nothing at all but suggest to the Garbage Collector clean itself up. NOTE : My leak is not from static methods I know because I removed them from my entire project and the RAM still increased. I would post my code, but it is rather large so I doubt anyone is up to reading it.

Thanks for the help.

As you stated, System.gc() is just a suggestion. It's not guaranteed to force the garbage collection, though in practice it frequently does.

The Java garbage collection runs on its own periodically. If you see that your memory is increasing over time and you're not reclaiming it, then you have a memory leak. Calling System.gc() won't fix that. If your memory is leaking, eventually there will be nothing to collect.

In general, you shouldn't need to force GC. As I mentioned, the GC will run on its own. You can tweak its behavior - http://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html .

The original problem is comes from memory leak. Sympthom is * because of memory leak, there are not enough memory space * so the JVM will try to do GC again again again. * but still have enough memory. so GC again again.

So the System.GC or kind of GC tuning is not helpful.

To fix this problem, u have to find our where is the memory leakage point.

In JVM, there are tools that dumps current memory foot print (heapdump).

You can find out the leakage point by using this. For more information, please refer this - http://www.oracle.com/technetwork/java/javase/memleaks-137499.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM