Can multiple JVM processes share memory for common classes?

I want to run several Java processes on my web server, one for each web application. I use a web framework (Play), which has many supporting classes and jar files, and Java processes use a lot of memory. One playback process shows about 225 MB of "resident private" memory. (I am testing this on Mac OS X, with Java 1.7.0_05.) The application code can only be a few MB. I know that typical Java web applications are banks added to a single server process (Tomcat, etc.), but it seems the standard way to start Play is with a standalone application / process. If these are C programs, most of these 200 MB will be a shared library and not duplicated in every application. Is there any way to do this in Java? I see a few pages about the distribution of class data , but it seems to apply only to the main runtime classes.

+6
source share
4 answers

This is not possible with Oracle VM at this time.

But I agree that this would be a good feature, especially since Java has all the information necessary for this automatically.

At the top of my hat, I think JIT is the only reason why this cannot work: JIT takes into account runtime behavior. Therefore, if application A uses some code in a different template than application B, this will lead to the creation of assembler code generated at runtime.

But then the usual "template" is "how often this code is used." Therefore, if application A called some method very often, but B didn’t, they could still share the code, because A already paid the price for optimization / compilation.

What you can try is to deploy several applications as WAR files into one virtual machine. But, in my experience, this often causes problems with code that incorrectly cleans stream locators or shuts down.

+5
source

The IBM JDK has a jvm parameter to achieve this. Check out @ http://www.ibm.com/developerworks/library/j-sharedclasses/

And this will go to the next step: http://www.ibm.com/developerworks/library/j-multitenant-java/index.html

+4
source

If you use a servlet container with virtual host support (I believe Tomcat does this), you can use play2-war-plugin . From Play 2.1, the requirement to always be the root application will be canceled, so you can probably use any servlet container.

Keep in mind that you may have to set up a war file to move material from WEB-INF/lib to your servlet container's lib directory so that all classes are not loaded again, and this may affect your application if it uses singleton or other forms of common class data.

+1
source

The problem of exchanging memory between JVM instances is more pressing on mobile platforms, and as far as I know, Android has a pretty smart solution for Zygote: the virtual machine is initialized, and then when it starts the application it fork() Linux editor uses copy operations to write to the RAM pages, therefore most data will not be duplicated.

Porting this solution may be possible if you are running Linux and want to try using Dalvik as your virtual machine (I have seen claims that Dalvik has a working tomcat port). I would expect this to be a huge job, ultimately saving you a little $ s on memory updates.

0
source

All Articles