多个 JVM 进程可以共享公共类的内存吗?
我想在我的 Web 服务器上运行多个 Java 进程,每个 Web 应用程序一个.我正在使用一个具有大量支持类和 jar 文件的 Web 框架(Play),并且 Java 进程使用大量内存.一个 Play 进程显示大约 225MB 的驻留私有"内存.(我正在使用 Java 1.7.0_05 的 Mac OS X 上对此进行测试.)特定于应用程序的代码可能只有几 MB.我知道典型的 Java Web 应用程序是添加到一个服务器进程(Tomcat 等)的 jar,但似乎运行 Play 的标准方式是作为独立的应用程序/进程.如果这些是 C 程序,那 200MB 中的大部分将是共享库,并且不会在每个应用程序中复制.有没有办法在 Java 中实现这一点?我看到一些关于类数据共享的页面,但这似乎只适用于核心运行时类.
I'd like to run multiple Java processes on my web server, one for each web app. I'm using a web framework (Play) that has a lot of supporting classes and jar files, and the Java processes use a lot of memory. One Play process shows about 225MB of "resident private" memory. (I'm testing this on Mac OS X, with Java 1.7.0_05.) The app-specific code might only be a few MB. I know that typical Java web apps are jars added to one server process (Tomcat, etc), but it appears the standard way to run Play is as a standalone app/process. If these were C programs, most of that 200MB would be shared library and not duplicated in each app. Is there a way to make this happen in Java? I see some pages about class data sharing, but that appears to apply only to the core runtime classes.
推荐答案
目前,使用 Oracle VM,这是不可能的.
At this time and with the Oracle VM, this isn't possible.
但我同意,这将是一个不错的功能,尤其是因为 Java 拥有自动执行此操作所需的所有信息.
But I agree, it would be a nice feature, especially since Java has all the information it needs to do that automatically.
最重要的是,我认为 JIT 是它无法工作的唯一原因:JIT 将运行时行为考虑在内.因此,如果应用 A 以与应用 B 不同的模式使用某些代码,则会导致在运行时生成不同的汇编代码.
Of the top of my hat, I think that the JIT is the only reason why this can't work: The JIT takes runtime behavior into account. So if app A uses some code in a different pattern than app B, that would result in different assembler code generated at runtime.
但是,通常的模式"是这段代码的使用频率".因此,如果应用 A 经常调用某个方法而 B 没有调用,他们仍然可以共享代码,因为 A 已经为优化/编译它付出了代价.
But then, the usual "pattern" is "how often is this code used." So if app A called some method very often and B didn't, they could still share the code because A has already paid the price for optimizing/compiling it.
您可以尝试将多个应用程序作为 WAR 文件部署到单个 VM 中.但根据我的经验,这通常会导致代码无法正确清理线程局部变量或关闭挂钩的问题.
What you can try is deploy several applications as WAR files into a single VM. But from my experience, that often causes problems with code that doesn't correctly clean up thread locals or shutdown hooks.
相关文章