2

Can we run multiple processes in one JVM? And each process should have its own memory quota?

My aim is to start new process when a new http request comes in and assign a separate memory to the process so that each user request has its own memory quota - and doesn't bother other user requests if one's memory quota gets full.

How can I achieve this?

Not sure if this is hypothetical.

1
  • 2
    Can we run multiple processes in one JVM? Yes. and each process should have own memory quota? No. Commented May 12, 2017 at 10:29

2 Answers 2

6

Short answer: not really.

The Java platform offers you two options:

  1. Threads. And that is the typical answer in many cases: each new incoming request is dealt with by a separate thread (which is probably coming out of a pool to limit the overall number of thread instances that get created/used in parallel). But of course: threads exist in the same process; there is no such thing as controlling the memory consumption "associated" by what a thread is doing.
  2. Child processes. You can create a real process and use that to run whatever you intend to run. But of course: then you have an external real process to deal with.

So, in essence, the real answer is: no, you can't apply this idea to Java. The "more" Java solution would be to look into concepts such as application servers, for example Tomcat or WebSphere.

Or, if you insist on doing things manually; you could build your own "load balancer"; where you have one client-facing JVM; which simply "forwards" requests to one of many other JVMs; and those "other" JVMs would work independently; each running in its own process; which of course you could then "micro manage" regarding CPU/memory/... usage.

Sign up to request clarification or add additional context in comments.

4 Comments

About Child processes "you have an external real process to deal with" is it intend to start another JVM ?
It is a child process. You decide what this process will do. Of course, you can run another JVM within a child process. But understand: that JVM runs completely on its own. There is no magic connection between those two JVMs, so if you want to communicate with that thing, you have to use sockets, or other ways of inter-process communication!
Ok. How can I run multiple processes with in a JVM that share no memory with each other? If I use ProcessBuilder it will start another JVM. My each http request is isolated but starting another JVM for each request isn't a good way though. So the situation I want to achieve is running multiple process in one jvm and controlling memory quota for each process.
I understand. But you have to understand that this isn't possible with standard java. If at all, you would have to search for a special proprietary JVM that supports this requirement. I am not aware of such a thing...
1

The closest concept is Application Isolation API (JSR-121) that AFAIK has not been implemented: See https://en.wikipedia.org/wiki/Application_Isolation_API.

"The Application Isolation API (JSR 121) provides a specification for isolating and controlling Java application life cycles within a single Java Virtual Machine (JVM) or between multiple JVMs. An isolated computation is described as an Isolate that can communicate and exchange resource handles (e.g. open files) with other Isolates through a messaging facility."

See also https://www.flux.utah.edu/janos/jsr121-internal-review/java/lang/isolate/package-summary.html:

"Informally, isolates are a construct midway between threads and JVMs. Like threads, they can be used to initiate concurrent execution. Like JVMs, they cause execution of a "main" method of a given class to proceed within its own system-level context, independently of any other Java programs that may be running. Thus, isolates differ from threads by guaranteeing lack of interference due to sharing statics or per-application run-time objects (such as the AWT thread and shutdown hooks), and they differ from JVMs by providing an API to create, start, terminate, monitor, and communicate with these independent activities."

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.