Space Rocker
Space Rocker

Reputation: 787

How to check memory utilization by java program?

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?

Upvotes: 3

Views: 1201

Answers (4)

malaverdiere
malaverdiere

Reputation: 1537

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;

Upvotes: 1

OrangeDog
OrangeDog

Reputation: 38836

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

Upvotes: 1

Art Licis
Art Licis

Reputation: 3679

I think JConsole would be a good start.

Upvotes: 3

QuantumRob
QuantumRob

Reputation: 2924

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

Upvotes: 0

Related Questions