Measuring memory used by a program?

I have a Java program. I want to measure the total memory used by the program, especially the peak memory. Is there a way to do it?

I have tried utilities like time (which returns 0) and top (which is not very useful) as the program does not run for long.

Can anyone suggest a way to do this?

Thanks,
Santhosh Pathical

What OS - Linux?

For Java-based programs, the best monitoring means is in the JVM itself - JMX. If you are not monitoring for too long time, you can leave JConsole open and it has a function of exporting the data to CSV file.

Refer to an earlier reply of mine for a similar question for further details:

Sorry for the late reply

Yes it it Linux.

Thanks for the help but I was looking for something for a more generic Unix/Linux utility that would help me to measure the memory usage as I also have other scripts for which I would like to see the memory usage.

Thanks,
Santhosh Pathical

The language being Java makes it a bit more complicated. It's a bit like trying to measure how much memory a BASH script is using; anything you ask except maybe BASH itself would just tell you how much memory BASH is using.

The process itself can call getrusage via JNI but the OS has to support getrusage first.If getrusage is not there then you can try opening /proc/self/status like a text file and read the values stored in there:
VmSize: 12084 kB
VmLck: 0 kB
VmRSS: 10280 kB
VmData: 9636 kB
VmStk: 188 kB
VmExe: 72 kB
VmLib: 2112 kB

Corona mentions a problem: the JVM process. You may need to get the pid of your process via a call to ps. Then access your values via pstat or the /proc filesystem or however your OS works.