With the System.gc()
method it is possible to force a full GC run. This is very expensive. Is there an option to force the first generation GC only? My application is currently running on Java 11. I am thinking of something like MemoryPoolMXBean
.
Background: I want to start a large memory consuming task and if the free memory is lower than the needed memory then I want to know if I can start it without risk or should I try it later or swap cache to the disk. In real I want to know the free memory after such first GC. Our analysis shows that this will be enough in most cases (>99%).
I use the follow code to check if there are enough memory for the memory intentive operation: