1

Is it possible to get size of retained heap memory by a given Thread without creating memory dump programatically?

I know that it's possible to get the size of memory in runtime context like:

 Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();

but i need to calculate memory occupied by given Thread without creating memory dump.

UPDATE: A Thread allocates object while execution. At any given moment JVM knows which objects has been allocated by which Thread. So I need to extract size of memory for objects that have been allocated by given Thread.

Is it possible?

Iana Mykhailenko
  • 553
  • 5
  • 15
  • use a profiling tool – jgr208 Nov 13 '14 at 17:10
  • I need to do it programatically. Maybe with instrumenting the code. – Iana Mykhailenko Nov 13 '14 at 20:43
  • Thread A creates an object O, and hands it to thread B. How would you define which thread owns object O? – Ira Baxter Nov 13 '14 at 22:01
  • .... you might usefully collect information about how much data a thread has allocated via new(...). What will you do with the answer? – Ira Baxter Nov 13 '14 at 22:04
  • I thought about instrumentation of new (here I have ability to get current Thread ID - thread that created the object). Maybe i can also instrument finalise() methods (but how to get Thread ID which created an object??? Finalise is called by GC). Also i need to calculate memory for each threads on new and on finalise. I have concerns about overhead in such case. May be the better decision exists. – Iana Mykhailenko Nov 14 '14 at 09:38

1 Answers1

0

Here's the problem---threads do not have memory. A process has memory. A thread is just one execution stream within the process. The only memory assigned dedicated to a thread is the stack (even that is usually process managed).

There is then no heap memory retained by a thread.

user3344003
  • 20,574
  • 3
  • 26
  • 62
  • May be i didn't explain well. A Thread allocates object while execution. At any given moment JVM knows which objects has been allocated by which Thread. So I need to extract size of memory for objects that have been allocated by given Thread. It's possible to do with memory dump. But i need more efficient way. May be with instrumentation. – Iana Mykhailenko Nov 14 '14 at 09:18