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”
I here will post some java/j2ee questions found and the answers. Please feel free to comment also suggesting answers. This blog is dedicated for discussion and new thoughts. Please email new questions at jvquestions@gmail.com. I would post it on the blog.
1 comments:
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