gpt4 book ai didi

java - 是否有任何逻辑可以取消引用变量以在 lambda 中使用以允许垃圾收集?

转载 作者:行者123 更新时间:2023-12-05 07:01:21 25 4
gpt4 key购买 nike

我有一个占用大量内存的对象 (foo) 和一个较小的对象 (bar)。我想确保 foo 被垃圾收集,但需要在程序的生命周期内访问我的 lambda 中的 hello 字段。

我想知道在创建我的 lambda 之前是否有任何逻辑可以首先将 bar 解除引用到它自己的变量中。我相信这可能会让 foo 被垃圾收集,但我不确定。

即在下面的示例中,因为我只使用 bar.hello,在 A 上执行 B 是否有任何逻辑允许 foo 被垃圾收集?第一个 lambda (A) 是否隐式持有对 foo 的引用,第二个 (B) 是否删除了该引用?

    class Bar { // Use within lambda
String hello;
}

class Foo { // Memory-heavy class
Bar bar;
String world;
}

Foo foo = new Foo();

// A (access bar through foo, will foo remain in memory?)
run(() -> System.out.println(foo.bar.hello));

// B (foo should have no more references, should be GC'd?)
Bar bar = foo.bar;
run(() -> System.out.println(bar.hello));

最佳答案

foo 准确的说是一个变量,它引用了一个类Foo的实例。

但是,是的,更改 lambda 表达式将启用类 Foo 实例的垃圾回收。

public class LambdaTest {

public static class OuterClass {
public InnerClass inner;
}

public static class InnerClass {
public String value;
}

public static void main(String[] args) {
// Attempting to define the value printer lambda here fails,
// since 'outer1' is neither final nor effectively final.
//
// Requiring 'outer1' to be final enables safe inlining of
// outer's value. Otherwise, a reference to stack frame
// would be held by the lambda. Languages which place stack frames
// on the heap don't do this. java is not one of those language.

// OuterClass outer1 = null;
//
// Runnable valuePrinter =
// () -> { System.out.println("Value [ " + outer.inner.value + " ]"); };
// outer1 = new OuterClass();

OuterClass outer1 = new OuterClass();

// The value of 'outer1' is inlined in the lambda.
//
// This creates a new reference to the value of 'outer1', which will prevent
// that OuterClass instance from being garbage collected.

Runnable valuePrinter1 =
() -> { System.out.println("Inside lambda1 [ " + outer1.inner.value + " ]"); };

// A new assignment to 'outer1' would cause the lambda definition to
// fail.

// outer = new OuterClass();

outer1.inner = new InnerClass();
outer1.inner.value = "TestValue1";
System.out.println("Outside lambda1 [ " + outer1.inner.value + " ]");
valuePrinter1.run();

// While the 'outer1' value was inlined, the other parts of the
// 'outer.inner.value' expression were not.
//
// Changes made to the 'value' and 'inner' references are visible to
// the lambda.

outer1.inner.value = "TestValue2";
System.out.println("Outside lambda1 [ " + outer1.inner.value + " ]");
valuePrinter1.run();

outer1.inner = new InnerClass();
outer1.inner.value = "TestValue3";
System.out.println("Outside lambda1 [ " + outer1.inner.value + " ]");
valuePrinter1.run();

OuterClass outer2 = new OuterClass();
InnerClass inner2 = new InnerClass();
outer2.inner = inner2;
inner2.value = "TestValue4";

// The InnerClass instance referenced by 'inner2' has no reference to
// the OuterClass instance referenced by 'outer2'.
//
// The lambda 'valuePrinter2' inlines the reference held by 'inner2'.
//
// A live reference to 'valuePrinter2' will not prevent the value referenced
// by 'outer2' from being garbage collected.

System.out.println("Outside lambda2 [ " + inner2.value + " ]");
Runnable valuePrinter2 =
() -> { System.out.println("Inside lambda2 [ " + inner2.value + " ]"); };
valuePrinter2.run();
}

// Output:
//
// Outside lambda1 [ TestValue1 ]
// Inside lambda1 [ TestValue1 ]
// Outside lambda1 [ TestValue2 ]
// Inside lambda1 [ TestValue2 ]
// Outside lambda1 [ TestValue3 ]
// Inside lambda1 [ TestValue3 ]
// Outside lambda2 [ TestValue4 ]
// Inside lambda2 [ TestValue4 ]

关于java - 是否有任何逻辑可以取消引用变量以在 lambda 中使用以允许垃圾收集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63887867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com