java - How do I check if ConcurrentLinkedQueue leaves garbage (dereferenced instances) for the GC? -
i using bunch of concurrentlinkedqueues in application , gc overhead huge. how check if concurrentlinkedqueue culprit? there standard way in java profile these info structures memory allocation/deallocation?
one way write simple test programme , run -verbose:gc jvm option. example, code:
import java.util.concurrent.concurrentlinkedqueue; public class testgc { public static void main(string[] args) throws exception { final concurrentlinkedqueue<string> queue = new concurrentlinkedqueue<string>(); string[] strings = new string[1024]; for(int = 0; < strings.length; i++) { strings[i] = "string" + i; } system.gc(); thread.sleep(1000); system.out.println("starting..."); while(true) { for(int = 0; < strings.length; i++) queue.offer(strings[i]); for(int = 0; < strings.length; i++) queue.poll(); } } } produces output:
$ java -verbose:gc testgc [gc 1352k->560k(62976k), 0.0015210 secs] [full gc 560k->440k(62976k), 0.0118410 secs] starting... [gc 17336k->536k(62976k), 0.0005950 secs] [gc 17432k->536k(62976k), 0.0006130 secs] [gc 17432k->504k(62976k), 0.0005830 secs] [gc 17400k->504k(62976k), 0.0010940 secs] [gc 17400k->536k(77824k), 0.0006540 secs] [gc 34328k->504k(79360k), 0.0008970 secs] [gc 35320k->520k(111616k), 0.0008920 secs] [gc 68104k->520k(111616k), 0.0009930 secs] [gc 68104k->520k(152576k), 0.0006350 secs] [gc 109064k->520k(147968k), 0.0007740 secs] (keeps going forever) now if want know culprit can utilize profiling tool. wrote this memory sampler can plug in code find out in source code line instances beingness created. do:
memorysampler.start(); for(int = 0; < strings.length; i++) queue.offer(strings[i]); for(int = 0; < strings.length; i++) queue.poll(); memorysampler.end(); if (memorysampler.wasmemoryallocated()) memorysampler.printsituation(); and when run get:
starting... memory allocated on lastly pass: 24576 memory allocated total: 24576 stack trace: java.util.concurrent.concurrentlinkedqueue.offer(concurrentlinkedqueue.java:327) testgc.main(testgc2.java:25) from can see line 327 of concurrentlinkedqueue leaking instances gc, in other words, not pooling them:
public boolean offer(e e) { checknotnull(e); final node<e> newnode = new node<e>(e); (node<e> t = tail, p = t;;) { java memory-management memory-leaks garbage-collection profiling
No comments:
Post a Comment