Possible Duplica开发者_高级运维te:
find which type of garbage collector is running
Is there a way to tell what garbage collector is being used in a jvm by default?
This will print out a list of all of the Garbage Collectors currently loaded in your JVM.
import java.lang.management.*;
import java.util.List;
public class x {
public static void main(String args[]) {
List<GarbageCollectorMXBean> l = ManagementFactory.getGarbageCollectorMXBeans();
for(GarbageCollectorMXBean b : l) {
System.out.println(b.getName());
}
}
}
Is JConsole what you're after?
Excerpt:
Garbage collector information: Information on GC, including the garbage collector names, number of collections performed, and total time spent performing GC.
I think that the answer is that you cannot directly tell what the default garbage collector is. You can tell what garbage collectors are currently used in the current JVM, but that depends on other factors ... such as the JVM options ... so you cannot reliably infer the default GC from that.
Furthermore (not withstanding what @Lucas says), inferring (reliably) what the default is by other means would be hard:
As @Lucas points out, the default depends on whether the machine falls into the "server-class" category, and in some cases that depends on physical properties of the machine that are impossible to access portably.
The documentation of the process is patchy, and not necessarily entirely reliable.
The behaviour ("server-class" classification, default GC per class, etc) is liable to depend on the Java version. Indeed, it may even change with JVM patch releases.
IMO, a better approach would be to explicitly set the JVM options when launching the child process, and don't rely on the defaults. Alternatively, just go with whatever the default is.
精彩评论