Can we call the Garbage Collector explicitly? [dup

2019-03-25 16:56发布

This question already has an answer here:

My application has lot of iterations. Till now I haven't faced any memory issues. But from code level I can suspect there are few places, which causes memory leaks and out of memory problem. I am thinking of calling garbage collector manually. Is it good practice to call Garbage Collector manually?

6条回答
Rolldiameter
2楼-- · 2019-03-25 17:26

Is it good practice to call Garbage Collector manually?

No, it is definitely not good practice.

You can use System.gc(). Note that this is not guaranteed to call the garbage collector - it only gives a hint to the system that it might be a good idea to do garbage collection.

The garbage collector in Oracle's JVM contains a lot of sophisticated logic to determine when and what to cleanup. Tuning it requires knowledge about details on how it works. Just putting a System.gc() somewhere in your program is not likely to help a lot, and in fact, it can even make it worse.

See Java SE 6 HotSpot Virtual Machine Garbage Collection Tuning for details on how to tune garbage collection with Java SE 6.

查看更多
别忘想泡老子
3楼-- · 2019-03-25 17:28

Yes you can explicitly call garbage collector using

System.gc();

But what happens is that you can't order JVM to do garbage collection immediately. JVM decides on its own when to garbage collect.Hence its not a good idea of calling it manually.

Moreover regarding OutOfMemoryException doing manual garbage collection won't help you prevent the exception, since JVM throws this exception after reclaiming all the memory it can. It has some very sophisticated algorithms to determine when and how to do perform the garbage collection. So what i suggest is that if you are getting OutOfMemoryException then recheck your program make it more efficient or increase heap space.

查看更多
Root(大扎)
4楼-- · 2019-03-25 17:31

You can call Garbage Collector explicitly, but JVM decides whether to process the call or not. Ideally, you should never write code dependent on call to garbage collector.

JVM internally uses some algorithm to decide when to make this call. When you make call using System.gc(), it is just a request to JVM and JVM can anytime decide to ignore it.

查看更多
我命由我不由天
5楼-- · 2019-03-25 17:31

Calling System.gc() does not guarantee any GC. Garbage is collected if there is a real need for memory.
You can have a look at different garbage collectors here.
http://javarevisited.blogspot.in/2011/04/garbage-collection-in-java.html

You can include any of these GCs in your command line params as per your needs.

查看更多
看我几分像从前
6楼-- · 2019-03-25 17:39

Regardless of whether you can or cannot manually trigger the garbage collector (and the different levels of collection), and what impact this has on performance (which indeed is a topic worth discussion), it will not prevent OutOfMemoryErrors, because when the JVM is about to run out of memory, it does the most thorough collection it can anyway. Only if after that collection not enough memory is available, will it error out. Even if you trigger the collection earlier yourself, the result (the amount of memory reclaimed) is the same.

Memory leaks cannot be fixed by running garbage collection more often.

They have to be fixed in your program (stop referencing things you don't need anymore earlier), or (worst case, if it is a "real" leak) in the JVM or runtime library itself (but genuine memory management bugs should not exist anymore after so many years of service).

查看更多
Ridiculous、
7楼-- · 2019-03-25 17:44

You can call Garbage collector using:

System.gc();

But this does not mean that it'll be executed immediately. The JVM decides when to execute it. In general if the JVM is about to throw an OutOfMemoryError, calling System.gc() won't prevent it. Better investigate why you're leaking so much memory and clean it up along the way.

JavaDoc:

Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects

查看更多
登录 后发表回答