Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Monitoring memory used by a process

 
curtis harrison
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Jun Hong
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about top utility in Unix?
 
Zakaria Haque
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Bing Lu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roy told you that already
 
curtis harrison
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic