9

I am monitoring my Java application (written in JDK 1.7) using VisualVM. Following is the graph that shows heap memory usage for the duration that this application ran.

enter image description here

Looking at this graph ones see that there are a lot of spikes in it. These spikes indicate creation of objects by the application. Once the application is done with them it destroys them using gc (implicitly called in this case).

Also , here is a screenshot of memory profiler when the application is still running

enter image description here

To me the up and down nature of the graph indicates efficient usage of java objects. Is this inference right ?

What is the ideal nature of the heap usage graph that one should aim for ?

Are there any other ways that I can improve on the heap memory usage by my application ?

davison
  • 335
  • 1
  • 3
  • 16
  • 2
    Lots of spikes to the right show program produces more short-lived objects as time goes. Possibly, this can be optimized. – Victor Sorokin Oct 09 '14 at 16:13
  • Your memory usage looks perfectly fine – Steve Kuo Oct 09 '14 at 16:21
  • @VictorSorokin The JVM GC handles lots of short lived objects quite well. This is not an issue. – pickypg Oct 09 '14 at 16:24
  • @VictorSorokin The spikes do show that once the objects go beyond their scope , the gc gets implicitly invoked and kills them. – davison Oct 09 '14 at 16:27
  • Actually, it's up to OP to decide if it's an issue. Generally, as garbage being produced faster, GC is invoked more frequently, stealing CPU time from application itself. I belive, OP isn't bothered with that, but in extreme cases, it may lead to `OutOfMemory`: http://stackoverflow.com/a/1393503/162634 – Victor Sorokin Oct 09 '14 at 16:30
  • Can you include a screen shot from your memory profiler? – Peter Lawrey Oct 09 '14 at 16:55
  • @PeterLawrey Updated the question with a screen shot of the memory profiler – davison Oct 09 '14 at 18:32

2 Answers2

4

To me the up and down nature of the graph indicates efficient usage of java objects. Is this inference right ?

I would say its the efficient use of the garbage collector. I would suggest creating less object might be more efficient.

What is the ideal nature of the heap usage graph that one should aim for ?

That depends on your application. I tend to aim for one which is almost completely flat.

Are there any other ways that I can improve on the heap memory usage by my application ?

Loads

  • create less garbage. Use your memory profiler to find out where garbage is being created.
  • make the heap larger so it doesn't GC as often.
  • move your retained data off heap (you don't appear to have a lot)

In your case, the best option would be to reduce the amount of garbage you are producing.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I am actually recreating the same java type of objects when i require them. Would it help to reuse the objects created but un-initialize (set then to null) them when not needed. In this case jvm wont mark them as garbage. Hence the gc would be less frequently invoked. – davison Oct 09 '14 at 16:54
  • @davison it could help if you recycle most of the objects. If you recycle just a top level object it might not help. If you haven't used a commercial profiler like YourKit you might find that there is 10 things better to optimise first. You can get an eval license for YourKit and other profilers. If I don't measure, I am just guessing. – Peter Lawrey Oct 09 '14 at 16:57
  • 1
    @davison you can eliminate these Iterators by using an indexed loop. – Peter Lawrey Oct 09 '14 at 18:55
1

As long as the heap size keep almost same over time, you are ok. Used heap should go up and down due to the nature of pause the world gc in Sun JVM. Looks like lots of short lived objects are produced in your app, it may be inefficient, but sometimes you need create them. It's the lifestyle of Java :D

vxst
  • 79
  • 4