English 中文(简体)
Java vs Net
原标题:Objects lifespan in Java vs .Net

我读过“CLR通过C#”,在这个例子中,最初被分配到肥料的物体在一行执行后将有资格获得垃圾收集,而不是在第二行之后。

void Foo()
{
    Object obj = new Object();
    obj = null;
}

That s because local variable lifespan defined not by scope in which it was defined, but by last time you read it.

So my question is: what about Java? I have written this program to check such behavior, and it looks like object stays alive. I don t think that it s possible for the JVM to limit variable lifetime while interpreting bytecode, so I tried to run program with java -Xcomp to force method compilation, but finalize is not called anyway. Looks like that s not true for Java, but I hope I can get a more accurate answer here. Also, what about Android s Dalvik VM?

class TestProgram {

    public static void main(String[] args) {
        TestProgram ref = new TestProgram();
        System.gc();
    }

    @Override
    protected void finalize() {
        System.out.println("finalized");
    }
}

Added: Jeffrey Richter gives code example in "CLR via C#", something like this:

public static void Main (string[] args)
{
    var timer = new Timer(TimerCallback, null, 0, 1000); // call every second
    Console.ReadLine();
}

public static void TimerCallback(Object o)
{
    Console.WriteLine("Callback!");
    GC.Collect();
}

TimerCallback called only once on MS .Net if projects target is Release (timer destroyed after GC.Collect() call), and called every second if target is Debug (variables lifespan increased because programmer can try to access object with debugger). But on Mono callback called every second no matter how you compile it. Looks like Mono s Timer implementation stores reference to instance somewhere in thread pool. MS implementation doesn t do this.

最佳回答

请注意,仅仅因为要收集一个物体can,并不意味着实际上会收集某个特定点,因此,你的计算方法会带来错误的否定。 如果任何物体的<代码>定型<>代码/代码>方法被称作是,你可以肯定地说,这种方法是无法做到的,但如果不把这种方法称作你,就能够从逻辑上推断出任何东西。 与大多数与GC有关的问题一样,垃圾收集器的不确定性使得很难对它究竟将做些什么进行测试/保证。

On the topic of reachability/collectability, the JLS says (12.6.1):

A reachable object is any object that can be accessed in any potential continuing computation from any live thread. Optimizing transformations of a program can be designed that reduce the number of objects that are reachable to be less than those which would naively be considered reachable. For example, a 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.

无论你所期望的多少,我还是认为,上述段落与“一个物体是不可接受的,你肯定会赢得更多的使用”。

回到你原来的情况, ,你可以认为物体在第1行后不可接受与第2条之间有任何实际影响? 我最初的反应是,没有,如果你能够找到这样的情况,那么,这很可能是一个坏/扭曲的法典的标志,导致证书制度的斗争,而不是语言固有的弱点。

虽然我愿意反对。


Edit: 感谢有趣的例子。

我同意你的评估,并看到你回过来,尽管这个问题可能更严重地改变了你的法典的语义。

在书面守则中,你将<编码> 日码<>/代码> 分配给一个地方变量,但随后不在其中阅读。 即使是最细微的越狱分析也可显示timer。 在<代码>main方法中,其他任何地方使用的变量是n t,因此可以加以解释。 因此, 我认为,你的第一行完全等同于直接援引建筑商:

public static void Main (string[] args)
{
    new Timer(TimerCallback, null, 0, 1000); // call every second
    ...

在后一种情况下,可以清楚看出,在施工之后,新创建的<代码> 物体可立即到达(假定该物体在构造中不像在固定的田地上那样做任何污点);因此,该物体一经GC山轮即可收集。

现在,就 de而言,情况明显不同,因为你所提到的理由是,开发商可能希望在方法上稍后检查当地变量的状况。 因此,汇编者(和JIT汇编者)可以选择放弃这些内容;如同在方法结束时能够使用变数一样,防止收集工作到那时。

即便如此,我也不认为这实际上改变了语气。 GC的性质是,收集很少得到保证(至少在 Java,获得的唯一保障是,如果扔出一台外围信,那么所有被认为无法收买的物品都立即经过GC处理)。 事实上,假定你拥有充裕的空间来持有在运行时间期间制造的每一种物体,那么无机的GC执行是完全有效的。 因此,虽然你可能看到“<密码>><> >>>> /代码>的多少次发生行为变化,但这是很不错的,因为没有保证你根据你重新援引该编码的方式看待什么。 (从概念上来说,这类似于在万国邮联密集任务期间运行的时间,在系统负荷时会有更多的时间,结果都不错,因为接口没有提供这种保障。)

At this point I refer you back to the first sentence in this answer. :)

问题回答

总的来说, Java的行为是,如果标的在范围上可以达到(指的不是垃圾),则标的不是垃圾。 如果<代码>a是指指指指<>条码>的物体,则该物体的编号为

Within the scope where you can still reach the object referenced by ref, (you could add a the line System.out.println(ref.toString())), ref is not garbage.

然而,根据,这一旧来源来自Sun s网站,大部分取决于具体实施国际交易日志。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...