Java EE 8 High Performance
上QQ阅读APP看书,第一时间看更新

Memory

The GC.class_histogram command allows you to get a heap histogram. We will deal with this in the coming sections. But just to sum up very quickly, the heap is where most of your Java objects will go. Therefore, it is important to see how it is used.

If we execute the GC.class_histogram command in our process, the output will look as follows:

$ jcmd 7577 GC.class_histogram
7577:

num #instances #bytes class name
----------------------------------------------
1: 192795 16202648 [C
2: 10490 4667040 [B
3: 191582 4597968 java.lang.String
4: 38779 3412552 java.lang.reflect.Method
5: 20107 2243296 java.lang.Class
6: 70045 2241440 java.util.HashMap$Node
7: 24429 2078312 [Ljava.util.HashMap$Node;
8: 47188 1887520 java.util.LinkedHashMap$Entry
9: 28134 1745104 [Ljava.lang.Object;
38: 2175 121800 com.sun.tools.javac.file.ZipFileIndex$DirectoryEntry
39: 1890 120960 com.mysql.jdbc.ConnectionPropertiesImpl$BooleanConnectionProperty
1739: 6 192 java.util.regex.Pattern$3
2357: 1 96 com.sun.crypto.provider.SunJCE
2478: 4 96 org.glassfish.jersey.server.AsyncContext$State
2548: 1 88 org.glassfish.ejb.startup.EjbDeployer
2558: 2 80 [Lcom.mysql.jdbc.StringUtils$SearchMode;
2649: 2 80 org.glassfish.kernel.embedded.EmbeddedDomainPersistence
2650: 2 80 org.glassfish.persistence.jpa.PersistenceUnitInfoImpl
2652: 1 80 org.hibernate.validator.internal.engine.ConfigurationImpl
2655: 5 80 org.jboss.weld.manager.BeanManagerImpl
2678: 1 72 [Lorg.glassfish.jersey.uri.UriComponent$Type;
2679: 2 72 [Lsun.security.jca.ProviderConfig;
2680: 1 72 com.github.rmannibucau.quote.manager.model.Quote
2689: 3 72 com.sun.enterprise.container.common.impl.ComponentEnvManagerImpl$FactoryForEntityManagerWrapper
2770: 3 72 org.eclipse.persistence.jpa.jpql.parser.TableExpressionFactory
6925: 1 16 sun.reflect.ReflectionFactory
Total 1241387 61027800

Here again, it is a partial output (truncated in multiple places) since it is too verbose for this book. If we find most of the environments we know, it is important to notice the following things:

  • com.mysql for the JDBC driver our application uses
  • com.github.rmannibucau for our application (the quote entity in particular)
  • com.sun.enterprise for the GlassFish server
  • org.jboss.weld for the CDI container of GlassFish
  • org.hibernate.validator for the GlassFish bean validation implementation
  • sun, com.sun, java, and so on for the JVM

Now, an important thing is to be able to interpret these figures. The first column is not very important but the next two are. As written in the table header, they represent the number of instances and their size in bytes.

If you run several concurrent requests on your server and filter the output for your quote entity, you can see the following:

 138:           591          42552  com.github.rmannibucau.quote.manager.model.Quote

This line means that the heap currently has 591 instances of Quote and it takes 42,552 bytes.

This means that it is a statistic you can check in real time while the server is running. But as it is written in the command help, it impacts the server (slows it down), so you need to use it for tuning purposes only.

The last interesting figure of the GC.class_histogram command is the total size of the heap, which is the last number printed. In our previous output, it was 61,027,800 bytes (about 61 MB).