Reprint Infoq
In a Java Virtual machine (hereinafter referred to as the JVM), a class contains its corresponding metadata, such as class hierarchy information, method data and method information (such as bytecode, stack, and variable size), running a constant pool, determined symbolic references, and virtual method tables.
In the past (when the custom ClassLoader was used infrequently), classes were almost "static" and were rarely unloaded and recycled, so classes could also be considered "permanent". In addition, because classes are part of the JVM implementation, they are not created by programs because they are also considered "non-heap" memory.
In a hotspot virtual machine prior to JDK8, these "permanent" data for classes are stored in a zone called a permanent generation. For a permanent generation of contiguous memory space, we can control the size of the permanent generation by setting the value of the-xx:maxpermsize before the JVM starts, and the 32-bit machine defaults to a 64m,64-bit machine with a permanent size of 85M. A permanent garbage collection is bound to a garbage collection in the old age, and once one of the areas is full, the two zones are garbage collected. But there is an obvious problem as we can pass? Xx:maxpermsize sets the size of the permanent generation, once the metadata of the class exceeds the set size, the program runs out of memory and a memory overflow error (OOM) occurs.
The permanent generation of resignation, Ying Yuan space
With the advent of Java8, we will never see a permanent generation again. However, this does not mean that the metadata information for the class has disappeared. This data is moved to a local memory area that is not connected to the heap, which is the meta-space we are going to refer to.
This change is necessary because it is difficult to tune the permanent generation. Metadata in a permanent generation may move as each full GC occurs. It is also difficult to set the space size for a permanent generation, because there are many factors, such as the total number of classes, the size of the constant pool, and the number of methods.
At the same time, each type of garbage collector for a hotspot virtual machine requires special processing of metadata in a permanent generation. The separation of metadata from the permanent generation not only realizes the seamless management of the meta-space, but also simplifies the full GC and optimizes the future of the concurrent isolation class meta-data.
Remove the effects of a permanent generation
Because the metadata of the class is allocated in local memory, the maximum allocated space of the meta-space is the available memory space of the system. Therefore, we will not encounter a memory overflow error when there is a permanent generation, nor will there be any such thing as leaking data to the swap area. The end user can set a maximum free space for the meta space, and if not set, the JVM automatically increases the capacity of the meta space dynamically based on the metadata size of the class.
Note : The removal of a permanent generation does not resolve the problem of a custom class loader leak. As a result, you must also monitor your memory consumption, because once a leak occurs, it can take up a lot of your local memory, and it can also cause the swap to get worse.
Meta-space memory management
The memory management of the meta-space is done by the meta-space virtual machine. Previously, for the metadata of the class we needed a different garbage collector for processing, and now we just need to execute the C + + code of the meta-space virtual machine. In Meta space, the life cycle of a class and its metadata is the same as its corresponding class loader. In other words, as long as the ClassLoader survives, the metadata of the classes it loads is also alive and therefore not recycled.
The meta-space we've been talking about is a little bit less rigorous. To be exact, the storage area of each classloader is called a meta-space, and all of the meta-spaces together are the meta-space we have been talking about. When a classloader is marked by the garbage collector as no longer surviving, its corresponding meta-space is recycled. There are no relocation and compression operations during the meta-space recovery process. However, metadata in the meta-space is scanned to determine the Java reference.
The meta-space virtual machine is responsible for the allocation of the meta-space, which is used in the form of block allocation. The size of the block varies depending on the type of ClassLoader. There is a global list of free block blocks in the Meta space virtual machine. When a class loader requires a block, it obtains and maintains a list of its own blocks from this global list of block blocks. When a class loader no longer survives, the blocks it holds will be freed and returned to the global Block list. The block that the ClassLoader holds is then divided into chunks, each of which stores meta information for a single cell. The block in the block is a linear assignment (a pointer collision allocation form). The block is assigned from the memory-mapped area. These global virtual memory-mapped areas are connected as a linked list, which is returned to the operating system once a virtual memory-mapped area is emptied.
Shows how the virtual memory map area is allocated for tuple blocks. Class Loaders 1 and 3 indicate that a reflection is used or an anonymous class loader, and they use a particular large group of blocks. The Class Loaders 2 and 4 use small or medium-sized blocks based on the number of their internal entries.
Meta-space tuning and tools
As mentioned above, the Meta space virtual machine controls the growth of the meta space. But there are times when we want to limit growth, such as by explicitly setting-xx:maxmetaspacesize on the command line. By default, the value of-xx:maxmetaspacesize is not limited, so the meta-space can even extend to the swap, but it will fail when we make local memory allocations.
For a 64-bit server-side JVM, its default –xx:metaspacesize value is 21MB. This is the initial high-water line. Once this watermark is reached, the full GC will be triggered and unloaded with unused classes (i.e. the class loader for those classes no longer survives), and the high watermark will be reset. The value of the new high watermark depends on how much space is freed after the GC. If the free space is insufficient, the high watermark rises. If you release too much space, the high watermark drops. The above high watermark adjustment occurs many times if the initialized high watermark is set too low. We can observe the full GC multiple calls through the garbage collector's logs. To avoid frequent GC, it is recommended to set –xx:metaspacesize to a relatively high value.
After multiple GC, the meta-space virtual machine automatically adjusts the high watermark to postpone the next garbage collection.
Are there two options? Xx:minmetaspacefreeratio and? Xx:maxmetaspacefreeratio, they resemble the GC's freeratio option, which is used to set the maximum and minimum values of the meta-space idle scale. We can set the corresponding values for these two options from the command line.
Here are some improved tools to get more information about meta-space.
- Jmap-clstats PID Print class loader data. (-clstats is an alternative to-permstat,-permstat the data used to print the ClassLoader before JDK8). The following example output is the class loader data for the DaCapo ' s Avrora benchmark program
$ jmap-clstats attaching to process ID 6476, please wait ... Debugger attached successfully. Server compiler detected. JVM version is 25.5-b02finding class loader instances: done.computing per loader stat. Done.please wait. Computing liveness.liveness analysis could be inaccurate ... class_loader classes bytes Parent_loader alive? Type 655 1222734 null live 0x000000074004a6c0 0 0 0x000000074004a708 dead Java/util/[ema il protected]0x000000074004a760 0 0 Null dead SUN/MISC/[EMAIL PROTECTED]0X00000007401189C8 1 1471 0x00000007400752f8 dead sun/reflect/[email protected]0x000000074004a708 316053 0x0 00000074004a760 dead Sun/misc/[email protected]0x00000007400752f8 538 773854 0x000000074004a708 dead Org/dacapo/harness/[email protected]total = 6 1310 2314112 N/a alive=1, dead=5 N/a
- JSTAT-GC lvmid is used to print meta-space information, the details are as follows
- jcmd PID gc.class_stats A new diagnostic command to connect to the running JVM and output a histogram of the detailed class metadata.
Note : Under JDK 6 build 13, need to add ? Xx:+unlockdiagnosticvmoptions can use the Jcmd command correctly.
$ jcmd help GC.class_stats9522:GC.class_statsProvide statistics about Java class meta data. Requires -XX:+UnlockDiagnosticVMOptions.Impact: High: Depends on Java heap size and content. Syntax : GC.class_stats [options] [] Arguments: columns : [optional] Comma-separated list of all the columns to show. If not specified, the following columns are shown: InstBytes,KlassBytes,CpAll,annotations,MethodCount,Bytecodes,MethodAll,ROAll,RWAll,Total (STRING, no default value)Options: (options must be specified using the or = syntax) -all : [optional] Show all columns (BOOLEAN, false) -csv : [optional] Print in CSV (comma-separated values) format for spreadsheets (BOOLEAN, false) -help : [optional] Show meaning of all the columns (BOOLEAN, false)
Tip : If you want to learn more about the fields, please visit here
Example output using the Jcmd:
$ jcmd gc.class_stats 7140:index Super instbytes klassbytes annotations cpall methodcount bytecodes methodall ROAll Rwall Total ClassName 1-1 426416 480 0 0 0 0 0 24 5 [C 2-1 290136 480 0 0 0 0 0 40 576 616 [Lavrora.arch.legacy.LegacyInstr; 3-1 269840 480 0 0 0 0 0 576 [B 4 43 137856 648 0 19248 129 4886 25288 16368 30568 46936 java.lang.Class 5 43 136968 624 0 8760 94 4570 33616 12072 32000 44072 java.lang.String 6 4 3 75872 560 0 1296 7 149 1400 880 2680 3560 Java.util.hashmap$node 7 836 57408 608 0 720 3 1480 528 2488 3016 AVrora.sim.util.MulticastFSMProbe 8 43 55488 504 0 680 1 31 440 2 1536 1816 Avrora.sim.finitestatemachine$state 9-1 53712 480 0 0 0 0 0 576 [Ljava.lang.Object; 10-1 49424 480 0 0 0 0 0 576 [I 11-1 49248 480 0 0 0 0 0 576 [Lavrora.sim.platform.Exter Nalflash$page; 12-1 24400 480 0 0 0 0 0 576 608 [Ljava.util.Hash Map$node; 394 21408 520 0 3 1216 432 2080 2512 Avrora.sim.Atmel Interpreter$ioregbehavior 14 727 19800 672 0 968 4 71 1240 664 2 472 3136 AVRORA.ARCH.LEGACY.LEGACYINSTR$MOVW ... 1299 1300 0 608 0 1 5 104 1128 sun.util.resources. Localenamesbundle 1300 1098 0 608 0 1744 10 290 1808 1176 3208 4 384 Sun.util.resources.OpenListResourceBundle 1301 1098 0 616 0 2184 12 395 2200 1480 3800 5280 sun.util.resources.ParallelListResourceBundle 2244312 794288 2024 2260976 12801 561882 3135144 1906688 4684704 6591392 total 34% 12.1% 0% 34.3% -8.5% 47.6% 28.9% 71.1% 100.0%index Super instbytes klassbytes annotations Cpall Methodcount by Tecodes methodall roall rwall Total ClassName
Problems that exist
As mentioned earlier, the meta-space virtual machine takes the form of block allocation, and the size of the chunk is determined by the class loader type. Class information is not a fixed size, so it is possible to allocate free chunks and classes that require different chunk sizes, in which case fragmentation can occur. The meta-space virtual machine currently does not support compression operations, so fragmentation is the biggest problem at the moment.
Where's the permanent generation of Java?