8

When we start threads in a Java program, is there any way for us to assign memory limit to each one of them?

I mean we assign something like this for a new Java process:

/usr/java/default/bin/java -Xms512m -Xmx1024m -jar /opt/abc/MyProcessor/MyProcessor.jar

Is there any way we can do similar thing with Java threads?

Basically, each of my threads is going to do some task, and I wish to put some maximum limit on each one's memory usage.

Bhushan
  • 18,329
  • 31
  • 104
  • 137
  • Can you provide any more details of the tasks? IS there some specific class used by the threads that is paticularly large/troublesome? – Martin James Jul 06 '11 at 18:58
  • @Martin James: No, there is no specific class as such. The thing is, each thread monitors a directory and whenever there is a file in that, it picks it up and processes it. Now, if there are many files in there, it will try to process many of them simultaneously, and this may consume too much memory. – Bhushan Jul 06 '11 at 19:04

4 Answers4

5

Is there any way we can do similar thing with Java threads?

No. Threads in a process are typically meant to access shared main memory within a process (the JVM in this case).

Basically, each of my threads is going to do some task, and I wish to put some maximum limit on each one's memory usage.

You do either:

  • The easy way. Spawn off new JVM processes where you can specify the heap size on each.
  • The hard way (not recommended by me; this is an option that is available). You could approximate the size of the objects that are created in each thread, and halt further execution of the thread if the size of the objects created by a thread exceeds a certain amount. This will require you to encapsulate the new keyword. In simpler words, all objects will have to be instantiated from factories that will keep tab on the approximate memory usage. Do keep in mind that object sizes on the heap are an approximation; Java does not have a sizeof operator. If you need to keep count of objects on the stack, then it is easy to do so, using the -Xss flag passed to the JVM at startup.
Vineet Reynolds
  • 76,006
  • 17
  • 150
  • 174
  • 1
    Because any third party library (including the standard Java libraries) you use allocates objects itself from elsewhere in that library or from the standard Java libraries, the second option would end up being a *very* crude approximation. – Dan Feb 24 '15 at 00:42
2

To place a limit on each thread you need to run each thread in a seperate process.

Threads share memory so there is no way to assign memory to a specific thread automatically. However you can do the calculations yourself in code (for each data type of interest) and manage how much memory each thread is using.

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

You can increase thread stack size with -Xss. It will apply to every thread. Heap is shared so you can´t change it for threads separatelly.

Alex Gitelman
  • 24,429
  • 7
  • 52
  • 49
1

In a word, no.

There is no concept of heap memory "owned" by a particular thread.

You can, however, tweak the maximum stack size (each thread has its own stack), but I doubt this is what you're after.

NPE
  • 486,780
  • 108
  • 951
  • 1,012