I have got such code:
public static class Obj {//Class within class
Long a;
}
private static final Map<String, Obj> map = new ConcurrentHashMap<>();
public static void main(String[] args)
{
for(long l = 0;l<10000090L;l++)
{
Obj o = new Obj();
o.a = l;
map.put("asd", o);
map.remove("asd");
}
}
On my PC i have added this code in a Thread - so i can easily see memory usage. I have started profiling it with Your Kit Java Profiler.
I have added additional arguments to VM: -XX:+UseConcMarkSweepGC -XX:+UseTLAB -XX:+CMSIncrementalMode -XX:+CMSIncrementalPacing
My result of testing this program:
Before loop Your Kit is showing - User Par Serivce Space 0 bytes, User Par Eden Space 12 mb, Used CMS Old Gen 0 Bytes
After loot Your Kit is showing - User Par Serivce Space 8 KB, User Par Eden Space 38 mb, Used CMS Old Gen 0 Bytes 1,6 mb
Then i forced Garbage Collector, results:
Your Kit is showing - User Par Serivce Space 0 bytes, User Par Eden Space 1,4 mb, Used CMS Old Gen 1,5 mb
So if i read this values correctly, Obj or "asd" is staying in memory until Full Garbage collector.
Is there any way to make those objects be removed in real time without waiting for Garbage collector to check is every object in program can be accessed?