java.lang.management パッケージ - MemoryMXBean インタフェース
ManagementFactory クラスを介して取得できるMXBeanの一つが実装するインタフェース。
- java.lang.management パッケージ - ManagementFactory クラス - HHeLiBeXの日記 正道編
- MemoryMXBean (Java 2 Platform SE 5.0)
java.lang.managementパッケージに属するMXBeanの9つのインタフェースのうち、3つだけ説明が詳細に書いてあるのだが、これはそのうちの1つ。先にGarbageCollectorMXBeanやMemoryManagerMXBeanについて書いたが、こっちを先に読めばよかったのかも‥
で、整理するために超訳(何)。
メモリ(Memory)
メモリプールとメモリマネージャ(Memory Pools and Memory Managers)
というのがあると。クラス図にするとこんな感じ。
MemoryTypeは、先のHeapとNon-Heap Memory。
メモリ使用量の監視(Memory Usage Monitoring)
メモリ使用量とは、たとえば「アプリケーションのメモリ使用量」、「メモリ管理システムのワークロード」、「潜在的メモリリーク」。
メモリ使用量は3通りの方法で監視される。「ポーリング」、「使用量閾値通知」、「コレクション使用量閾値通知(??)」。
通知(Notifications)
はまた後で。
-
- -
で、また例によって実際に実行してみる。
今回用いたのは、次のJava VM。
import java.lang.management.ManagementFactory; import java.lang.management.MemoryMXBean; import java.lang.management.MemoryUsage; import java.util.ArrayList; 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("========"); printMemoryMXBeanInfo(); { List<String> list = new ArrayList<String>(); for (int i = 0; i < 100000; ++i) { list.add(String.valueOf(i)); } } System.out.println("### start invoke System.gc()"); System.gc(); System.out.println("### end invoke System.gc()"); printMemoryMXBeanInfo(); } private static void printMemoryMXBeanInfo() { MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); System.out.printf("%-30s: %s\n", "isVerbose", memoryMXBean.isVerbose()); System.out.printf("%-30s:\n", "HeapMemoryUsage"); { MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage(); System.out.printf(" %-30s: %20d\n", "Committed", heapMemoryUsage.getCommitted()); System.out.printf(" %-30s: %20d\n", "Init", heapMemoryUsage.getInit()); System.out.printf(" %-30s: %20d\n", "Max", heapMemoryUsage.getMax()); System.out.printf(" %-30s: %20d\n", "Used", heapMemoryUsage.getUsed()); } System.out.printf("%-30s:\n", "NonHeapMemoryUsage"); { MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage(); System.out.printf(" %-30s: %20d\n", "Committed", nonHeapMemoryUsage.getCommitted()); System.out.printf(" %-30s: %20d\n", "Init", nonHeapMemoryUsage.getInit()); System.out.printf(" %-30s: %20d\n", "Max", nonHeapMemoryUsage.getMax()); System.out.printf(" %-30s: %20d\n", "Used", nonHeapMemoryUsage.getUsed()); } System.out.printf("%-30s: %10d\n", "ObjectPendingFinalizationCount", memoryMXBean.getObjectPendingFinalizationCount()); memoryMXBean.setVerbose(true); } }
java.version = 1.5.0_22 java.vendor = Sun Microsystems Inc. java.runtime.version = 1.5.0_22-b03 os.name = Windows Vista ======== isVerbose : false HeapMemoryUsage : Committed : 2031616 Init : 0 Max : 66650112 Used : 353696 NonHeapMemoryUsage : Committed : 29851648 Init : 29556736 Max : 121634816 Used : 12665432 ObjectPendingFinalizationCount: 0 [GC 752K->524K(1984K), 0.0038556 secs] [GC 1036K->960K(1984K), 0.0035222 secs] [GC 1472K->1471K(1984K), 0.0043514 secs] [Full GC 1471K->1471K(1984K), 0.0246191 secs] [GC 1920K->1919K(2924K), 0.0069852 secs] [GC 2370K->2367K(2924K), 0.0042217 secs] [GC 2879K->2878K(3412K), 0.0038538 secs] [Full GC 2878K->2623K(3412K), 0.0330316 secs] [GC 3135K->3133K(4952K), 0.0040114 secs] [GC 3645K->3645K(4952K), 0.0034106 secs] [GC 4157K->4155K(4952K), 0.0048144 secs] [GC 4667K->4665K(5208K), 0.0047651 secs] [Full GC 4665K->4435K(5208K), 0.0437698 secs] [GC 5464K->5463K(7972K), 0.0050437 secs] ### start invoke System.gc() [Full GC 5830K->5389K(7972K), 0.0586163 secs] ### end invoke System.gc() isVerbose : true HeapMemoryUsage : Committed : 9920512 Init : 0 Max : 66650112 Used : 5531928 NonHeapMemoryUsage : Committed : 29917184 Init : 29556736 Max : 121634816 Used : 12725744 ObjectPendingFinalizationCount: 0
java.version = 1.5.0 java.vendor = IBM Corporation java.runtime.version = pwi32dev-20090707 (SR10 ) os.name = Windows Vista ======== isVerbose : false HeapMemoryUsage : Committed : 4194304 Init : 4194304 Max : 1068138496 Used : 1008024 NonHeapMemoryUsage : Committed : 3578436 Init : 0 Max : -1 Used : 2744300 ObjectPendingFinalizationCount: 0 ### start invoke System.gc() ### end invoke System.gc() isVerbose : true HeapMemoryUsage : Committed : 10738688 Init : 4194304 Max : 1068138496 Used : 7523008 NonHeapMemoryUsage : Committed : 3578436 Init : 0 Max : -1 Used : 2726820 ObjectPendingFinalizationCount: 0
以上が標準出力への出力。verboseをtrueにした場合、SunのJava VMは、GCの実行状況が標準出力に出るが、IBMのJava VMの場合は標準エラー出力に次のようなものが出力される。
<?xml version="1.0" ?> <verbosegc version="200906_09"> <af type="tenured" id="1" timestamp="Nov 12 03:54:54 2009" intervalms="0.000"> <minimum requested_bytes="235368" /> <time exclusiveaccessms="0.022" /> <tenured freebytes="209408" totalbytes="4194304" percent="4" > <soa freebytes="0" totalbytes="3984896" percent="0" /> <loa freebytes="209408" totalbytes="209408" percent="100" /> </tenured> <gc type="global" id="1" totalid="1" intervalms="0.000"> <expansion type="tenured" amount="1048576" newsize="5242880" timetaken="0.002" reason="insufficient free space following gc" /> <refs_cleared soft="0" threshold="32" weak="17" phantom="0" /> <finalization objectsqueued="1" /> <timesms mark="11.102" sweep="0.212" compact="0.000" total="11.412" /> <tenured freebytes="1900272" totalbytes="5242880" percent="36" > <soa freebytes="1585904" totalbytes="4928512" percent="32" /> <loa freebytes="314368" totalbytes="314368" percent="100" /> </tenured> </gc> <tenured freebytes="1664904" totalbytes="5242880" percent="31" > <soa freebytes="1350536" totalbytes="4928512" percent="27" /> <loa freebytes="314368" totalbytes="314368" percent="100" /> </tenured> <time totalms="11.954" /> </af> (中略) <sys id="1" timestamp="Nov 12 03:54:54 2009" intervalms="0.000"> <time exclusiveaccessms="0.019" /> <tenured freebytes="1230080" totalbytes="9215488" percent="13" > <soa freebytes="1230080" totalbytes="9215488" percent="13" /> <loa freebytes="0" totalbytes="0" percent="0" /> </tenured> <gc type="global" id="4" totalid="4" intervalms="5.223"> <compaction movecount="207792" movebytes="7511440" reason="compact on aggressive collection" /> <classloadersunloaded count="0" timetakenms="0.088" /> <expansion type="tenured" amount="1523200" newsize="10738688" timetaken="0.003" reason="insufficient free space following gc" /> <refs_cleared soft="0" threshold="32" weak="0" phantom="0" /> <finalization objectsqueued="0" /> <timesms mark="26.671" sweep="0.189" compact="77.217" total="104.300" /> <tenured freebytes="3221984" totalbytes="10738688" percent="30" > <soa freebytes="3221984" totalbytes="10738688" percent="30" /> <loa freebytes="0" totalbytes="0" percent="0" /> </tenured> </gc> <tenured freebytes="3221984" totalbytes="10738688" percent="30" > <soa freebytes="3221984" totalbytes="10738688" percent="30" /> <loa freebytes="0" totalbytes="0" percent="0" /> </tenured> <time totalms="104.337" /> </sys> </verbosegc>