File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Performance
Bookmark "Profile a stand-alone Java program " Watch "Profile a stand-alone Java program " New topic

Profile a stand-alone Java program

Tarun Trehan
Ranch Hand

Joined: Mar 16, 2012
Posts: 51

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.

    Tarun Trehan (
    Jeanne Boyarsky
    author & internet detective

    Joined: May 26, 2003
    Posts: 33124

    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.

    [OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
    Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
    Tarun Trehan
    Ranch Hand

    Joined: Mar 16, 2012
    Posts: 51

    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: 51

    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: 51

    Appreciate any help on this ??
    Ulf Dittmer

    Joined: Mar 22, 2005
    Posts: 42965
    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

    Joined: Mar 16, 2012
    Posts: 51


    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.
    I agree. Here's the link:
    subject: Profile a stand-alone Java program
    It's not a secret anymore!