-3

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.

berkayk
  • 2,376
  • 3
  • 21
  • 26
  • 2
    @MitchWheat On linux with enough cores [it could](http://stackoverflow.com/questions/14579124/why-does-whiletrue-without-thread-sleep-cause-100-cpu-usage-on-linux-but)... – assylias Jun 04 '13 at 10:29
  • @MitchWheat you are wrong. In linux CPU usage seems to be represented for each core. So 2 cores each with 100% usage will show as 200% – Matthew Kirkley Jun 04 '13 at 10:31
  • 2
    @Matthew Kirkley: sigh! there is a difference with what is shown and what is actually correct! – Mitch Wheat Jun 04 '13 at 10:32
  • 2
    @MitchWheat You have decided to define for yourself what "100% CPU" means. You seem to be alone in your definition. – Marko Topolnik Jun 04 '13 at 10:37
  • @MarkoTopolnik Not alone, the Microsoft world is on his side ;-) – assylias Jun 04 '13 at 10:38
  • @assylias Yes, and Microsoft is *The Truth* :) – Marko Topolnik Jun 04 '13 at 10:38
  • On my ubuntu server, yeah it shows 1190% now. When I start the server, CPU initializes with 10% then it jumps by 100%. I am not going to lie for a simple cause. http://imgur.com/UdX9UDk shows the top output. – berkayk Jun 04 '13 at 10:50
  • Sorry but your comments made this thread very very useless, now I have to open another thread to get help. – berkayk Jun 04 '13 at 12:52
  • If you compare two quantities it is valid to say one is 110% of the other. BUT if you have some resources (regardless of what those resources are), the maximum value is 100%. Period. By definition. Not my definition. Not Microsoft's. Also, I'm unclear why you reference MS; I didn't. – Mitch Wheat Jun 04 '13 at 13:46
  • By default, top on Linux runs with IRIX mode on (hit shift-I to toggle). With IRIX mode on, on e.g. a 4 core system, top will show 1 fully utilized core as 100%, and 4 cores as 400%. With IRIX mode off, it shows 1 core as 25% and 4 cores as 100%. The Windows task manager is the same as top with irix mode off. @see http://stackoverflow.com/questions/14579124/why-does-whiletrue-without-thread-sleep-cause-100-cpu-usage-on-linux-but?lq=1 – Matthew Kirkley Jun 04 '13 at 14:30

3 Answers3

2

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

user1549804
  • 131
  • 1
  • 10
  • I am having difficulties on connection remotely. On the development system (my computer) I am not seeing anything unusual. After deploying to server and waiting 1-2 hours, usage jumps by 100%. I haven't tried anything new (like a new data structure or a different class type) on my latest additions to the code. – berkayk Jun 04 '13 at 10:54
  • The previous comments might be correct that the percentage might come from the other cores and support for more threads from your server. [some article](http://java.dzone.com/articles/high-cpu-troubleshooting-guide) – user1549804 Jun 04 '13 at 19:55
  • jvisualvm helped me find the infinite loop. Thanks. – berkayk Jun 05 '13 at 13:43
1

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.

BrookLeet
  • 58
  • 2
1

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

Matthew Kirkley
  • 4,138
  • 5
  • 31
  • 33