2

How to calculate what is the maximum memory needed to run a program in Java? (used by that program)

what i did: I recorded currentMemory value many times in run time, then subtract the minimum value from the maximum value.

double currentMemory = (Runtime.getRuntime().totalMemory() -  Runtime.getRuntime().freeMemory())

but the results wasn't convinced comparing with the activity monitor (Mac OS)

(1) Is there a better way to do that? (2) Should I measure the memory before calling garbage collector or after?

thank you for helping.

Reda
  • 157
  • 1
  • 10
  • See also https://stackoverflow.com/questions/27831778/how-to-really-benchmark-the-memory-usage-of-a-java-application?rq=1 – DNA Mar 02 '15 at 12:38
  • http://stackoverflow.com/questions/25552/get-os-level-system-information – Ved Mar 02 '15 at 12:39

1 Answers1

0

You could just use jconsole or jvisualvm utilities from the jdk installation folder (e.g. on windows $JAVA_HOME/bin/jconsole.exe) to get a nice Visual view of the memory consumption throughout the life of the application. They are pretty simple to use and very informative in providing realtime insights into the jvm

Kedar Parikh
  • 1,241
  • 11
  • 18