Find out how much RAM is used by Thread.

I would like to know how to find out how much RAM is consumed by some Threads. My program has about 15 classes, each of which works in its own theme.

So, how can I find out how much RAM is used by Thread1, Thread2, ... Thread15? Is there any way to do this?

Thanks for answers!

+4
source share
4 answers

Memory usage varies by JVM and OS.

All threads share a common heap. All of them have their own stack, which is usually 512 KB.

There are several ways to use thread memory. First check the following:

http://docs.oracle.com/javase/6/docs/jre/api/management/extension/com/sun/management/ThreadMXBean.html#getThreadAllocatedBytes%28long%29

+3
source

The short answer is that all dynamically allocated memory (heap) is shared by all threads unless you use the ThreadLocal Java class to declare variables that are local to the thread only.

In fact, the traditional Java memory model of shared data between threads (when using ThreadLocal) is what makes threads so powerful for sharing memory across threads.

As mentioned in sk4l, there is a getThreadAllocatedBytes method of the ThreadMXBean method if your JVM supports it, but keep in mind that this is usually just an approximation.

Finally, the most recent versions of Oracle JDK and OpenJDK include jconsole and JDK 6u7 and later versions include VisualVM , which you can use to join your process and view memory and thread information.

+3
source

All threads share all objects, so no one owns the object.

What you can do is use a memory profiler, for example. VisualVM, which is free with the JDK, and see how much each class uses (deep size, not small size) and this will tell you what you want to know.

+2
source

I do not think this is possible because all threads share one heap of memory. Thread is a working object, but not a data owner.

0
source

Source: https://habr.com/ru/post/1491268/


All Articles