可能存在重复: 在Java中强制垃圾回收?
我可以强制垃圾以任何方式收集Java?
System.gc()只是一个建议。它没用。
当我确定某些资源不再被使用时,为什么我不能强制清除它们?
就像C ++中的delete()和free ()in C?
当有很多资源不能被重用时,这可能真的吸引了性能。我们所能做的就是sleep()。
任何解决方案?谢谢 解决方案
)尽可能接近你可以得到的。 Java不是C或C ++,JVM为你管理内存,所以你没有那种细粒度的控制。如果您将不再使用的对象设置为null,或者放弃所有引用,它们将被清除。而且GC非常聪明,所以它应该好好照顾你。
也就是说,如果你在unix盒子上,强制线程转储(kill - 3),它会非常强制垃圾回收。
Possible Duplicate: Forcing Garbage Collection in Java?
Can I Force Garbage Collection in Java by any means?
System.gc() is just a suggestion.It's useless.
When I know for sure that some resources won't be used any more,why can't I force to clean them?
Just like delete() in C++ and free() in C?
When there are lots of resources that can't be reused,this can really suck the performance.All that we can do is sleep().
Any solutions?Thanks
解决方案Nope, System.gc() is as close as you can get. Java isn't C or C++, the JVM manages memory for you, so you don't have that kind of fine grained control. If you set objects you're no longer using to null, or loose all references, they will get cleaned up. And the GC is pretty smart, so it should take good care of you.
That said, if you are on a unix box, and force a thread dump (kill -3), it'll pretty much force garbage collection.
更多推荐
我可以在Java中强制垃圾回收吗?
发布评论