Explorar o código

Merge pull request #900 from akashche/patch-1

Add notes about CPU and RAM limits
Tianon Gravi %!s(int64=8) %!d(string=hai) anos
pai
achega
6ddcede5fd
Modificáronse 1 ficheiros con 20 adicións e 0 borrados
  1. 20 0
      openjdk/content.md

+ 20 - 0
openjdk/content.md

@@ -38,3 +38,23 @@ $ docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp %%REPO%%:7 javac Ma
 ```
 
 This will add your current directory as a volume to the container, set the working directory to the volume, and run the command `javac Main.java` which will tell Java to compile the code in `Main.java` and output the Java class file to `Main.class`.
+
+## Make JVM respect CPU and RAM limits
+
+On startup JVM tries to detect the number of available CPU cores and the amount of RAM to adjust its internal parameters (like the number of garbage collector threads to spawn) accordingly. When container is run with limited CPU/RAM, standard system API, used by JVM for probing, will return host-wide values. This can cause excessive CPU usage and memory allocation errors with older versions of JVM.
+
+Inside Linux containers, recent versions of OpenJDK 8 can correctly detect container-limited number of CPU cores by default. To enable the detection of container-limited amount of RAM the following options can be used:
+
+```console
+$ java -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap ...
+```
+
+Inside Windows Server (non-Hyper-V) containers, limit for number of available CPU cores does not work (is ignored by Host Compute Service). To set such limit manually, JVM can be started the following way:
+
+```console
+$ start /b /wait /affinity 0x3 path/to/java.exe ...
+```
+
+In this example CPU affinity hex mask `0x3` will limit JVM to 2 CPU cores.
+
+RAM limit is supported by Windows Server containers, but currently JVM cannot detect it. To prevent excessive memory allocations, `-XX:MaxRAM=...` option must be specified with the value that is not bigger than a containers RAM limit.