Late cleanup of stack objects
Arnaud Masson
arnaud.masson at fr.ibm.com
Tue Nov 8 13:56:11 UTC 2022
> I will check if C2 helps, but afaik since it’s triggered when method is
> used often (not because the method consumes significant memory), it’s
> not ideal to count on it anyway. Moreover, I suspect it’s not documented
> public behavior while lambda capture is well defined.
I have checked the following example with (no debugger)
--enable-preview
-XX:-TieredCompilation
-Xbatch
-Xcomp
-Xlog:nmethod+install
… to force C2, but the bigBuffer is still retained.
(Won’t C2 clear a stack ref only if it can be reused in the same scope for another java var?)
Thanks
Arnaud
import java.lang.management.ManagementFactory;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
public class Main1 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
var timer = new Timer();
var memBean = ManagementFactory.getMemoryMXBean();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.gc();
System.out.println("--- " + memBean.getHeapMemoryUsage());
}
}, 10*1000, 10*1000);
myApp();
}
private static void myApp() throws ExecutionException, InterruptedException {
var executor = Executors.newVirtualThreadPerTaskExecutor();
var future = executor.submit(() -> {
try {
System.out.println("Starting work");
var bigBuffer = new byte[1024 * 1024 * 1024];
System.out.println("bigBuffer size: " + bigBuffer.length);
// bigBuffer = null;
slowIO();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
future.get();
}
private static void slowIO() throws InterruptedException {
System.out.println("Starting slowIO");
Thread.sleep(Long.MAX_VALUE);
}
}
Unless otherwise stated above:
Compagnie IBM France
Siège Social : 17, avenue de l'Europe, 92275 Bois-Colombes Cedex
RCS Nanterre 552 118 465
Forme Sociale : S.A.S.
Capital Social : 664 069 390,60 €
SIRET : 552 118 465 03644 - Code NAF 6203Z
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20221108/f9fd8009/attachment-0001.htm>
More information about the loom-dev
mailing list