File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Performance and the fly likes Monitoring memory used by a process Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Monitoring memory used by a process" Watch "Monitoring memory used by a process" New topic

Monitoring memory used by a process

curtis harrison

Joined: Sep 12, 2001
Posts: 8
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.
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
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

Joined: Sep 12, 2001
Posts: 8
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

Joined: Sep 05, 2001
Posts: 181
How about top utility in Unix?

Jun Hong<br />SCJP, SCJD, SCWCD, SCEA<br />IBM Certified Systems Expert(V4.0)
Zakaria Haque
Ranch Hand

Joined: Jan 02, 2002
Posts: 60
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
Hope it helps.

tobe bondhu nouka bherao<br />shonabo gaan aj shara raat
curtis harrison

Joined: Sep 12, 2001
Posts: 8
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...

See ya,
Bing Lu
Ranch Hand

Joined: Oct 10, 2000
Posts: 42
Roy told you that already
curtis harrison

Joined: Sep 12, 2001
Posts: 8
When I had first read it, I took totalMemory() to mean system memory, not process memory...
Thanks all
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
I agree. Here's the link:
subject: Monitoring memory used by a process
It's not a secret anymore!