• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Profile a stand-alone Java program

 
Ranch Hand
Posts: 51
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


     
    author & internet detective
    Posts: 41878
    909
    Eclipse IDE VI Editor Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Appreciate any help on this ??
     
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
    reply
      Bookmark Topic Watch Topic
    • New Topic