JVM Heap - Is there a way to determine how much heap a single thread consumes?
Is there a way to programatically (not using profilers etc) determine how much heap memory a single thread consumes? Or is there a way to set a limit on the memory consumed by a thread?
(I think this might be po开发者_开发问答ssible on Java7 though)
You can use ThreadMXBean.getThreadAllocatedBytes(long id):
Returns an approximation of the total amount of memory, in bytes, allocated in heap memory for the thread of the specified ID. The returned value is an approximation because some Java virtual machine implementations may use object allocation mechanisms that result in a delay between the time an object is allocated and the time its size is recorded.
精彩评论