未使用的对象是否可用于垃圾回收,但仍然可以在堆栈中看到它们? [英] Is unused object available for garbage collection when it's still visible in stack?

查看:91
本文介绍了未使用的对象是否可用于垃圾回收,但仍然可以在堆栈中看到它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的例子中,有两个功能相当的方法:

  public class Question {

public static String method1(){
String s = new String(s1);
//对s1进行一些操作
s = new String(s2);
return s;


public static String method2(){
final String s1 = new String(s1);
//对s1进行一些操作
final String s2 = new String(s2);
返回s2;


$ / code>

然而在第一个( method1 )其中字符串s1在 return 语句之前显然可用于垃圾回收。在第二个( method2 )中,字符串s1仍然可以访问(尽管从代码审查的角度来看它已经不再使用了)。

我的问题是 - jvm规范中有没有什么规定说,一旦变量在堆栈中未被使用,它就可以用于垃圾回收了? b
$ b

编辑:
有时变量可以引用像完全呈现的图像这样的对象,记忆。


因为实际考虑,我在问。我有一大块内存贪婪的代码在一种方法,并认为如果我可以帮助JVM(一点),只需将这种方法分成几个小的。



我真的



更新 :每个 href =http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.6.1 =nofollow> jls-12.6.1


Java编译器或代码生成器可能会选择设置一个变量或参数,该变量或参数将不再用于null,以便为此类存储一个对象有可能被快速收回


所以看起来GC有可能声称仍然可见的对象。我怀疑,然而,这种优化是在离线编译期间完成的(它会搞砸调试),而且很可能会由JIT完成。

解决方案

不,因为你的代码可以想象地检索它并对它做些什么,而抽象的JVM不会考虑将要发布什么代码。但是,一个非常非常非常聪明的优化JVM可能会分析前面的代码,并发现无法引用 s1 并将其垃圾收集。不过,你绝对不能指望这一点。


In the following example there are two functionally equivalent methods:

public class Question {

    public static String method1() {
        String s = new String("s1");
        // some operations on s1
        s = new String("s2");
        return s;
    }

    public static String method2() {
        final String s1 = new String("s1");
        // some operations on s1
        final String s2 = new String("s2");
        return s2;
    }
}

however in first(method1) of them string "s1" is clearly available for garbage collection before return statement. In second(method2) string "s1" is still reachable (though from code review prospective it's not used anymore).

My question is - is there anything in jvm spec which says that once variable is unused down the stack it could be available for garbage collection?

EDIT: Sometimes variables can refer to object like fully rendered image and that have impact on memory.

I'm asking because of practical considerations. I have large chunk of memory-greedy code in one method and thinking if I could help JVM (a bit) just by splitting this method into few small ones.

I really prefer code where no reassignment is done since it's easier to read and reason about.

UPDATE: per jls-12.6.1:

Java compiler or code generator may choose to set a variable or parameter that will no longer be used to null to cause the storage for such an object to be potentially reclaimable sooner

So it looks like it's possible for GC to claim object which still visible. I doubt, however that this optimisation is done during offline compilation (it would screw up debugging) and most likely will be done by JIT.

解决方案

No, because your code could conceivably retrieve it and do something with it, and the abstract JVM does not consider what code is coming ahead. However, a very, very, very clever optimizing JVM might analyze the code ahead and find that there is no way s1 could ever be referenced, and garbage collect it. You definitely can't count on this, though.

这篇关于未使用的对象是否可用于垃圾回收,但仍然可以在堆栈中看到它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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