Domanda

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?

È stato utile?

Soluzione

Garbage Collector jumps in whenever it is required and in most of the cases it jumps in at a time which is the most optimized. Manually request GC to trigger using System.gc() might not give advantage at all.

Moreover, how do you know if "abj" is staying till full gc. Your eden generation has occupied 38 mb space and 0 mb in old generation. Which in your case with only short lived objects is the expected behavior (And normally small gc's should work unless you insert at a very fast rate and eden filled up quite quickly)

Altri suggerimenti

The garbage collector is the only thing that ever removes objects from memory. You can call System.gc() any time you like, which requests for the garbage collector to run sooner, rather than later. But usually, there's no need to. The garbage collector will run of its own accord, before your program gets into memory problems.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top