3

Is there any native java code to check the memory utilized by the program, or the only way possible is to check memory utilized by the JVM itlself?
can this be done purely in java or we need external process to fulfill this job?

Space Rocker
  • 787
  • 3
  • 11
  • 25
  • possible duplicate of [Using Java to get OS-level system information](http://stackoverflow.com/questions/25552/using-java-to-get-os-level-system-information) – Greg Hewgill Dec 23 '10 at 21:48

4 Answers4

4

I think JConsole would be a good start.

Art Licis
  • 3,619
  • 1
  • 29
  • 49
1

You could use java.lang.management.MemoryUsage, or the external tool, VisualVM (shipped with JDK).

OrangeDog
  • 36,653
  • 12
  • 122
  • 207
1

I personally find java.lang.management.MemoryMXBean to be simpler to use than MemoryUsage. You can get the memory used by the program as follows:

MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
long heapMemUsed = memoryBean.getHeapMemoryUsage().getUsed();
long otherMemUsed = memoryBean.getNonHeapMemoryUsage().getUsed();
long totalMemoryUsed = heapMemUsed + otherMemUsed;
malaverdiere
  • 1,527
  • 4
  • 19
  • 36
0

Is there something that jConsole can't provide? That shows Heap, Classes, Threads, CPU and a heck of a lot more.

Mech
  • 2,904
  • 3
  • 24
  • 29