Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread-specific heap allocation

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 ... :)

like image 432
Xtra Coder Avatar asked Jan 17 '12 11:01

Xtra Coder


People also ask

Do threads have their own heap allocated area?

Depends on the OS. The standard c runtime on windows and unices uses a shared heap across threads. This means locking every malloc/free. On Symbian, for example, each thread comes with its own heap, although threads can share pointers to data allocated in any heap.

Does each thread get its own heap?

Multiple threads could be running using this same thread procedure but since each thread has its own heap there is no contention between threads. In addition, the fact that each heap is not thread-safe gives a measurable increase in performance even if just one copy of the thread is running.

Do threads have the same heap?

We talked about the two types of memory available to a process or a thread, the stack and the heap. It is important to distinguish between these two types of process memory because each thread will have its own stack, but all the threads in a process will share the heap.

Do multiple threads share the same heap?

The answer is simple: all threads in C share the same address space. This means all memory, the heap included, is shared between all threads.


3 Answers

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.

like image 75
alf Avatar answered Oct 06 '22 22:10

alf


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.

like image 38
bmargulies Avatar answered Oct 06 '22 23:10

bmargulies


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.

like image 30
Peter Lawrey Avatar answered Oct 07 '22 00:10

Peter Lawrey