読者です 読者をやめる 読者になる 読者になる

HHeLiBeXの日記 正道編

日々の記憶の記録とメモ‥

java.lang.management パッケージ - MemoryPoolMXBean インタフェース

Java Java 1.4 to 5.0

ManagementFactory クラスを介して取得できるMXBeanの一つが実装するインタフェース。

java.lang.managementパッケージに属するMXBeanのうち、詳細な説明が書いてある2つ目。(1つ目は「java.lang.management パッケージ - MemoryMXBean インタフェース - HHeLiBeXの日記 正道編」参照)
で、同様に超訳

モリタイプ(Memory Type)

Java VMはheapとnon-heap memoryを持つ。Java VMは1つ以上のメモリプールを持ち、各メモリプールはheapまたはnon-heapのいずれかを表す。

メモリ使用量監視(Memory Usage Monitoring)

メモリプールは次の属性を持つ。

  1. メモリ使用量
    • 現在の使用量のおよその値
    • getUsage()メソッド
  2. ピークメモリ使用量
  3. 使用量の閾値
    • getUsageThresholdCount()メソッド
    • setUsageThreshold()メソッド
    • isUsageThresholdSupported()メソッド
    • システムのメモリ使用量はポーリングや閾値通知によって監視できる
      • ポーリング(Polling)
      • 使用量閾値通知(Usage Threshold Notifications)
  4. コレクション使用量の閾値
    • getCollectionUsageThresholdCount()メソッド
    • setCollectionUsageThreshold()メソッド
    • isCollectionUsageThresholdSupported()メソッド
    • ガベージコレクトされた(garbage-collected)メモリプールのみでサポートされる

メモリ使用量の監視については後で。

    • -

実際に実行してみる。
今回用いたのは、次のJava VM

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryUsage;
import java.util.List;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.printf("%-20s = %s\n", "java.version", System.getProperty("java.version"));
        System.out.printf("%-20s = %s\n", "java.vendor", System.getProperty("java.vendor"));
        System.out.printf("%-20s = %s\n", "java.runtime.version", System.getProperty("java.runtime.version"));
        System.out.printf("%-20s = %s\n", "os.name", System.getProperty("os.name"));

        System.out.println("========");

        List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
        System.out.printf("%-20s: %s\n", "# of MXBeans", memoryPoolMXBeans.size());
        {
            int i = 0;
            for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) {
                System.out.printf("[%2d]\n", i);
                System.out.printf("    %-24s: %s\n", "Name", memoryPoolMXBean.getName());
                System.out.printf("    %-24s:\n", "MemoryManagerNames");
                {
                    String[] memoryManagerNames = memoryPoolMXBean.getMemoryManagerNames();
                    for (int j = 0; j < memoryManagerNames.length; ++j) {
                        System.out.printf("        [%2d]: %s\n", j, memoryManagerNames[j]);
                    }
                }
                System.out.printf("    %-24s: %s\n", "Type", memoryPoolMXBean.getType());
                System.out.printf("    %-24s: %s\n", "isValid", memoryPoolMXBean.isValid());
                
                System.out.printf("    %-24s:\n", "Usage");
                if (memoryPoolMXBean.isValid()) {
                    MemoryUsage usage = memoryPoolMXBean.getUsage();
                    System.out.printf("        %-24s: %20d\n", "Committed", usage.getCommitted());
                    System.out.printf("        %-24s: %20d\n", "Init", usage.getInit());
                    System.out.printf("        %-24s: %20d\n", "Max", usage.getMax());
                    System.out.printf("        %-24s: %20d\n", "Used", usage.getUsed());
                }
                
                System.out.printf("    %-24s:\n", "PeakUsage");
                if (memoryPoolMXBean.isValid()) {
                    MemoryUsage peakUsage = memoryPoolMXBean.getPeakUsage();
                    System.out.printf("        %-24s: %20d\n", "Committed", peakUsage.getCommitted());
                    System.out.printf("        %-24s: %20d\n", "Init", peakUsage.getInit());
                    System.out.printf("        %-24s: %20d\n", "Max", peakUsage.getMax());
                    System.out.printf("        %-24s: %20d\n", "Used", peakUsage.getUsed());
                }
                
                System.out.printf("    %-24s: %s\n", "isUsageThresholdSupported", memoryPoolMXBean.isUsageThresholdSupported());
                if (memoryPoolMXBean.isUsageThresholdSupported()) {
                    System.out.printf("    %-24s: %s\n", "UsageThreshold", memoryPoolMXBean.getUsageThreshold());
                    System.out.printf("    %-24s: %s\n", "UsageThresholdCount", memoryPoolMXBean.getUsageThresholdCount());
                    System.out.printf("    %-24s: %s\n", "isUsageThresholdExceeded", memoryPoolMXBean.isUsageThresholdExceeded());
                }
                
                System.out.printf("    %-24s:\n", "CollectionUsage");
                {
                    MemoryUsage collectionUsage = memoryPoolMXBean.getCollectionUsage();
                    if (collectionUsage != null) {
                        System.out.printf("        %-24s: %20d\n", "Committed", collectionUsage.getCommitted());
                        System.out.printf("        %-24s: %20d\n", "Init", collectionUsage.getInit());
                        System.out.printf("        %-24s: %20d\n", "Max", collectionUsage.getMax());
                        System.out.printf("        %-24s: %20d\n", "Used", collectionUsage.getUsed());
                    }
                }
                System.out.printf("    %-24s: %s\n", "isCollectionUsageThresholdSupported", memoryPoolMXBean.isCollectionUsageThresholdSupported());
                if (memoryPoolMXBean.isCollectionUsageThresholdSupported()) {
                    System.out.printf("    %-24s: %s\n", "CollectionUsageThreshold", memoryPoolMXBean.getCollectionUsageThreshold());
                    System.out.printf("    %-24s: %s\n", "CollectionUsageThresholdCount", memoryPoolMXBean.getCollectionUsageThresholdCount());
                    System.out.printf("    %-24s: %s\n", "isCollectionUsageThresholdExceeded", memoryPoolMXBean.isCollectionUsageThresholdExceeded());
                }

                ++i;
            }
        }
    }

}

resetPeakUsage()、setCollectionUsageThreshold()、setUsageThreshold()は省略(←なんか面倒になったらしい‥)
Sun Java 2 SDK 1.5.0_22

java.version         = 1.5.0_22
java.vendor          = Sun Microsystems Inc.
java.runtime.version = 1.5.0_22-b03
os.name              = Windows Vista
========
# of MXBeans        : 7
[ 0]
    Name                                : Code Cache
    MemoryManagerNames                  :
        [ 0]: CodeCacheManager
    Type                                : Non-heap memory
    isValid                             : true
    Usage                               :
        Committed                           :               491520
        Init                                :               196608
        Max                                 :             33554432
        Used                                :               482304
    PeakUsage                           :
        Committed                           :               491520
        Init                                :               196608
        Max                                 :             33554432
        Used                                :               483712
    isUsageThresholdSupported           : true
    UsageThreshold                      :                    0
    UsageThresholdCount                 :                    0
    isUsageThresholdExceeded            : true
    CollectionUsage                     :
    isCollectionUsageThresholdSupported : false
[ 1]
    Name                                : Eden Space
    MemoryManagerNames                  :
        [ 0]: MarkSweepCompact
        [ 1]: Copy
    Type                                : Heap memory
    isValid                             : true
    Usage                               :
        Committed                           :               524288
        Init                                :               524288
        Max                                 :              4194304
        Used                                :               175416
    PeakUsage                           :
        Committed                           :               524288
        Init                                :               524288
        Max                                 :              4194304
        Used                                :               489088
    isUsageThresholdSupported           : false
    CollectionUsage                     :
        Committed                           :               524288
        Init                                :               524288
        Max                                 :              4194304
        Used                                :                    0
    isCollectionUsageThresholdSupported : true
    CollectionUsageThreshold            :                    0
    CollectionUsageThresholdCount       :                    0
    isCollectionUsageThresholdExceeded  : true
[ 2]
    Name                                : Survivor Space
    MemoryManagerNames                  :
        [ 0]: MarkSweepCompact
        [ 1]: Copy
    Type                                : Heap memory
    isValid                             : true
    Usage                               :
        Committed                           :                65536
        Init                                :                65536
        Max                                 :               458752
        Used                                :                65536
    PeakUsage                           :
        Committed                           :                65536
        Init                                :                65536
        Max                                 :               458752
        Used                                :                65536
    isUsageThresholdSupported           : false
    CollectionUsage                     :
        Committed                           :                65536
        Init                                :                65536
        Max                                 :               458752
        Used                                :                65536
    isCollectionUsageThresholdSupported : true
    CollectionUsageThreshold            :                    0
    CollectionUsageThresholdCount       :                    0
    isCollectionUsageThresholdExceeded  : true
[ 3]
    Name                                : Tenured Gen
    MemoryManagerNames                  :
        [ 0]: MarkSweepCompact
    Type                                : Heap memory
    isValid                             : true
    Usage                               :
        Committed                           :              1441792
        Init                                :              1441792
        Max                                 :             61997056
        Used                                :               181088
    PeakUsage                           :
        Committed                           :              1441792
        Init                                :              1441792
        Max                                 :             61997056
        Used                                :               181088
    isUsageThresholdSupported           : true
    UsageThreshold                      :                    0
    UsageThresholdCount                 :                    0
    isUsageThresholdExceeded            : true
    CollectionUsage                     :
        Committed                           :                    0
        Init                                :              1441792
        Max                                 :             61997056
        Used                                :                    0
    isCollectionUsageThresholdSupported : true
    CollectionUsageThreshold            :                    0
    CollectionUsageThresholdCount       :                    0
    isCollectionUsageThresholdExceeded  : true
[ 4]
    Name                                : Perm Gen
    MemoryManagerNames                  :
        [ 0]: MarkSweepCompact
    Type                                : Non-heap memory
    isValid                             : true
    Usage                               :
        Committed                           :              8388608
        Init                                :              8388608
        Max                                 :             67108864
        Used                                :               596536
    PeakUsage                           :
        Committed                           :              8388608
        Init                                :              8388608
        Max                                 :             67108864
        Used                                :               596536
    isUsageThresholdSupported           : true
    UsageThreshold                      :                    0
    UsageThresholdCount                 :                    0
    isUsageThresholdExceeded            : true
    CollectionUsage                     :
        Committed                           :                    0
        Init                                :              8388608
        Max                                 :             67108864
        Used                                :                    0
    isCollectionUsageThresholdSupported : true
    CollectionUsageThreshold            :                    0
    CollectionUsageThresholdCount       :                    0
    isCollectionUsageThresholdExceeded  : true
[ 5]
    Name                                : Perm Gen [shared-ro]
    MemoryManagerNames                  :
        [ 0]: MarkSweepCompact
    Type                                : Non-heap memory
    isValid                             : true
    Usage                               :
        Committed                           :              8388608
        Init                                :              8388608
        Max                                 :              8388608
        Used                                :              5652200
    PeakUsage                           :
        Committed                           :              8388608
        Init                                :              8388608
        Max                                 :              8388608
        Used                                :              5652200
    isUsageThresholdSupported           : true
    UsageThreshold                      :                    0
    UsageThresholdCount                 :                    0
    isUsageThresholdExceeded            : true
    CollectionUsage                     :
        Committed                           :                    0
        Init                                :              8388608
        Max                                 :              8388608
        Used                                :                    0
    isCollectionUsageThresholdSupported : true
    CollectionUsageThreshold            :                    0
    CollectionUsageThresholdCount       :                    0
    isCollectionUsageThresholdExceeded  : true
[ 6]
    Name                                : Perm Gen [shared-rw]
    MemoryManagerNames                  :
        [ 0]: MarkSweepCompact
    Type                                : Non-heap memory
    isValid                             : true
    Usage                               :
        Committed                           :             12582912
        Init                                :             12582912
        Max                                 :             12582912
        Used                                :              5966840
    PeakUsage                           :
        Committed                           :             12582912
        Init                                :             12582912
        Max                                 :             12582912
        Used                                :              5966840
    isUsageThresholdSupported           : true
    UsageThreshold                      :                    0
    UsageThresholdCount                 :                    0
    isUsageThresholdExceeded            : true
    CollectionUsage                     :
        Committed                           :                    0
        Init                                :             12582912
        Max                                 :             12582912
        Used                                :                    0
    isCollectionUsageThresholdSupported : true
    CollectionUsageThreshold            :                    0
    CollectionUsageThresholdCount       :                    0
    isCollectionUsageThresholdExceeded  : true

IBM Java 2 SDK 1.5.0 SR10

java.version         = 1.5.0
java.vendor          = IBM Corporation
java.runtime.version = pwi32dev-20090707 (SR10 )
os.name              = Windows Vista
========
# of MXBeans        : 5
[ 0]
    Name                                : class storage
    MemoryManagerNames                  :
        [ 0]: J9 non-heap manager
    Type                                : Non-heap memory
    isValid                             : true
    Usage                               :
        Committed                           :              2137860
        Init                                :                    0
        Max                                 :                   -1
        Used                                :              1937200
    PeakUsage                           :
        Committed                           :              2137860
        Init                                :                    0
        Max                                 :                   -1
        Used                                :              1979632
    isUsageThresholdSupported           : false
    CollectionUsage                     :
    isCollectionUsageThresholdSupported : false
[ 1]
    Name                                : JIT code cache
    MemoryManagerNames                  :
        [ 0]: J9 non-heap manager
    Type                                : Non-heap memory
    isValid                             : true
    Usage                               :
        Committed                           :               524288
        Init                                :                    0
        Max                                 :                   -1
        Used                                :               524288
    PeakUsage                           :
        Committed                           :               524288
        Init                                :                    0
        Max                                 :                   -1
        Used                                :               524288
    isUsageThresholdSupported           : false
    CollectionUsage                     :
    isCollectionUsageThresholdSupported : false
[ 2]
    Name                                : JIT data cache
    MemoryManagerNames                  :
        [ 0]: J9 non-heap manager
    Type                                : Non-heap memory
    isValid                             : true
    Usage                               :
        Committed                           :               524288
        Init                                :                    0
        Max                                 :                   -1
        Used                                :                17452
    PeakUsage                           :
        Committed                           :               524288
        Init                                :                    0
        Max                                 :                   -1
        Used                                :                18068
    isUsageThresholdSupported           : false
    CollectionUsage                     :
    isCollectionUsageThresholdSupported : false
[ 3]
    Name                                : miscellaneous non-heap storage
    MemoryManagerNames                  :
        [ 0]: J9 non-heap manager
    Type                                : Non-heap memory
    isValid                             : true
    Usage                               :
        Committed                           :               524288
        Init                                :                    0
        Max                                 :                   -1
        Used                                :               371228
    PeakUsage                           :
        Committed                           :               589824
        Init                                :                    0
        Max                                 :                   -1
        Used                                :               443132
    isUsageThresholdSupported           : false
    CollectionUsage                     :
    isCollectionUsageThresholdSupported : false
[ 4]
    Name                                : Java heap
    MemoryManagerNames                  :
        [ 0]: J9 GC
    Type                                : Heap memory
    isValid                             : true
    Usage                               :
        Committed                           :              4194304
        Init                                :              4194304
        Max                                 :           1068138496
        Used                                :              1146336
    PeakUsage                           :
        Committed                           :              4194304
        Init                                :              4194304
        Max                                 :           1068138496
        Used                                :              1146336
    isUsageThresholdSupported           : true
    UsageThreshold                      :                    0
    UsageThresholdCount                 :                    0
    isUsageThresholdExceeded            : true
    CollectionUsage                     :
        Committed                           :                    0
        Init                                :              4194304
        Max                                 :           1068138496
        Used                                :                    0
    isCollectionUsageThresholdSupported : true
    CollectionUsageThreshold            :                    0
    CollectionUsageThresholdCount       :                    0
    isCollectionUsageThresholdExceeded  : false