当多个java程序在同一台机器上运行时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18394560/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:10:29  来源:igfitidea点击:

When multiple java programs run on the same machine

javajvm

提问by Aniket Thakur

Each java application will run in a specific Java Virtual Machine Instance. I am really getting confused on below aspects and Googling has confused me even more. Different articles on different sites.

每个 Java 应用程序都将在特定的 Java 虚拟机实例中运行。我真的对以下方面感到困惑,而谷歌搜索让我更加困惑。不同网站上的不同文章。

  1. If I have a web service written in java it will need a JVM instance to run.So can JVM be made a daemon process?

  2. If yes when we run any other java application it will use this instance of JVM or create a new one?

  3. Main memory available in any machine is constant. When we start n java processes simultaneously without providing any initial heap size how is the heap size distributed among processes?

  4. Is there any process that manages n number of JVM instances or is it managed by OS itself?

  5. When stop-the-world happens during an GC are other JVM instances(different threads I assume) affected?

  1. 如果我有一个用 java 编写的 web 服务,它需要一个 JVM 实例来运行。那么 JVM 可以成为一个守护进程吗?

  2. 如果是,当我们运行任何其他 Java 应用程序时,它会使用这个 JVM 实例还是创建一个新实例?

  3. 任何机器中可用的主内存都是恒定的。当我们同时启动 n 个 java 进程而不提供任何初始堆大小时,堆大小如何在进程之间分配?

  4. 是否有任何进程管理 n 个 JVM 实例或由操作系统本身管理?

  5. 当在 GC 期间发生 stop-the-world 时,其他 JVM 实例(我假设是不同的线程)会受到影响吗?

采纳答案by Stephen C

1) If I have a web service written in java it will need a JVM instance to run. So can JVM be made a daemon process?

1) 如果我有一个用 Java 编写的 Web 服务,它将需要一个 JVM 实例才能运行。那么JVM可以成为守护进程吗?

Yes it can. How it is done depends on the O/S and on the web server container itself.

是的,它可以。如何完成取决于操作系统和 Web 服务器容器本身。

2) If yes when we run any other java application it will use this instance of JVM or create a new one?

2)如果是,当我们运行任何其他 Java 应用程序时,它会使用这个 JVM 实例还是创建一个新实例?

No. Each Java application uses an independent JVM.

不可以。每个 Java 应用程序都使用一个独立的 JVM。

Each JVM is a separate process, and that means there is no sharing of stacks, heaps, etcetera. (Generally, the only things that might be shared are the read only segments that hold the code of the core JVM and native libraries ... in the same way that normal processes might share code segments.)

每个 JVM 都是一个单独的进程,这意味着不共享堆栈、堆等。(通常,唯一可能共享的内容是包含核心 JVM 和本机库代码的只读段……与正常进程可能共享代码段的方式相同。)

3) Main memory available in any machine is constant. When we start n java processes simultaneously without providing any initial heap size how is the heap size distributed among processes?

3) 任何机器中可用的主内存都是恒定的。当我们同时启动 n 个 java 进程而不提供任何初始堆大小时,堆大小如何在进程之间分配?

The mechanism for deciding how big to make the heap if you don't specify a size depends on the JVM / platform / version you are using, and whether you using the "client" or "server" model (for Hotspot JVMs). The heuristic doesn't take account of the number or size of other JVMs.

如果您不指定大小,则决定堆大小的机制取决于您使用的 JVM/平台/版本,以及您使用的是“客户端”还是“服务器”模型(对于 Hotspot JVM)。启发式不考虑其他 JVM 的数量或大小。

Reference: https://stackoverflow.com/a/4667635/139985

参考:https: //stackoverflow.com/a/4667635/139985

In practice, you would probably be better off specifying the heap size directly.

实际上,您最好直接指定堆大小。

4) Is there any process that manages n number of JVM instances or is it managed by OS itself?

4) 是否有任何进程管理 n 个 JVM 实例或由操作系统本身管理?

Neither. The number of JVM instances is determined by the actions of various things that can start processes; e.g. daemons scripts, command scripts, users typing commands at the command line, etcetera. Ultimately, the OS may refuse to start any more processes if it runs out of resources, but JVMs are not treated any different to other processes.

两者都不。JVM 实例的数量由可以启动进程的各种事物的动作决定;例如守护进程脚本、命令脚本、用户在命令行输入命令等。最终,如果操作系统耗尽资源,它可能会拒绝启动更多进程,但 JVM 的处理方式与其他进程没有任何不同。

5) When stop-the-world happens during an GC are other JVM instances(different threads I assume) affected?

5) 当 GC 期间发生 stop-the-world 时,其他 JVM 实例(我假设是不同的线程)会受到影响吗?

No. The JVMs are independent processes. They don't share any mutable state. Garbage collection operates on each JVM independently.

不可以。JVM 是独立的进程。它们不共享任何可变状态。垃圾收集在每个 JVM 上独立运行。

回答by Alexei Kaigorodov

  1. see How to Daemonize a Java Program?
  2. new instance of JVM will be created
  3. the same way as memory is shared between all other processes
  4. it is managed by O/S
  5. other instances are not affected
  1. 请参阅如何守护 Java 程序?
  2. 将创建新的 JVM 实例
  3. 与所有其他进程之间共享内存的方式相同
  4. 它由 O/S 管理
  5. 其他实例不受影响

If your instances have to coordinate their work, you can create single main instance which would run/stop other instances.

如果您的实例必须协调它们的工作,您可以创建单个主实例来运行/停止其他实例。

You did not explain why you need multiple JVM instances. Probably, single instance would work better.

您没有解释为什么需要多个 JVM 实例。可能,单个实例会更好。