19

Is there a way to find out how much memory my java thread is taking in the VM?

For example, using stack trace dump, or some other means.

Thanks

Vikas Jain
  • 201
  • 1
  • 2
  • 5
  • You should try out the [VisualVM](https://visualvm.dev.java.net/) tool. – Vinay Pai Jan 06 '11 at 20:00
  • 7
    Sun must be listening - you can use the new interfaces on the platform-specific `ThreadMXBean` to get per-thread memory usage: http://download.oracle.com/javase/6/docs/jre/api/management/extension/com/sun/management/ThreadMXBean.html#getThreadAllocatedBytes%28long%29 – BeeOnRope Jul 13 '11 at 22:29
  • @BeeOnRope that's awesome answer! You should post it as an answer and I'll vote it up. – Hendy Irawan May 20 '14 at 16:19
  • 1
    @HendyIrawan, in fact it was originally, but it seems like it was deleted by a moderator and converted to a comment. Not sure why... – BeeOnRope May 27 '14 at 05:54

1 Answers1

24

Java threads use the heap as shared memory. Individual threads have their stack (the size of which you can set via the -Xss command line option, default is 512KB), but all other memory (the heap) does not belong to specific threads, and asking how much of it one specific thread uses simply does not make sense.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • But in my system, I have all the threads doing similar kind of functioning. So just to get an approximate picture, I am thinking of getting the size of heap and dividing it by the number of threads to get the approximate size each thread is taking.. would that be a fare assumption? – Vikas Jain Jan 06 '11 at 21:09
  • @Vikas: it still does not make sense. What do you want to use that number *for*? – Michael Borgwardt Jan 06 '11 at 21:19
  • I have to make a system to dynamically increase or decrease the number of threads based on the some external factors. I wanted to analyse the two options I have. 1. Keep starting and removing threads in the system every so much time OR 2. Start with the MAX number of threads and to reduce, I just tell them to wait in a Thread.sleep. .. this 2nd approach is more simple from implementation point of view, but wanted to analyse that how much stack space a waiting thread is taking in the system so that I run out the chances of Out of memory. – Vikas Jain Jan 06 '11 at 21:27
  • 8
    Thread stack size: "On x86 Solaris/Linux it is 320k in the 32-bit VM and 1024k in the 64-bit VM. On Windows, [...] this value is 320k in the 32-bit VM and 1024k in the 64-bit VM." [source](http://www.oracle.com/technetwork/java/hotspotfaq-138619.html#threads_oom) – Ricket Oct 03 '12 at 17:08
  • @MichaelBorgwardt the use case ofr such information could be, for example to determine, if some of similar threads are wasting too much memory. For example, when threads do something with user given arguments, and user finds corner case and sends your thread to infinite loop, where it expands some collection size. In such case it would be good to be able to see how much any thread by itself uses memory, that is not shared with others. – Lauri Dec 20 '13 at 15:48
  • 1
    @Lauri: all heap memory is shared between all threads per definition. – Michael Borgwardt Dec 20 '13 at 16:50
  • Idk...I think the question is valid, to an extent. I agree it's a bit ridiculous to micromanage how much memory a Thread consumes on it's own, at least in most cases (i.e. stack size, which you answered by specifying the default is 512kB), but I'm getting into it with someone now complaining about a large number of Threads existing for a process, with not enough heap size allocated to it to allow the usage of memory for the stack for each Thread, and advocating never using Threadpools for this reason, which I think is ridiculous. Thread memory footprint for stack size is pertinent. – searchengine27 Feb 21 '17 at 17:52
  • It does make sense. If I have an app where I can control the number of threads, while the RAM is fixed, I want to know the performance limit of my machine, it is totally valid. If I can create 10000 threads doing my job, I can happily put in my technical documentation that with my code and this machine, I can simulate the behavior of 10000 users; if not, or I have to look into my code to reduce the memory leak/failure, or I have to change my machine, or I want to know how many threads I can run. – WesternGun Feb 27 '18 at 09:01
  • @FaithReaper: it only makes sense if every thread does *exactly* the same things, creates the same objects that are not shared beetween threads, and does not depend on any external data. There's probably a lot of scenarios (specifically in web and app servers) where you can make a rough estimate, but very rarely will it be something you want to actually commit to. Some user actions will require more memory than others. What if your 10000 users next week all decide to try out a new feature that needs a lot of memory? – Michael Borgwardt Feb 27 '18 at 10:04