• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Profile a stand-alone Java program

 
Tarun Trehan
Ranch Hand
Posts: 51
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have written a simple program that loops through an array and creates some objects.
I would like to know how can profile this program to check for :

  • Memory Usage
  • Memory Leaks


  • I have heard of visualvm as a tool to input a heap dump from applications and analyze.
    But, how can i go ahead with a simple JAVA standalone program.
    Appreciate your inputs.


     
    Jeanne Boyarsky
    author & internet detective
    Marshal
    Posts: 34669
    366
    Eclipse IDE Java VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your program doesn't run long enough to adequately profile. You could solve this by running it a lot of times in a loop. Or by putting a sleep at the end. The sleep approach would only check for a memory leak as proper memory could get returned by then.

    You can still run jhat or visualvm or Eclipse Memory Analyzer against a standalone program. You "just" need to get the process id to take a heap dump or launch visualvm against it. THe "just" is in quotes because you need sufficient time to get the heap dump or look at a visual tool before your program ends. Which goes back to my suggestions above.
     
    Tarun Trehan
    Ranch Hand
    Posts: 51
    Android Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Jeanne,

    Will add suggested options and go through the visualvm tutorial to profile.

    Thanks Again !!!
     
    Tarun Trehan
    Ranch Hand
    Posts: 51
    Android Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Jeanne/All,

    I went through visualvm and tried to profile a simple stand alone program. I was able see increase in memory for the following code:



    As expected, memory shoots up and is visible in visualvm snapshot attached i.e. Memory_Shoots.JPG.

    However, if i remove the following comment from the code and allow the program to sleep for 1 second; the result is different:


    The memory increases but then stabilizes and it goes on. Please refer Memory_Stabilizes.JPG

    I am unable to understand this behavior. Is the sleep giving JVM a chance to remove un referenced objects ? Can you please provide your inputs.


    Memory_Shoots.JPG
    [Thumbnail for Memory_Shoots.JPG]
    Memory_Stabilizes.JPG
    [Thumbnail for Memory_Stabilizes.JPG]
     
    Tarun Trehan
    Ranch Hand
    Posts: 51
    Android Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Appreciate any help on this ??
     
    Ulf Dittmer
    Rancher
    Posts: 42968
    73
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What do you mean by "the memory stabilizes"? Memory is consistently allocated in both cases, and every so often reclaimed. Naturally, if the code sleeps most of the time, the rate of increase is a lot slower. In fact, in the second case it's so slow that what you're seeing is not the objects that your code allocates, but the objects the JVM allocates in the course of its normal operation (which is so much more that it entirely obscures your data in the graph).

    In the first case it's just a matter of time until the JVM exits with an OutOfMemoryException because its memory allocation is exhausted (because the objects your code allocates can't be reclaimed).
     
    Tarun Trehan
    Ranch Hand
    Posts: 51
    Android Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ulf,

    Thanks for your input.
    I ran the program with for much longer period of time and was able to see the increase in heap.
    Appreciate your inputs.
     
    Consider Paul's rocket mass heater.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic