是否有任何逻辑可以取消引用变量以在 lambda 中使用以允许垃圾收集?
Is there any logic to dereferencing a variable for use within a lambda to allow garbage collection?
我有一个占用大量内存的对象 (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
是一个变量,它引用了一个class Foo
实例。
但是,是的,更改 lambda 表达式将启用 class 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 ]
}
这是一个有趣的问题,恕我直言。
以下是我如何稍微简化代码库:
public class DeleteMe {
public static void main(String[] args) {
Foo foo = new Foo();
Thread t = new Thread(() -> System.out.println(foo.bar.hello));
t.start();
}
static class Bar { // Use within lambda
String hello = "hello";
}
static class Foo { // Memory-heavy class
Bar bar = new Bar();
}
}
现在这是一个捕获 lambda,如果你 运行 它(带有特殊标志:-Djdk.internal.lambda.dumpProxyClasses=/some/path/goes/here
)并查看 de-compiled 代码,你会注意到它“捕获" 一个 Foo
实例 。用某种更简单的话来说,lambda 将在内部转换为 class(实现 Runnable
),它将以 Foo
实例作为参数。
如果将该代码更改为:
Foo foo = new Foo();
Bar bar = foo.bar;
Thread t = new Thread(() -> System.out.println(bar.hello));
t.start();
运行 使用相同的标志,反编译 - 你会看到现在捕获了一个 Bar
实例。
这改变了一切。
这意味着 Foo
实例符合 GC 条件,只要命中此行:
run(() -> System.out.println(bar.hello));
这正是您想要的。
其他代码有:
Thread t = new Thread(() -> System.out.println(foo.bar.hello));
正如我所说,将被转换为实现 Runnable
的 class
并且看起来像这样:
class RunnableImpl implements Runnable {
private final Foo foo;
public RunnableImpl(Foo foo) {
this.foo = foo;
}
public void run(){
Bar bar = foo.bar;
System.out.println(bar.hello);
}
}
不完全是这样,但是(对于这个例子)如果我把它完全翻译正确也没关系。这意味着 Thread
实例将在其中存储一个 Runnable
实例(RunnableImpl
的实例),如上所示,它又将具有 Foo
的实例.
所以在第二种变体中,foo
只能在Thread结束时进行收集;这可能会晚很多,甚至在线程开始之前。
我有一个占用大量内存的对象 (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
是一个变量,它引用了一个class Foo
实例。
但是,是的,更改 lambda 表达式将启用 class 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 ]
}
这是一个有趣的问题,恕我直言。
以下是我如何稍微简化代码库:
public class DeleteMe {
public static void main(String[] args) {
Foo foo = new Foo();
Thread t = new Thread(() -> System.out.println(foo.bar.hello));
t.start();
}
static class Bar { // Use within lambda
String hello = "hello";
}
static class Foo { // Memory-heavy class
Bar bar = new Bar();
}
}
现在这是一个捕获 lambda,如果你 运行 它(带有特殊标志:-Djdk.internal.lambda.dumpProxyClasses=/some/path/goes/here
)并查看 de-compiled 代码,你会注意到它“捕获" 一个 Foo
实例 。用某种更简单的话来说,lambda 将在内部转换为 class(实现 Runnable
),它将以 Foo
实例作为参数。
如果将该代码更改为:
Foo foo = new Foo();
Bar bar = foo.bar;
Thread t = new Thread(() -> System.out.println(bar.hello));
t.start();
运行 使用相同的标志,反编译 - 你会看到现在捕获了一个 Bar
实例。
这改变了一切。
这意味着 Foo
实例符合 GC 条件,只要命中此行:
run(() -> System.out.println(bar.hello));
这正是您想要的。
其他代码有:
Thread t = new Thread(() -> System.out.println(foo.bar.hello));
正如我所说,将被转换为实现 Runnable
的 class
并且看起来像这样:
class RunnableImpl implements Runnable {
private final Foo foo;
public RunnableImpl(Foo foo) {
this.foo = foo;
}
public void run(){
Bar bar = foo.bar;
System.out.println(bar.hello);
}
}
不完全是这样,但是(对于这个例子)如果我把它完全翻译正确也没关系。这意味着 Thread
实例将在其中存储一个 Runnable
实例(RunnableImpl
的实例),如上所示,它又将具有 Foo
的实例.
所以在第二种变体中,foo
只能在Thread结束时进行收集;这可能会晚很多,甚至在线程开始之前。