Domanda

My server has some kind of memory leak or something, java CPU usage jumps to 100% in an hour and after many hours, it jumps to 1100%.

I was wondering if this loop traversing may cause a memory leak.

for (Object o : friends.values()) {
    doSomethingWith(o); 
}

friends is a ConcurrentHashMap and it's content may change on some situations when a friend goes online or offline.

If this is safe, how can I find the memory leak?

Thanks in advance.

È stato utile?

Soluzione

you can use visualvm to monitor your application while it is running.

Create a memory dump and use Eclipse Memory Analyzer to study your dump

You should be able to get more details with these tools

Altri suggerimenti

ConcurrentHashMap is thread safe indeed. so i don't think it's the root cause of your mem leak. in fact, if u has some terrible or heavy codes in your code of 'doSomethingWith' it will cause the CPU occupation problem. memory leak is not the same as CPU high rate. memory leak means app's memory increase continuously and not low down again.

The loop traversing certainly could cause a memory leak, but it would depend on what is happening in your 'doSomethingWith(o)' method.

You will want to get your hands on a profiling tool such as http://www.ej-technologies.com/download/jprofiler/files

or a free tool such as http://docs.oracle.com/javase/6/docs/technotes/guides/visualvm/intro.html

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