Reputation: 4117
I know this question is a frequently one, however I checked some solutions from this site, but it seems like it didn't work for me.
So, I have written a small java program, and I want to know how much memory it consumes at different execution moments. I tried Runtime.getRuntime.total() and Runtime.getRuntime.free(), but I'm getting the (almost) same results each time.
I am not interested in how much memory is there available or used by the entire JVM, I want to know specifically for my java process.
Upvotes: 2
Views: 13732
Reputation: 719436
I am not interested in how much memory is there available or used by the entire JVM, I want to know specifically for my java process.
In a typical use-case, there is no real distinction between "the entire JVM" and "your Java process". At least, not from the perspective of memory usage.
The thing is, everything that the JVM does, and every bit of memory it allocates is done at the behest of the application. And every word of memory remains reachable because something in your application might use it at some point.
So really what you are asking for doesn't make a lot of sense. And naturally, the information is not available. Even if this did make sense, I don't know what you would gain by knowing what memory "belongs" to your application and what to the JVM.
Upvotes: 3
Reputation: 8254
this will get you how much heap memory your process has used.
MemoryUsage heapMemoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
heapMemoryUsage.getUsed();
you can also get all your memory pools and iterate through them to determine other memory usage
List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans()
you could also use jvisualvm to interrogate your application.
Upvotes: 13