This discussion is archived
4 Replies Latest reply: Nov 21, 2012 8:52 AM by jtahlborn RSS

heap memory allocated by a single thread

975293 Newbie
Currently Being Moderated
Via JMX or other way, could I know the heap memory is allocated by a single thread? It should be programatically

Thanks
  • 1. Re: heap memory allocated by a single thread
    jtahlborn Expert
    Currently Being Moderated
    i'm not aware of any way to do it programatically, or even easily via JMX. the information that is needed is available via the JVMTI interface that debuggers/profilers typically use, but you can't really use this from inside the JVM. regardless, the information available via JVMTI takes a bit of work to sort through in order to actually determine which thread owns what (and some objects could be referenced by many threads, in which case there is no clear "ownership").

    short answer, this info is not available. why do you need it?
  • 2. Re: heap memory allocated by a single thread
    975293 Newbie
    Currently Being Moderated
    I need it because I am receiving a script groovy via http and I have to execute it and return the result to the client but I have no control over the scripts I receive, so in order the script not to provoque an OutOfMemoryError, I would like to know if I coud avoid it and know the memory heap occupied by single thread .

    Thanks
  • 3. Re: heap memory allocated by a single thread
    baftos Expert
    Currently Being Moderated
    Write a small program that starts as many do-nothing-in-a-loop threads and see what happens. You may well find out that the limit is not the heap size, but some OS imposed limit on the number of threads per process.
    By the way, the most memory allocated for a do-nothing thread is the thread stack, which is not in the heap. The heap will only contain the Thread object and whatever it allocates internally to be able to function, which should be very little (tenths of bytes, I guess).

    Edited by: baftos on Nov 21, 2012 11:33 AM
  • 4. Re: heap memory allocated by a single thread
    jtahlborn Expert
    Currently Being Moderated
    baftos wrote:
    Write a small program that starts as many do-nothing-in-a-loop threads and see what happens. You may well find out that the limit is not the heap size, but some OS imposed limit on the number of threads per process.
    By the way, the most memory allocated for a do-nothing thread is the thread stack, which is not in the heap. The heap will only contain the Thread object and whatever it allocates internally to be able to function, which should be very little (tenths of bytes, I guess).
    i'm pretty sure the OP is not looking for the memory used by the Thread itself, but the total of all the Objects "owned" by the thread.

    The OP is looking to control code which may cause OOME through poor memory usage. This is a difficult problem, and not really all that possible in the jvm as it stands today (at least the Oracle jvm).

    we have a similar situation in our product. The "best" we have been able to do is use JMX to determine when the jvm is low on memory. it's not really possible to figure out which Thread is responsible for this situation, unfortunately. in our product, we switch into a "low memory" mode where various internal data structures switch to a conservative memory mode.

    you may be able to use some heuristics to pick a script to kill, e.g. longest running script.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points