docker 容器提供了相关的内存限制。具体使用方式如:
-m 512m
## 完整例子
docker run –rm -m 512m -e JAVA_OPTS=’-Xmx512m’ tomcat:8
通过-m 进行限制,但是在实际应用重,会出现jvm内存一直到内存满也没有执行gc。
在查询之后,问题出现在docker容器下jvm识别的内存为宿主机内存。
之后又添加了jvm的内存限制:
-Xmx512m
但是在openjdk 还是没有生效。
最后在
OpenJDK and Containers
找到。可以使用 -XX:MaxRam=500m。
JVM Argument
Effect | |
---|---|
-XX:+UseSerialGC | Uses only 1 GC thread. This both limits the cpu instruction count for garbage collection, but also results in the minimal memory footprint. |
-XX:MaxRAM=n | Sets the maximum amount of memory used by the JVM to n, where n may be expressed in terms of megabytes 100m or gigabytes 2g. |
-XX:+UseCGroupMemoryLimitForHeap | This flag present in the more recent builds of JDK8 tells the JVM to use the information in /sys/fs/cgroup/memory/memory.limit_in_bytes to calculate memory defaults. |
-XX:ParallelGCThreads=n | Set the number of parallel GC threads to n. This is helpful if you are trying to limit the cpu usage of your container, or if you are running with a JVM that doesn’t include the patch to calculate GC threads based on processors available to the cgroup. |
-XX:+TieredCompilation -XX:TieredStopAtLevel=1 |
Turns off the optimizing compiler. This can sometimes decrease the footprint of your running JVM. |
-XX:MinHeapFreeRatio=20 -XX:MaxHeapFreeRatio=40 |
These parameters tell the heap to shrink aggressively and to grow conservatively. Thereby optimizing the amount of memory available to the operating system. |
-XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 |
These parameters are necessary when running parallel GC if you want to use the Min and Max Heap Free ratios. |
-XX:+UnlockDiagnosticVMOptions -XX:NativeMemoryTracking= “summary” -XX:+PrintNMTStatistics |
These options will print out the non-heap memory usage of your JVM. |
-Xss228k | This will decrease the size of your Java Stacks. |