关于垃圾收集。为什么我们需要调用System.gc();? [英] regarding garbage collection.Why do we need to call System.gc();?

查看:113
本文介绍了关于垃圾收集。为什么我们需要调用System.gc();?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

垃圾收集在对象被引用时自动调用,不再可用于任何变量。但是我喜欢知道为什么当我们自动调用垃圾回收时显式调用System.gc()。当我们调用System.gc();

解决方案

你没有。正如你所说,垃圾收集是自动的。 System.gc()甚至不强制垃圾收集;这只是给JVM一个提示,现在可能是清理一下的好时机。通常,试图强制垃圾回收器做你想做的事情与System.gc()是一个黑客应用程序的人认为他们知道比他们实际做得更好,或作为破解代码的(尝试)解决方法。



我一直在写Java多年,我还没有看到调用System.gc真的是正确的事情(在我写的任何东西中)。


Garbage collection is called automatically when an object is refered to is no longer available to any variable. But I like know why do we call explicitly using System.gc() when garbage collection is called automatically.When do we call System.gc();

解决方案

You don't. As you say, garbage collection is automatic. System.gc() doesn't even force a garbage collection; it's simply a hint to the JVM that "now may be a good time to clean up a bit"

In general, trying to force the garbage collector to do what you want with System.gc() is a hack applied by people who think they know better than they actually do, or as an (attempted) workaround for broken code.

I've been writing Java for years and I've yet to see a situation where calling System.gc was really the right thing to do (in anything I've written)

这篇关于关于垃圾收集。为什么我们需要调用System.gc();?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆