生活随笔
收集整理的這篇文章主要介紹了
JDK6u25里添加的按线程统计分配内存量: JMX
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
轉(zhuǎn)載自?http://rednaxelafx.iteye.com/blog/1021619
Oracle幾天前發(fā)布的JDK 6 update 25里添加的一個(gè)新功能非常有趣,可以按照線程來跟蹤(GC堆)內(nèi)存的分配量。這個(gè)功能在VM核心、解釋器、C1編譯器、C2編譯器以及GC中都有代碼支持,并且通過JMX API暴露出來。?
不過新加的這功能不是加在java.開頭的包里,而是加在com.sun.management.ThreadMXBean這個(gè)接口上,要用的話還得cast一下。?
當(dāng)然,這么底層的功能不可能沒有代價(jià)。添加這個(gè)功能后,在GC堆上分配空間的slow-path會(huì)比以前稍微慢一些。但希望對(duì)整體性能的影響并不大吧。?
Fast-path是在TLAB上分配空間的,而TLAB的分配數(shù)據(jù)是在TLAB refill的時(shí)候才批量更新,所以這個(gè)功能對(duì)fast-path的執(zhí)行效率基本上沒影響,以稍微放寬數(shù)據(jù)精準(zhǔn)性為代價(jià)。?
相關(guān)鏈接:?
Bug ID 7003271: Hotspot should track cumulative Java heap bytes allocated on a per-thread basis?
changeset?
JMX中,該功能由ThreadMXBean上新增的幾個(gè)方法提供。詳情可見下面例子。?
ThreadMXBean.getThreadAllocatedBytes(long threadId)的用法基本上可以看成跟System.currentTimeMillis()用于計(jì)時(shí)的用法一樣,在兩點(diǎn)上記錄并且求差即可。?
不知道為什么JVMTI沒得到這個(gè)更新,或許是因?yàn)楦翵VMTI spec涉及committee stuff??
==============================================================?
直接拿一段Groovy腳本來演示吧:?
先看JDK 6 update 24的情況?
Groovysh代碼?
?
D:\sdk\groovy-1.7.2\bin>groovysh??Groovy?Shell?(1.7.2,?JVM:?1.6.0_24)??Type?'help'?or?'\h'?for?help.??-------------------------------------------------------------------------------??groovy:000>?import?java.lang.management.*??===>?[import?java.lang.management.*]??groovy:000>?tb?=?ManagementFactory.threadMXBean??===>?sun.management.ThreadImpl@8b677f??groovy:000>?tb.class.methods.name.unique().sort()??===>?[dumpAllThreads,?equals,?findDeadlockedThreads,?findMonitorDeadlockedThreads,?getAllThreadIds,?getClass,?getCurrentThreadCpuTime,?getCurrentThreadUserTime,?getDaemonThreadCount,?getPeakThreadCount,?getThreadCount,?getThreadCpuTime,?getThreadInfo,?getThreadUserTime,?getTotalStartedThreadCount,?hashCode,?isCurrentThreadCpuTimeSupported,?isObjectMonitorUsageSupported,?isSynchronizerUsageSupported,?isThreadContentionMonitoringEnabled,?isThreadContentionMonitoringSupported,?isThreadCpuTimeEnabled,?isThreadCpuTimeSupported,?notify,?notifyAll,?resetPeakThreadCount,?setThreadContentionMonitoringEnabled,?setThreadCpuTimeEnabled,?toString,?wait]??groovy:000>?tb.class.methods.findAll?{?it.name?=~?/Alloc/}.each?{?println?it?};?null??===>?null??
這個(gè)時(shí)候ThreadMXBean上還沒有跟alloc相關(guān)的方法。?
?
Groovysh代碼?
?
D:\sdk\groovy-1.7.2\bin>groovysh??Groovy?Shell?(1.7.2,?JVM:?1.6.0_25)??Type?'help'?or?'\h'?for?help.??-------------------------------------------------------------------------------??groovy:000>?import?java.lang.management.*??===>?[import?java.lang.management.*]??groovy:000>?tb?=?ManagementFactory.threadMXBean??===>?sun.management.ThreadImpl@9b1670??groovy:000>?tb.class.methods.name.unique().sort()??===>?[dumpAllThreads,?equals,?findDeadlockedThreads,?findMonitorDeadlockedThreads,?getAllThreadIds,?getClass,?getCurrentThreadCpuTime,?getCurrentThreadUserTime,?getDaemonThreadCount,?getPeakThreadCount,?getThreadAllocatedBytes,?getThreadCount,?getThreadCpuTime,?getThreadInfo,?getThreadUserTime,?getTotalStartedThreadCount,?hashCode,?isCurrentThreadCpuTimeSupported,?isObjectMonitorUsageSupported,?isSynchronizerUsageSupported,?isThreadAllocatedMemoryEnabled,?isThreadAllocatedMemorySupported,?isThreadContentionMonitoringEnabled,?isThreadContentionMonitoringSupported,?isThreadCpuTimeEnabled,?isThreadCpuTimeSupported,?notify,?notifyAll,?resetPeakThreadCount,?setThreadAllocatedMemoryEnabled,?setThreadContentionMonitoringEnabled,?setThreadCpuTimeEnabled,?toString,?wait]??groovy:000>?tb.class.methods.findAll?{?it.name?=~?/Alloc/}.each?{?println?it?};??null??public?boolean?sun.management.ThreadImpl.isThreadAllocatedMemoryEnabled()??public?boolean?sun.management.ThreadImpl.isThreadAllocatedMemorySupported()??public?long[]?sun.management.ThreadImpl.getThreadAllocatedBytes(long[])??public?long?sun.management.ThreadImpl.getThreadAllocatedBytes(long)??public?void?sun.management.ThreadImpl.setThreadAllocatedMemoryEnabled(boolean)??===>?null??groovy:000>?tb.threadAllocatedMemoryEnabled??===>?true??groovy:000>?tid?=?Thread.currentThread().id??===>?1??groovy:000>?tb.getThreadAllocatedBytes(tid)??===>?48106672??groovy:000>?tb.getThreadAllocatedBytes(tid)??===>?48751520??groovy:000>?tb.getThreadAllocatedBytes(tid)??===>?49384752??groovy:000>?tb.getThreadAllocatedBytes(tid)??===>?50086240??groovy:000>?quit??
總結(jié)
以上是生活随笔為你收集整理的JDK6u25里添加的按线程统计分配内存量: JMX的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。