java 我怎样才能看到java正在使用哪个垃圾收集器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2498942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I see which garbage collector java is using
提问by Thirler
The Java Virtual Machine supports several garbage collection strategies.
Java 虚拟机支持多种垃圾收集策略。
This articleexplains them.
这篇文章解释了它们。
Now I am wondering which (automatically selected) strategy my application is using, is there any way to let the JVM(version 1.6) print this information?
现在我想知道我的应用程序正在使用哪种(自动选择的)策略,有没有办法让 JVM(1.6 版)打印此信息?
Edit: The JVM detects if it is in client or server mode. So the question really is how can I see which has been detected?
编辑:JVM 检测它是处于客户端还是服务器模式。所以问题真的是我如何才能看到哪些已被检测到?
采纳答案by JoseK
http://java.sun.com/j2se/1.5.0/docs/guide/vm/gc-ergonomics.htmlwhich is applicable for J2SE 6 as well states that the default is the Parallel Collector.
适用于 J2SE 6 的http://java.sun.com/j2se/1.5.0/docs/guide/vm/gc-ergonomics.html也指出默认是并行收集器。
We tested this once on a JVM 1.5 by setting only
我们通过仅设置在 JVM 1.5 上测试了一次
-server -Xms3g -Xmx3g -XX:PermSize=128m -XX:LargePageSizeInBytes=4m -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps
and the output showed
并且输出显示
41359.597: [GC [PSYoungGen: 90499K->32K(377344K)] 268466K->181862K(2474496K), 0.0183138 secs] 41359.615: [Full GC [PSYoungGen: 32K->0K(377344K)] [PSOldGen: 181830K->129760K(2097152K)] 181862K->129760K(2474496K) [PSPermGen: 115335K->115335K(131072K)], 4.4590942 secs]
where PS stands for Parallel Scavenging
其中 PS 代表并行清除
回答by fglez
jmap -heap
jmap -heap
Prints a heap summary. GC algorithm used, heap configuration and generation wise heap usage are printed.
打印堆摘要。使用的 GC 算法、堆配置和生成明智的堆使用被打印出来。
http://java.sun.com/javase/6/docs/technotes/tools/share/jmap.html
http://java.sun.com/javase/6/docs/technotes/tools/share/jmap.html
回答by intropedro
Put this in the JAVA_OPTS:
把它放在 JAVA_OPTS 中:
-XX:+UseSerialGC -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps
For the UseSerialGCwe will see in the log:
对于UseSerialGC,我们将在日志中看到:
7.732: [GC 7.732: [DefNew: 419456K->47174K(471872K), 0.1321800 secs] 419456K->47174K(1520448K), 0.1322500 secs] [Times: user=0.10 sys=0.03, real=0.14 secs]
For the UseConcMarkSweepGCwe will see in the log:
对于UseConcMarkSweepGC,我们将在日志中看到:
5.630: [GC 5.630: ['ParNew: 37915K->3941K(38336K), 0.0123210 secs] 78169K->45163K(1568640K), 0.0124030 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
For the UseParallelGCwe will see in the log:
对于UseParallelGC,我们将在日志中看到:
30.250: [GC [PSYoungGen: 441062K->65524K(458752K)] 441062K->76129K(1507328K), 0.1870880 secs] [Times: user=0.33 sys=0.03, real=0.19 secs]
回答by Lord Nighton
Looks like, we have more convenient way to define the version of GCat runtime. Always use tools, my suggestion.
To define GC version we need two tools that come with JVM (placed in your jdk/bindirectory):
看起来,我们有更方便的方法来定义GC运行时的版本。总是使用工具,我的建议。要定义 GC 版本,我们需要 JVM 附带的两个工具(放置在您的jdk/bin目录中):
VisualVM- start it and try to profile some process (for example you can profile VisualVM itself). Your profile will show you a PIDof process (see the green rectangles at a screenshot).jMap- start this tool with-heap <PID>options and find a string dedicated to a Garbage Collector type (see a pink line at a screenshot)
VisualVM- 启动它并尝试分析一些进程(例如,您可以分析 VisualVM 本身)。您的个人资料将向您显示进程的PID(请参阅屏幕截图中的绿色矩形)。jMap- 使用-heap <PID>选项启动此工具并找到专用于垃圾收集器类型的字符串(请参阅屏幕截图中的粉红色线)
回答by jarnbjo
As Joachim already pointed out, the article you refer to describes the VM strategies offered by Sun's VM. The VM specification itself does not mandate specific GC algorithms and hence it won't make sense to have e.g. enumerated values for these in the API.
正如 Joachim 已经指出的那样,您参考的文章描述了 Sun 的 VM 提供的 VM 策略。VM 规范本身并没有强制要求特定的 GC 算法,因此在 API 中为这些算法提供枚举值是没有意义的。
You can however get some infos from the Management API:
但是,您可以从 Management API 获取一些信息:
List<GarbageCollectorMXBean> beans =
ManagementFactory.getGarbageCollectorMXBeans();
Iterating through these beans, you can get the name of the GC (although only as a string) and the names of the memory pools, which are managed by the different GCs.
遍历这些 bean,您可以获得 GC 的名称(尽管只是作为字符串)和由不同 GC 管理的内存池的名称。
回答by vikas sharma viky
Best way to get this is : Go to command Line and enter the following command.
最好的方法是:转到命令行并输入以下命令。
java -XX:+PrintCommandLineFlags -version
java -XX:+PrintCommandLineFlags -version
It will show you result like :
它会显示如下结果:
C:\windows\system32>java -XX:+PrintCommandLineFlags -version
-XX:InitialHeapSize=132968640 -XX:MaxHeapSize=2127498240 -XX:+PrintCommandLineFl
ags -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesInd
**ividualAllocation -XX:+UseParallelGC**
java version "1.8.0_66"
Java(TM) SE Runtime Environment (build 1.8.0_66-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)`enter code here`
回答by walkeros
You can write simple progam which connects via jmx to your java process:
您可以编写通过 jmx 连接到您的 java 进程的简单程序:
public class PrintJMX {
public static void main(String[] args) throws Exception {
String rmiHostname = "localhost";
String defaultUrl = "service:jmx:rmi:///jndi/rmi://" + rmiHostname + ":1099/jmxrmi";
JMXServiceURL jmxServiceURL = new JMXServiceURL(defaultUrl);
JMXConnector jmxConnector = JMXConnectorFactory.connect(jmxServiceURL);
MBeanServerConnection mbsc = jmxConnector.getMBeanServerConnection();
ObjectName gcName = new ObjectName(ManagementFactory.GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",*");
for (ObjectName name : mbsc.queryNames(gcName, null)) {
GarbageCollectorMXBean gc = ManagementFactory.newPlatformMXBeanProxy(mbsc,
name.getCanonicalName(),
GarbageCollectorMXBean.class);
System.out.println(gc.getName());
}
}
}


