PermGen and Metaspace

本文探讨了Java中Metaspace的概念,它是用于存储已加载类元数据的空间。从Java 8开始,PermGen被Metaspace取代,后者默认大小无限。文章详细解释了如何通过调整Metaspace的初始和最大大小来提高JVM性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址
In this post, we take a look at Metaspace, and explain how to increase the performance of your JVM with a few simple commands.

PermGen is an abbreviation for Permanent Generation and it’s a special heap space which is separate from the main Java heap where JVM keeps track of metadata of the classes which have been loaded. In Java 8, PermGen has been renamed to Metaspace - with some subtle differences. From our perspective, it is important to note that Metaspace has an unlimited default maximum size. On the contrary, PermGen from Java 7 and earlier has a default maximum size of 64 MB on 32-bit JVM and 82 MB on the 64-bit version. Of course, these are not the same as the initial sizes. Java 7 and earlier starts with something around 12-21 MB of the initial PermGen space.

JVM

Default maximum PermGen size (MB)

Default maximum Metaspace size

32-bit client JVM

64

unlimited

32-bit server JVM

64

unlimited

64-bit JVM

82

unlimited

It is worthwhile to mention that prior to Java 7, interned Strings used to be kept on the PermGen. That caused some serious problems with the infamous:

java.lang.OutOfMemoryError: PermGen space

Whenever there is a need to resize PermGen/Metaspace, JVM will do it as it does with the standard heap. Resizing those spaces requires a full GC, which is always an expensive operation. It can usually be observed during a startup when a lot of classes are being loaded. Especially if the application has dependencies on many external libraries. If there are a lot of full GCs during the startup, it’s usually because of that. If that case, increasing the initial size can boost the startup performance.

To increase PermGen, we have the following commands:

-XX:PermSize=N - sets the initial (and minimum size) of the Permanent Generation space.

-XX:MaxPermSize=N - sets the maximum size of the Permanent Generation space.

In Java 8 and onwards, we can set the initial and maximum size of Metaspace using the following commands:

-XX:MetaspaceSize=N - sets the initial (and minimum size) of the Metaspace.

-XX:MaxMetaspaceSize=N - sets the maximum size of the Metaspace.

--- lowercaseOutputLabelNames: true lowercaseOutputName: true whitelistObjectNames: ["java.lang:type=OperatingSystem"] blacklistObjectNames: [] rules: - pattern: 'java.lang<type=OperatingSystem><>(committed_virtual_memory|free_physical_memory|free_swap_space|total_physical_memory|total_swap_space)_size:' name: os_$1_bytes type: GAUGE attrNameSnakeCase: true - pattern: 'java.lang<type=OperatingSystem><>((?!process_cpu_time)\w+):' name: os_$1 type: GAUGE attrNameSnakeCase: true 解释代码后,解释# HELP jvm_info VM version info # TYPE jvm_info gauge jvm_info{runtime="Java(TM) SE Runtime Environment",vendor="Oracle Corporation",version="1.7.0_79-b15",} 1.0 # HELP os_process_cpu_load java.lang:name=null,type=OperatingSystem,attribute=ProcessCpuLoad # TYPE os_process_cpu_load gauge os_process_cpu_load 0.07142857142857142 # HELP os_open_file_descriptor_count java.lang:name=null,type=OperatingSystem,attribute=OpenFileDescriptorCount # TYPE os_open_file_descriptor_count gauge os_open_file_descriptor_count 84.0 # HELP os_max_file_descriptor_count java.lang:name=null,type=OperatingSystem,attribute=MaxFileDescriptorCount # TYPE os_max_file_descriptor_count gauge os_max_file_descriptor_count 4096.0 # HELP os_total_swap_space_bytes java.lang:name=null,type=OperatingSystem,attribute=TotalSwapSpaceSize # TYPE os_total_swap_space_bytes gauge os_total_swap_space_bytes 4.160745472E9 # HELP os_total_physical_memory_bytes java.lang:name=null,type=OperatingSystem,attribute=TotalPhysicalMemorySize # TYPE os_total_physical_memory_bytes gauge os_total_physical_memory_bytes 8.201289728E9 # HELP os_system_cpu_load java.lang:name=null,type=OperatingSystem,attribute=SystemCpuLoad # TYPE os_system_cpu_load gauge os_system_cpu_load 0.10714285714285714 # HELP os_committed_virtual_memory_bytes java.lang:name=null,type=OperatingSystem,attribute=CommittedVirtualMemorySize # TYPE os_committed_virtual_memory_bytes gauge os_committed_virtual_memory_bytes 3.57505024E9 # HELP os_system_load_average java.lang:name=null,type=OperatingSystem,attribute=SystemLoadAverage # TYPE os_system_load_average gauge os_system_load_average 0.17 # HELP os_free_physical_memory_bytes java.lang:name=null,type=OperatingSystem,attribute=FreePhysicalMemorySize # TYPE os_free_physical_memory_bytes gauge os_free_physical_memory_bytes 6.509023232E9 # HELP os_available_processors java.lang:name=null,type=OperatingSystem,attribute=AvailableProcessors # TYPE os_available_processors gauge os_available_processors 2.0 # HELP os_free_swap_space_bytes java.lang:name=null,type=OperatingSystem,attribute=FreeSwapSpaceSize # TYPE os_free_swap_space_bytes gauge os_free_swap_space_bytes 4.160745472E9 # HELP jmx_scrape_duration_seconds Time this JMX scrape took, in seconds. # TYPE jmx_scrape_duration_seconds gauge jmx_scrape_duration_seconds 0.001119413 # HELP jmx_scrape_error Non-zero if this scrape failed. # TYPE jmx_scrape_error gauge jmx_scrape_error 0.0 # HELP jmx_scrape_cached_beans Number of beans with their matching rule cached # TYPE jmx_scrape_cached_beans gauge jmx_scrape_cached_beans 0.0 # HELP jvm_buffer_pool_used_bytes Used bytes of a given JVM buffer pool. # TYPE jvm_buffer_pool_used_bytes gauge jvm_buffer_pool_used_bytes{pool="direct",} 8192.0 jvm_buffer_pool_used_bytes{pool="mapped",} 0.0 # HELP jvm_buffer_pool_capacity_bytes Bytes capacity of a given JVM buffer pool. # TYPE jvm_buffer_pool_capacity_bytes gauge jvm_buffer_pool_capacity_bytes{pool="direct",} 8192.0 jvm_buffer_pool_capacity_bytes{pool="mapped",} 0.0 # HELP jvm_buffer_pool_used_buffers Used buffers of a given JVM buffer pool. # TYPE jvm_buffer_pool_used_buffers gauge jvm_buffer_pool_used_buffers{pool="direct",} 1.0 jvm_buffer_pool_used_buffers{pool="mapped",} 0.0 # HELP jvm_memory_objects_pending_finalization The number of objects waiting in the finalizer queue. # TYPE jvm_memory_objects_pending_finalization gauge jvm_memory_objects_pending_finalization 0.0 # HELP jvm_memory_bytes_used Used bytes of a given JVM memory area. # TYPE jvm_memory_bytes_used gauge jvm_memory_bytes_used{area="heap",} 2.38314152E8 jvm_memory_bytes_used{area="nonheap",} 5.160628E7 # HELP jvm_memory_bytes_committed Committed (bytes) of a given JVM memory area. # TYPE jvm_memory_bytes_committed gauge jvm_memory_bytes_committed{area="heap",} 5.19569408E8 jvm_memory_bytes_committed{area="nonheap",} 5.2822016E7 # HELP jvm_memory_bytes_max Max (bytes) of a given JVM memory area. # TYPE jvm_memory_bytes_max gauge jvm_memory_bytes_max{area="heap",} 1.823473664E9 jvm_memory_bytes_max{area="nonheap",} 1.3631488E8 # HELP jvm_memory_bytes_init Initial bytes of a given JVM memory area. # TYPE jvm_memory_bytes_init gauge jvm_memory_bytes_init{area="heap",} 1.28145152E8 jvm_memory_bytes_init{area="nonheap",} 2.4576E7 # HELP jvm_memory_pool_bytes_used Used bytes of a given JVM memory pool. # TYPE jvm_memory_pool_bytes_used gauge jvm_memory_pool_bytes_used{pool="Code Cache",} 3370048.0 jvm_memory_pool_bytes_used{pool="PS Eden Space",} 1.78591736E8 jvm_memory_pool_bytes_used{pool="PS Survivor Space",} 2.4223408E7 jvm_memory_pool_bytes_used{pool="PS Old Gen",} 3.5499008E7 jvm_memory_pool_bytes_used{pool="PS Perm Gen",} 4.8236232E7 # HELP jvm_memory_pool_bytes_committed Committed bytes of a given JVM memory pool. # TYPE jvm_memory_pool_bytes_committed gauge jvm_memory_pool_bytes_committed{pool="Code Cache",} 4063232.0 jvm_memory_pool_bytes_committed{pool="PS Eden Space",} 4.09468928E8 jvm_memory_pool_bytes_committed{pool="PS Survivor Space",} 2.4641536E7 jvm_memory_pool_bytes_committed{pool="PS Old Gen",} 8.5458944E7 jvm_memory_pool_bytes_committed{pool="PS Perm Gen",} 4.8758784E7 # HELP jvm_memory_pool_bytes_max Max bytes of a given JVM memory pool. # TYPE jvm_memory_pool_bytes_max gauge jvm_memory_pool_bytes_max{pool="Code Cache",} 5.0331648E7 jvm_memory_pool_bytes_max{pool="PS Eden Space",} 6.31242752E8 jvm_memory_pool_bytes_max{pool="PS Survivor Space",} 2.4641536E7 jvm_memory_pool_bytes_max{pool="PS Old Gen",} 1.367343104E9 jvm_memory_pool_bytes_max{pool="PS Perm Gen",} 8.5983232E7 # HELP jvm_memory_pool_bytes_init Initial bytes of a given JVM memory pool. # TYPE jvm_memory_pool_bytes_init gauge jvm_memory_pool_bytes_init{pool="Code Cache",} 2555904.0 jvm_memory_pool_bytes_init{pool="PS Eden Space",} 3.2505856E7 jvm_memory_pool_bytes_init{pool="PS Survivor Space",} 5242880.0 jvm_memory_pool_bytes_init{pool="PS Old Gen",} 8.5458944E7 jvm_memory_pool_bytes_init{pool="PS Perm Gen",} 2.2020096E7 # HELP jvm_memory_pool_collection_used_bytes Used bytes after last collection of a given JVM memory pool. # TYPE jvm_memory_pool_collection_used_bytes gauge jvm_memory_pool_collection_used_bytes{pool="PS Eden Space",} 0.0 jvm_memory_pool_collection_used_bytes{pool="PS Survivor Space",} 2.4223408E7 jvm_memory_pool_collection_used_bytes{pool="PS Old Gen",} 0.0 jvm_memory_pool_collection_used_bytes{pool="PS Perm Gen",} 0.0 # HELP jvm_memory_pool_collection_committed_bytes Committed after last collection bytes of a given JVM memory pool. # TYPE jvm_memory_pool_collection_committed_bytes gauge jvm_memory_pool_collection_committed_bytes{pool="PS Eden Space",} 4.09468928E8 jvm_memory_pool_collection_committed_bytes{pool="PS Survivor Space",} 2.4641536E7 jvm_memory_pool_collection_committed_bytes{pool="PS Old Gen",} 0.0 jvm_memory_pool_collection_committed_bytes{pool="PS Perm Gen",} 0.0 # HELP jvm_memory_pool_collection_max_bytes Max bytes after last collection of a given JVM memory pool. # TYPE jvm_memory_pool_collection_max_bytes gauge jvm_memory_pool_collection_max_bytes{pool="PS Eden Space",} 6.31242752E8 jvm_memory_pool_collection_max_bytes{pool="PS Survivor Space",} 2.4641536E7 jvm_memory_pool_collection_max_bytes{pool="PS Old Gen",} 1.367343104E9 jvm_memory_pool_collection_max_bytes{pool="PS Perm Gen",} 8.5983232E7 # HELP jvm_memory_pool_collection_init_bytes Initial after last collection bytes of a given JVM memory pool. # TYPE jvm_memory_pool_collection_init_bytes gauge jvm_memory_pool_collection_init_bytes{pool="PS Eden Space",} 3.2505856E7 jvm_memory_pool_collection_init_bytes{pool="PS Survivor Space",} 5242880.0 jvm_memory_pool_collection_init_bytes{pool="PS Old Gen",} 8.5458944E7 jvm_memory_pool_collection_init_bytes{pool="PS Perm Gen",} 2.2020096E7 # HELP jvm_classes_currently_loaded The number of classes that are currently loaded in the JVM # TYPE jvm_classes_currently_loaded gauge jvm_classes_currently_loaded 8198.0 # HELP jvm_classes_loaded_total The total number of classes that have been loaded since the JVM has started execution # TYPE jvm_classes_loaded_total counter jvm_classes_loaded_total 8198.0 # HELP jvm_classes_unloaded_total The total number of classes that have been unloaded since the JVM has started execution # TYPE jvm_classes_unloaded_total counter jvm_classes_unloaded_total 0.0 # HELP jmx_exporter_build_info A metric with a constant '1' value labeled with the version of the JMX exporter. # TYPE jmx_exporter_build_info gauge jmx_exporter_build_info{version="0.18.0",name="jmx_prometheus_javaagent",} 1.0 # HELP jvm_threads_current Current thread count of a JVM # TYPE jvm_threads_current gauge jvm_threads_current 29.0 # HELP jvm_threads_daemon Daemon thread count of a JVM # TYPE jvm_threads_daemon gauge jvm_threads_daemon 28.0 # HELP jvm_threads_peak Peak thread count of a JVM # TYPE jvm_threads_peak gauge jvm_threads_peak 31.0 # HELP jvm_threads_started_total Started thread count of a JVM # TYPE jvm_threads_started_total counter jvm_threads_started_total 33.0 # HELP jvm_threads_deadlocked Cycles of JVM-threads that are in deadlock waiting to acquire object monitors or ownable synchronizers # TYPE jvm_threads_deadlocked gauge jvm_threads_deadlocked 0.0 # HELP jvm_threads_deadlocked_monitor Cycles of JVM-threads that are in deadlock waiting to acquire object monitors # TYPE jvm_threads_deadlocked_monitor gauge jvm_threads_deadlocked_monitor 0.0 # HELP jvm_threads_state Current count of threads by state # TYPE jvm_threads_state gauge jvm_threads_state{state="NEW",} 0.0 jvm_threads_state{state="WAITING",} 14.0 jvm_threads_state{state="TIMED_WAITING",} 7.0 jvm_threads_state{state="UNKNOWN",} 0.0 jvm_threads_state{state="TERMINATED",} 0.0 jvm_threads_state{state="RUNNABLE",} 8.0 jvm_threads_state{state="BLOCKED",} 0.0 # HELP process_cpu_seconds_total Total user and system CPU time spent in seconds. # TYPE process_cpu_seconds_total counter process_cpu_seconds_total 17.38 # HELP process_start_time_seconds Start time of the process since unix epoch in seconds. # TYPE process_start_time_seconds gauge process_start_time_seconds 1.749808021796E9 # HELP process_open_fds Number of open file descriptors. # TYPE process_open_fds gauge process_open_fds 84.0 # HELP process_max_fds Maximum number of open file descriptors. # TYPE process_max_fds gauge process_max_fds 4096.0 # HELP process_virtual_memory_bytes Virtual memory size in bytes. # TYPE process_virtual_memory_bytes gauge process_virtual_memory_bytes 3.575046144E9 # HELP process_resident_memory_bytes Resident memory size in bytes. # TYPE process_resident_memory_bytes gauge process_resident_memory_bytes 6.88590848E8 # HELP jmx_config_reload_failure_total Number of times configuration have failed to be reloaded. # TYPE jmx_config_reload_failure_total counter jmx_config_reload_failure_total 0.0 # HELP jmx_config_reload_success_total Number of times configuration have successfully been reloaded. # TYPE jmx_config_reload_success_total counter jmx_config_reload_success_total 0.0 # HELP jvm_memory_pool_allocated_bytes_total Total bytes allocated in a given JVM memory pool. Only updated after GC, not continuously. # TYPE jvm_memory_pool_allocated_bytes_total counter jvm_memory_pool_allocated_bytes_total{pool="PS Survivor Space",} 3.523552E7 jvm_memory_pool_allocated_bytes_total{pool="Code Cache",} 3718208.0 jvm_memory_pool_allocated_bytes_total{pool="PS Perm Gen",} 4.5692072E7 jvm_memory_pool_allocated_bytes_total{pool="PS Eden Space",} 1.43917056E9 jvm_memory_pool_allocated_bytes_total{pool="PS Old Gen",} 3.5499008E7 # HELP jvm_gc_collection_seconds Time spent in a given JVM garbage collector in seconds. # TYPE jvm_gc_collection_seconds summary jvm_gc_collection_seconds_count{gc="PS Scavenge",} 11.0 jvm_gc_collection_seconds_sum{gc="PS Scavenge",} 0.15 jvm_gc_collection_seconds_count{gc="PS MarkSweep",} 0.0 jvm_gc_collection_seconds_sum{gc="PS MarkSweep",} 0.0 # HELP jmx_config_reload_failure_created Number of times configuration have failed to be reloaded. # TYPE jmx_config_reload_failure_created gauge jmx_config_reload_failure_created 1.749808021823E9 # HELP jmx_config_reload_success_created Number of times configuration have successfully been reloaded. # TYPE jmx_config_reload_success_created gauge jmx_config_reload_success_created 1.749808021822E9 # HELP jvm_memory_pool_allocated_bytes_created Total bytes allocated in a given JVM memory pool. Only updated after GC, not continuously. # TYPE jvm_memory_pool_allocated_bytes_created gauge jvm_memory_pool_allocated_bytes_created{pool="PS Survivor Space",} 1.749808022447E9 jvm_memory_pool_allocated_bytes_created{pool="Code Cache",} 1.749808022448E9 jvm_memory_pool_allocated_bytes_created{pool="PS Perm Gen",} 1.749808022448E9 jvm_memory_pool_allocated_bytes_created{pool="PS Eden Space",} 1.749808022448E9 jvm_memory_pool_allocated_bytes_created{pool="PS Old Gen",} 1.749808022448E9
06-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值