9

Is it possible to make some sub-set of threads (e.g. from specific ThreadPool) allocate memory from own heap? E.g. most of the threads are allocating from regular shared heap, and few worker threads are allocating from individual heaps (1:1 per thread).

The intent is to ensure safe execution of the code in shared environment - typical worker is stateless and is running on separate thread, processing of one request should not consume more than 4MB of heap.

Update #1 Re: But why are you worried about "safe execution" and unpredictable increasing of heap consumption?

The point is about safe hosting of arbitrary 3rd party java code within my process. Once of the points is to not get "Out of Memory" for my entire process because of bugs in the 3rd party code.

Update #2 Re: As of limiting memory usage per thread, in Java the language it's impossible

According to my investigation before I've posted this question my opinion is the same, I'm just hoping I'm missing something.

The only possible alternative solutions for my use-case as I see right now are ...

1) How much memory does my java thread take? - track thread memory usage in some governor thread and terminate bad threads

2) Run Java code on my own JVM - Yes it is possible. You can download a JVM open source implementation, modify it ... :)

trincot
  • 317,000
  • 35
  • 244
  • 286
Xtra Coder
  • 3,389
  • 4
  • 40
  • 59
  • 1
    No, the whole idea behind threads is to be as lightweight as possible, so they share the same heap; it would also make synchronization harder if they allowed it. But why are you worried about "safe execution" and unpredictable increasing of heap consumption? – Viruzzo Jan 17 '12 at 11:24
  • 2
    Typical JVM implementations use Thread Local Allocation Buffers (TLABs), where each thread has its own little area to make small allocations without much thread-safety overhead. That doesn't address availability though. RTSJ has some stuff relating to threads and heaps, but that's about latency. – Tom Hawtin - tackline Jan 17 '12 at 11:39

3 Answers3

5

Check out Java nonblocking memory allocation — threads are usually allocating memory from their own allocation blocks already. So if the speed is of concern, Sun has done it for you.

As of limiting memory usage per thread, in Java the language it's impossible. Whether it is possible (or makes sense) in JVM and Java the platform is an interesting question. You can of course do it the same way as any memory profiler does, but I'm afraid the management system will outgrow the application itself pretty soon.

Community
  • 1
  • 1
alf
  • 8,377
  • 24
  • 45
2

No. There is no concept of this in Java. There is one 'heap' that new allocates from. Java allocation is thread-safe. And why do you think that making more heaps would cause threads to consume less memory?

If you want to control memory usage in a thread, don't allocate things.

You could, in theory, create pools of reusable objects for a purpose like this, but the performance would almost certainly be worse than the obvious alternative.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
2

Threads by design share all the heap and other regions of memory. Only the stack is truly thread local, and this space can be limited.

If you have tasks which you want to run in their own memory and/or can be stopped, you have to run them as a separate process.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130