vrbadev
vrbadev

Reputation: 506

Find out how much RAM is used by Thread

I would like to know how to find out how much RAM is consumed by certain Threads. There are in my program about 15 classes, each is running in own Thread.

So how can I discover how much RAM is used by Thread1, Thread2, ... Thread15? Is there any method for this?

Thanks for replies!

Upvotes: 6

Views: 4936

Answers (4)

Wang Jun
Wang Jun

Reputation: 595

I don't think that is possible, cause all thread share same memory heap. Thread is a running entity, but not data owner.

Upvotes: 0

linguanerd
linguanerd

Reputation: 731

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

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

As sk4l mentioned there is a getThreadAllocatedBytes method of ThreadMXBean method if your JVM supports it, but keep in mind this is typically just an approximate value.

Lastly, most recent versions of the Oracle JDK and OpenJDK include jconsole and JDK 6u7 and later include VisualVM either of which you can use to attach to your process and see information about memory and threads.

Upvotes: 4

Peter Lawrey
Peter Lawrey

Reputation: 533492

All threads share all objects so none owns an object.

What you can do is use a memory profiler e.g. VisualVM which is free with the JDK, and have a look at how much each class is using (deep size, not shallow size) and this will tell you what you want to know.

Upvotes: 2

sk4l
sk4l

Reputation: 195

Memory usage depends on JVM version and the OS.

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

There are a couple of ways to see per thread memory usage. First, check this:

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

Upvotes: 3

Related Questions