• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Monitoring memory used by a process

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good morning,
I want to monitor how much memory my java process is using at any given time. Is there a way, within a java process, to determine how much memory is being used by that process?
I could use Windows Task Manager, but we want to start several processes on several boxes and have each process monitor itself.
Is this possible? The main item of interest is how much memory my current process is using.
Thanks,
Curtis
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can call maybe Runtime.getRuntime().freeMemory() and Runtime.getruntime().totalMemory() to check the memory before and after the process.
does that help?
 
curtis harrison
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That may be skewed by other processes running on the system. Is there any way to see exactly how much of the system memory my particular process is using?
 
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about top utility in Unix?
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well in one of jobs I worked in a project developing a system monitoring software. It had a scaled down free version including monitoring agents, a logging and a reporting server, it should be perfect for your needs. I am not sure if they are giving it for free anymore. You can contact them at http://www.path-communications.com
Hope it helps.
 
curtis harrison
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok guys,
I found my own answer... Using the following code, you can get the current memory being used by you java process:
//initialize it once, it keeps up to date..
Runtime runtime = Runtime.getRuntime();

//then call this whenever you want to know the
//current memory used by your process...
System.out.println(runtime.totalMemory());

See ya,
Curtis
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roy told you that already
 
curtis harrison
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops...
THANKS ROY..
When I had first read it, I took totalMemory() to mean system memory, not process memory...
Thanks all
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case anyone was unclear ...
totalMemory() returns the amount of memory used by the JVM. However, this is rather loosely related to the amount of memory that your Java program is actively using at any instant. That is because of garbage accumulation and collection.
Your Java program might have a 64Mbyte heap, but only 1Mbyte of active objects, the rest of the heap being either free or used by dead objects. The totalMemory() method tells you about the heap size, not the amount of memory used by active objects.
For the same reason, watching the size of your Java process in Windows Task Manager or UNIX top is not very helpful, as again this relates to the heap size, not how much is in use by active objects.
 
Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic