当您调用System.gc()时,幕后究竟发生了什么? [英] What exactly takes place behind the scenes when you call System.gc()?

查看:267
本文介绍了当您调用System.gc()时,幕后究竟发生了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用System.gc()要求进行垃圾收集,但不能保证会进行垃圾收集.不能保证的部分对我来说很有趣:有人可以解释您拨打此电话时发生的确切过程吗?

Calling System.gc() requests that garbage collection takes place, and isn't guaranteed. The part about it not being guaranteed is interesting to me: can someone explain the exact process that takes place when you make this call?

推荐答案

不能保证将进行垃圾回收,因为不能保证JVM支持垃圾回收(我不相信Java垃圾回收中提到了它.完全是JLS,当然不在索引中)

There are no guarantees that a garbage collection will take place, because there are no guarantees that the JVM supports garbage collection (I don't believe it's mentioned in the JLS at all, certainly not in the index).

但是,我认为这种不能保证"的信念有些错误.从Sun的 API文档:

However, I think this belief that "it's not guaranteed" is a little misplaced. From the Sun API docs:

当控件从方法返回时 调用,Java虚拟机具有 尽力回收空间 从所有丢弃的物体中.

When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

好的,所以也许不能保证,但是这向我表明,当您调用此方法时,普通的JVM实际上会启动GC周期.也许有些JVM不会,也许将来的垃圾收集器也不会,但是就目前而言,它已经做了广告.

OK, so maybe it's not guaranteed, but this indicates to me that a normal JVM will actually start a GC cycle when you call this method. Perhaps some JVMs won't, and perhaps some future garbage collector won't, but for now, it does what's advertised.

当然,也就是说,应用程序代码几乎没有理由调用此方法. JVM将在需要时收集垃圾.

Of course, that said, there's almost never a reason for application code to call this method. The JVM will collect the garbage when it needs to do so.

这篇关于当您调用System.gc()时,幕后究竟发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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