Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 possible on Java7 though)

like image 204
instantsetsuna Avatar asked Dec 07 '25 12:12

instantsetsuna


1 Answers

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.

like image 159
fglez Avatar answered Dec 09 '25 10:12

fglez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!