基础知识
MemoryUsage类
有四个值(均以字节为单位):
Init:java虚拟机在启动的时候向操作系统请求的初始内存容量,java虚拟机在运行的过程中可能向操作系统请求更多的内存或将内存释放给操作系统,所以init的值是不确定的。
represents the initial amount of memory (in bytes) that the Java virtual machine requests from the operating system for memory management during startup. The Java virtual machine may request additional memory from the operating system and may also release memory to the system over time. The value ofinit
may be undefined.Used:当前已经使用的内存量。
represents the amount of memory currently used (in bytes)Committed:表示保证java虚拟机能使用的内存量,已提交的内存量可以随时间而变化(增加或减少)。Java 虚拟机可能会将内存释放给系统,committed 可以小于 init。committed 将始终大于或等于 used。
represents the amount of memory (in bytes) that is guaranteed to be available for use by the Java virtual machine. The amount of committed memory may change over time (increase or decrease). The Java virtual machine may release memory to the system andcommitted
could be less thaninit
.committed
will always be greater than or equal toused
Max:表示可以用于内存管理的最大内存量(以字节为单位)。可以不定义其值。如果定义了该值,最大内存量可能随时间而更改。已使用的内存量和已提交的内存量将始终小于或等于 max(如果定义了 max)。如果内存分配试图增加满足以下条件的已使用内存将会失败:used > committed,即使 used <= max 仍然为 true(例如,当系统的虚拟内存不足时)。
represents the maximum amount of memory (in bytes) that can be used for memory management. Its value may be undefined. The maximum amount of memory may change over time if defined. The amount of used and committed memory will always be less than or equal tomax
ifmax
is defined. A memory allocation may fail if it attempts to increase the used memory such thatused > committed
even ifused <= max
would still be true (for example, when the system is low on virtual memory).
MemoryPoolMXBean
Memory Usage
The getUsage() method provides an estimate of the current usage of a memory pool. For a garbage-collected memory pool, the amount of used memory includes the memory occupied by all objects in the pool including both reachable and unreachable objects.
In general, this method is a lightweight operation for getting an approximate memory usage. For some memory pools, for example, when objects are not packed contiguously, this method may be an expensive operation that requires some computation to determine the current memory usage. An implementation should document when this is the case.
Peak Memory Usage
返回自 Java 虚拟机启动以来或自峰值重置以来此内存池的峰值内存使用量。
The Java virtual machine maintains the peak memory usage of a memory pool since the virtual machine was started or the peak was reset. The peak memory usage is returned by the getPeakUsage() method and reset by calling the resetPeakUsage() method.
Collection Usage Threshold
返回 Java 虚拟机最近回收了此内存池中的不使用的对象之后的内存使用量。
Collection usage threshold is a manageable attribute only applicable to some garbage-collected memory pools. After a Java virtual machine has expended effort in reclaiming memory space by recycling unused objects in a memory pool at garbage collection time, some number of bytes in the memory pools that are garbaged collected will still be in use. The collection usage threshold allows a value to be set for this number of bytes such that if the threshold is exceeded, a collection usage threshold exceeded notification will be emitted by the MemoryMXBean. In addition, the collection usage threshold count will then be incremented.
The isCollectionUsageThresholdSupported() method can be used to determine if this functionality is supported.
A Java virtual machine performs collection usage threshold checking on a memory pool basis. This checking is enabled if the collection usage threshold is set to a positive value. If the collection usage threshold is set to zero, this checking is disabled on this memory pool. Default value is zero. The Java virtual machine performs the collection usage threshold checking at garbage collection time.
Some garbage-collected memory pools may choose not to support the collection usage threshold. For example, a memory pool is only managed by a continuous concurrent garbage collector. Objects can be allocated in this memory pool by some thread while the unused objects are reclaimed by the concurrent garbage collector simultaneously. Unless there is a well-defined garbage collection time which is the best appropriate time to check the memory usage, the collection usage threshold should not be supported.
The collection usage threshold is designed for monitoring the memory usage after the Java virtual machine has expended effort in reclaiming memory space. The collection usage could also be monitored by the polling and threshold notification mechanism described above for the usage threshold in a similar fashion.