How to get the amount of garbage already generated at a time from Java code?

Asked

Viewed 51 times

0

So people, I’d like to know if you have any method or even way to get the amount of Garbage generated at the current moment of the code. If you do not know but if you know any DOC that I can study to find a way, thank you.

  • Do you want to know this at runtime? If you cannot use a static code analysis tool. For example Sonarqube

  • Yes, at runtime...

  • I think that only with some profiler... Search for Visualvm it comes along with JDK

  • I use Jprofiler, but for this case I want to put the quantity in a variable :/

  • Oh right.. I think there’s no way... If you don’t really save the profiler data in some file that the application can read :P

  • Maybe it will help? http://docs.oracle.com/javase/6/docs/api/java/lang/management/GarbageCollectorMXBean.html; https://stackoverflow.com/q/466878/4438007

Show 1 more comment

1 answer

0

I’m not sure you’re gonna get exactly what you want, but maybe in one of the classes MemoryMXBean or MemoryPoolMXBean.

Instances of these classes can be obtained with the help of the class ManagementFactory:

MemoryMXBean memory = ManagementFactory.getMemoryMXBean();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.