This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Performance and the fly likes Profile a stand-alone Java program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Profile a stand-alone Java program " Watch "Profile a stand-alone Java program " New topic
Author

Profile a stand-alone Java program

Tarun Trehan
Ranch Hand

Joined: Mar 16, 2012
Posts: 49

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.




    Thanks,
    Tarun Trehan (http://allzhere.in)
    Jeanne Boyarsky
    internet detective
    Marshal

    Joined: May 26, 2003
    Posts: 30085
        
    149

    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.


    [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
    Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
    Tarun Trehan
    Ranch Hand

    Joined: Mar 16, 2012
    Posts: 49

    Thanks Jeanne,

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

    Thanks Again !!!
    Tarun Trehan
    Ranch Hand

    Joined: Mar 16, 2012
    Posts: 49

    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.




    [Thumbnail for Memory_Shoots.JPG]

    [Thumbnail for Memory_Stabilizes.JPG]

    Tarun Trehan
    Ranch Hand

    Joined: Mar 16, 2012
    Posts: 49

    Appreciate any help on this ??
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 41095
        
      44
    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).


    Ping & DNS - my free Android networking tools app
    Tarun Trehan
    Ranch Hand

    Joined: Mar 16, 2012
    Posts: 49

    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.
     
    wood burning stoves
     
    subject: Profile a stand-alone Java program
     
    Similar Threads
    Date Validation Against Multiple Formats
    Code review required for converting dateformat
    Date Formatting.
    convert the "Mon Oct 11 00:00:00 IST 2012" into a Date object
    Date Format Issue