The code to reproduce is really simple and the test should be easy to understand.
The behaviour is very counterintuitive.
I've written a library to write tests for memory leaks, which makes this very easy to read and understand.
The framework: https://github.com/Sandec/JMemoryBuddy
This is the test:
@Test
public void lambdaSanityTest() {
JMemoryBuddy.memoryTest(checker -> {
//fails
Runnable r = () -> { System.out.println("a");};
// works
//Runnable r = new Runnable() {
// @Override
// public void run() {
// System.out.println("b");
// }
//};
checker.assertCollectable(r); // not referenced should be collectable
});
}
It seems as the lifetime of the runnable is the same as the lambda provided to the test.
This also seems to be clearly wrong to me, because converting to an anonymous class fixes the problem.
In my opinion, converting to anonymous classes shouldn't have any change in behavior.
This might be a bug in Java Language, or in the hotspot.
The behaviour is very counterintuitive.
I've written a library to write tests for memory leaks, which makes this very easy to read and understand.
The framework: https://github.com/Sandec/JMemoryBuddy
This is the test:
@Test
public void lambdaSanityTest() {
JMemoryBuddy.memoryTest(checker -> {
//fails
Runnable r = () -> { System.out.println("a");};
// works
//Runnable r = new Runnable() {
// @Override
// public void run() {
// System.out.println("b");
// }
//};
checker.assertCollectable(r); // not referenced should be collectable
});
}
It seems as the lifetime of the runnable is the same as the lambda provided to the test.
This also seems to be clearly wrong to me, because converting to an anonymous class fixes the problem.
In my opinion, converting to anonymous classes shouldn't have any change in behavior.
This might be a bug in Java Language, or in the hotspot.