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?
-
possible duplicate of Using Java to get OS-level system informationGreg Hewgill– Greg Hewgill2010-12-23 21:48:21 +00:00Commented Dec 23, 2010 at 21:48
Add a comment
|
4 Answers
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;