0

I want to create a Java app that able to limit the CPU and Memory usage for each thread. Let's say, it is a Rest API, and between each request and response, there is a process. I want to limit them. Is it possible to do it from the code dynamically?

Next related question is, what if I have a distributed machines, and want to apply this CPU and Memory limitation for each process again?

Maybe you can recommend some books or blogs related to this advance topic.

Thank you

yeulucay
  • 434
  • 8
  • 17
  • Duplicate question to this question: https://stackoverflow.com/questions/4952528/limiting-java-applications-memory-and-cpu-usage?noredirect=1&lq=1 – Dudi Boy Nov 10 '18 at 21:34

2 Answers2

2

You cannot do this on Java layer alone, you need to dig into the virtual machine.

I would suggest to take the source code of some simple virtual machine like JamVM and try build to understand it. It may be cases when the possibility to set limits is more important than overall performance.

OpenJDK would provide you with rather recent code base but it is obviously much more complex implementation. Cacao may be somewhat in between.

As an alternative, you may try to partition your application between multiple Java virtual machines running in parallel. Of course, not every app can be partitioned this way, but a purpose written app probably could be. If to go this way, look for the existing projects like JPPF.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
1

You can achieve it partially with JVM (Java Virtual Machine) arguments - you can set stack size for thread and heap size for JVM. But there is no possibility of limit memory for each thread. Is there a way to put maximum memory usage limit on each thread in Java?

It's not possible also to limit CPU usage Limiting java application's memory and cpu usage

You can set threads priorities in Java program via setPriority() in Thread class - but it's awful and really not effective.

You can also spawn things that are now thread as separate process (not separate thread) - with fixed heap size and custom process priority (cpu usage). But you have to make multiprocesess communication effective, what require some knowledge (don't write it on you own, use some already battle tested messaging system). This solution require probably huge changes in your current code.

Text bellow is personal opinion

Whenever you have application where you have to set priorities via some magic (manually or in code) there is 99% of chance that application architecture is broken by design.

But WHY?

Well, people who made JVM spent years developing the most effective thread management solution. You should admit that JVM knows better how to handle it.

Alex Baranowski
  • 1,014
  • 13
  • 22