How can I see which garbage collector java is usin

2019-01-18 11:15发布

The Java Virtual Machine supports several garbage collection strategies.

This article explains 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?

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?

7条回答
Emotional °昔
2楼-- · 2019-01-18 11:35

You can write simple progam which connects via jmx to your java process:

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());
    }

  }
}
查看更多
闹够了就滚
3楼-- · 2019-01-18 11:38

http://java.sun.com/j2se/1.5.0/docs/guide/vm/gc-ergonomics.html which is applicable for J2SE 6 as well states that the default is the Parallel Collector.

We tested this once on a JVM 1.5 by setting only

-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

查看更多
【Aperson】
4楼-- · 2019-01-18 11:39

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.

You can however get some infos from the 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.

查看更多
forever°为你锁心
5楼-- · 2019-01-18 11:41

jmap -heap

Prints a heap summary. GC algorithm used, heap configuration and generation wise heap usage are printed.

http://java.sun.com/javase/6/docs/technotes/tools/share/jmap.html

查看更多
一纸荒年 Trace。
6楼-- · 2019-01-18 11:44

Put this in the JAVA_OPTS:

-XX:+UseSerialGC -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps

For the UseSerialGC we will see in the log:

 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 UseConcMarkSweepGC we will see in the log:

 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 UseParallelGC we will see in the log:

30.250: [GC [PSYoungGen: 441062K->65524K(458752K)] 441062K->76129K(1507328K), 0.1870880 secs] [Times: user=0.33 sys=0.03, real=0.19 secs]
查看更多
Viruses.
7楼-- · 2019-01-18 11:47

Best way to get this is : Go to command Line and enter the following command.

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`
查看更多
登录 后发表回答