question 14

Posted Thursday, September 03, 2009 by SacrosanctBlood in

Is it possible to know number of times the Garbage Collector has been
run, and if so, the elapsed time when it was run last time?



1 comment(s) to... “question 14”

1 comments:

SacrosanctBlood said...

Yes, It is possible to know number of times Garbage Collector has been
run, the specific Garbage Collectors being used, also when was it last
run.

We can use java.lang.managment.ManagementFactory to achieve the same.
The snippet of doing the same is below:



System.out.println(ManagementFactory.getGarbageCollectorMXBeans().get(0)
.getCollectionTime());

System.out.println(ManagementFactory.getGarbageCollectorMXBeans().get(0)
.getCollectionCount());





Obviously, instead of get(0), do a null check, iterate and bla bla bla..



The point is not about knowing how many times garbage is collected in
your home, but knowing that someone is doing that management:-) :-). And
he/she is accessible :-) :-)



There are Management beans provided by jvm that can be used for a
variety of purposes. These can be accessed using ManagementFactory
Class.

The Other beans that are present are:



1. ClassLoadingbean
2. CompilationBean
3. MemoryManagementBean
4. MemoryPoolBean
5. OperatingSystemBean
6. RuntimeBean
7. etc etc



So, if you are looking for some functionality related to any of the
above, you know where to look for them.



Post a Comment