java 为什么每个应用程序有一个 JVM?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13539132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why have one JVM per application?
提问by Apple Grinder
I read that each application runs in its own JVM. Why is it so ? Why don't they make one JVM run 2 or more apps ?
我读到每个应用程序都在自己的 JVM 中运行。为什么会这样?他们为什么不让一个 JVM 运行 2 个或更多应用程序?
I read a SO post, but could not get the answers there. Is there one JVM per Java application?
我读了一个 SO 帖子,但无法在那里得到答案。 每个 Java 应用程序是否有一个 JVM?
I am talking about applications launched via a public static void main(String[]) method ...)
我说的是通过 public static void main(String[]) 方法启动的应用程序......)
回答by Stephen C
(I assume you are talking about applications launched via a public static void main(String[])
method ...)
(我假设您正在谈论通过public static void main(String[])
方法启动的应用程序......)
In theory you can run multiple applications in a JVM. In practice, they can interfere with each other in various ways. For example:
理论上,您可以在一个 JVM 中运行多个应用程序。在实践中,它们可以以各种方式相互干扰。例如:
- The JVM has one set of System.in/out/err, one default encoding, one default locale, one set of system properties, and so on. If one application changes these, it affects all applications.
- Any application that calls System.exit() kills all applications.
- If one application thread goes wild, and consumes too much CPU or memory it will affect the other applications too.
- JVM 具有一组 System.in/out/err、一种默认编码、一种默认语言环境、一组系统属性,等等。如果一个应用程序更改这些,它会影响所有应用程序。
- 任何调用 System.exit() 的应用程序都会杀死所有应用程序。
- 如果一个应用程序线程变得异常,并消耗过多的 CPU 或内存,它也会影响其他应用程序。
In short, there are lots of problems. People have tried hard to make this work, but they have never really succeeded. One example is the Echidnalibrary, though that project has been quiet for ~10 years. JNodeis another example, though they (actually we) "cheated" by hacking core Java classes (like java.lang.System) so that each application got what appeared to be independent versions of System.in/out/err, the System properties and so on1.
简而言之,问题很多。人们努力使这项工作成功,但他们从未真正成功。一个例子是Echidna图书馆,尽管该项目已经沉寂了大约 10 年。 JNode是另一个例子,尽管他们(实际上是我们)通过攻击核心 Java 类(如 java.lang.System)来“欺骗”,因此每个应用程序都获得了看似独立版本的 System.in/out/err,系统属性等等1。
1 - This ("proclets") was supposed to be an interim hack, pending a proper solution using true "isolates". But isolates support stalled, primarily because the JNode architecture used a single address space with no obvious way to separate "system" and "user" stuff. So while we could create APIs that matched the isolate APIs, key isolate functionality (like cleanly killing an isolate) was virtually impossible to implement. Or at least, that was/is my view.
1 - 这(“proclets”)应该是一个临时黑客,等待使用真正的“隔离”的适当解决方案。但是对隔离的支持停滞不前,主要是因为 JNode 架构使用了一个单一的地址空间,没有明显的方法来分隔“系统”和“用户”的东西。因此,虽然我们可以创建与隔离 API 相匹配的 API,但关键的隔离功能(例如彻底杀死隔离)实际上是不可能实现的。或者至少,那是/是我的观点。
回答by Alexey Ragozin
Reason to have one JVM pre application, basically same having OS process per application. Here are few reasons why to have a process per application.
拥有一个 JVM 预应用程序的原因,基本上与每个应用程序具有操作系统进程相同。以下是为什么每个应用程序都有一个进程的几个原因。
- Application bug will not bring down / corrupt data in other applications sharing same process.
- System resources are accounted per process hence per application.
- Terminating process will automatically release all associated resources (application may not clean up for itself, so sharing processes may produce resource leaks).
- 应用程序错误不会降低/损坏共享同一进程的其他应用程序中的数据。
- 系统资源按进程计算,因此按应用计算。
- 终止进程会自动释放所有关联的资源(应用程序可能不会为自己清理,因此共享进程可能会产生资源泄漏)。
Well some applications such a Chrome go even further creating multiple processes to isolate different tabs and plugins.
一些应用程序(例如 Chrome)甚至会进一步创建多个进程来隔离不同的选项卡和插件。
Speaking of Java there are few more reasons not to share JVM.
说到 Java,还有几个理由不共享 JVM。
- Heap space maintenance penalty is higher with large heap size. Multiple smaller independent heaps easier to manage.
- It is fairly hard to unload "application" in JVM (there to many subtle reasons for it to stay in memory even if it is not running).
- JVM have a lot of tuning option which you may want to tailor for an application.
- 大堆大小的堆空间维护代价更高。多个更小的独立堆更易于管理。
- 在 JVM 中卸载“应用程序”是相当困难的(即使它没有运行,也有许多微妙的原因让它留在内存中)。
- JVM 有很多调整选项,您可能希望为应用程序量身定制。
Though there are several cases there JVM is actually shared between application:
尽管有几种情况,但实际上 JVM 是在应用程序之间共享的:
- Application servers and servlet containers (e.g. Tomcat). Server side Java specs are designed with shared server JVM and dynamic loading/unloading applications in mind.
- There few attempts to create shared JVM utility for CLI applications (e.g. nailgun)
- 应用程序服务器和 servlet 容器(例如 Tomcat)。服务器端 Java 规范在设计时考虑了共享服务器 JVM 和动态加载/卸载应用程序。
- 很少尝试为 CLI 应用程序创建共享的 JVM 实用程序(例如钉枪)
But in practice, even in server side java, it usually better to use JVM (or several) per applications, for reasons mentioned above.
但实际上,即使在服务器端 java 中,由于上述原因,每个应用程序通常最好使用 JVM(或多个)。
回答by mgarciaisaia
For isolating execution contexts.
用于隔离执行上下文。
If one of the processes hangs, or fails, or it's security is compromised, the others don't get affected.
如果其中一个进程挂起或失败,或者它的安全性受到损害,其他进程不会受到影响。
I think having separate runtimes also helps GC, because it has less references to handle than if it was altogether.
我认为拥有单独的运行时也有助于 GC,因为与完全一样,它需要处理的引用更少。
Besides, why would you run them all in one JVM?
此外,为什么要在一个 JVM 中运行它们?
回答by Evgeniy Dorofeev
Java Application Servers, like JBoss, are design to run many applications in one JVM
Java 应用服务器,如 JBoss,旨在在一个 JVM 中运行多个应用程序
回答by vels4j
It depends with your application.
这取决于您的应用程序。
Waratek provides Hosting multiple Java applications within a single JVMJust check that.
Waratek 提供在单个 JVM 中托管多个 Java 应用程序只需检查一下。