什么破坏了java中的局部变量? [英] What destroys the local variable in java?

查看:121
本文介绍了什么破坏了java中的局部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怀疑方法局部变量只在方法执行时才存在。另外,当 Eden Long-generation 块溢出(次要/主要GC)等时触发GC ...那么,如果在方法体 Eden 结束时没有溢出,那么不需要触发GC。尽管没有触发大/小GC,我们应该销毁所有局部变量。它是如何完成的?

解决方案

垃圾收集器 - 收割者,有时会知道 - 按照自己的时间表运行,超出参考范围的对象。
当然,在方法退出后,局部变量不能被引用,因为它们超出了范围,所以对于你的程序它们已经死了,但是它们仍然存在于堆上直到GC运行。



在正常情况下(以及大多数异常情况下),您无需告诉Reaper何时执行其工作。它会在需要时默默地来到,并带走那些不再需要的东西。这是使用高级语言工作的主要优点之一:假设您从不需要考虑管理死去的对象的释放等事情。你可以把它们抛在肩上,并知道它们永远不会打扰你。我想有一些高性能,高需求的应用程序需要GC来进行调试,但这是一种优化,应该始终假设为不成熟,除非您有相反的证据。

I suspect that the method-local variables live only as the method is being executed. Also, GC is triggered when Eden or Long-generation blocks are overflowed (minor/major GC) etc... So, what if at the end of the method body Eden is not overflowed, so there is no need to trigger GC. In spite of not triggering major/minor GC we shall destroy all local variable. How is it done?

解决方案

The garbage collector - the Reaper, as it's sometimes known - runs on its own schedule, and collects objects which are out of reference. The local variables, of course, cannot be referenced after the method exits, because they are out of scope, so to your program they are dead, but they still exist on the heap until the GC runs.

Under normal circumstances (and most abnormal ones) you do not need to tell the Reaper when to do its work. It will come, silently, when it is needed, and carry away those things which are no longer needed. This is one of the major advantages of working in a high-level language: it's safe to assume that you never need to think about things like managing deallocation of dead objects. You can just toss them over your shoulder and know that they will never bother you. I suppose there are some high-performance, high-demand applications that need to fiddle with the GC, but that's an optimization which should always be presumed premature unless you have really good evidence to the contrary.

这篇关于什么破坏了java中的局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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