Docker 内存占用测试 测试目的:合理设置内存、交换分区、内存预留

测试程序: JDK环境直接复制粘贴即可 cat >OutOfMemeber.java<<EOF package com.ptmind.datadeck; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; public class OutOfMemeber { public static void main(String[] args) { List<Object> list=new ArrayList<>(); DecimalFormat df = new DecimalFormat("######0.00"); while (true) {

                 byte b[] = new byte[1024*1024*100];
                 list.add(b);
                 double total = (Runtime.getRuntime().totalMemory()) / (1024.0 * 1024);
                 System.out.println("total:"+total+"M");
                 double max = (Runtime.getRuntime().maxMemory()) / (1024.0 * 1024);
                 System.out.println("max:"+max+"M");
                 double free = (Runtime.getRuntime().freeMemory()) / (1024.0 * 1024);
                 System.out.println("free:"+df.format(free)+"M");
                 double mayuse=(max - total + free);
                 System.out.println("mayuse:"+df.format(mayuse)+"M");
                 System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
             }
         }

} EOF javac OutOfMemeber.java -Xlint:unchecked java -Xmx20g OutOfMemeber #内存设置要大于内存限制才能申请到所有内存,否则jvm会预留内存无法占用全部

测试: 1、 命令如下: -m 4G:内存限制为4G --memory-swap=-1 :swap值为-1 --memory-reservation=4G:预留内存为4G

docker run --rm -m 4G --memory-swap=-1 --memory-reservation=4G -it java:testmem sh 内存: "Memory": 4294967296, "CpusetMems": "", "KernelMemory": 0, "MemoryReservation": 4294967296, "MemorySwap": -1, "MemorySwappiness": -1, 运行结果: total:4037.0M max:5461.5M free:35.76M mayuse:1460.26M

结论:最大占用4G 内存

2、 docker run --rm -m 4G --memory-swap=0 --memory-reservation=4G -it java:testmem sh 内存: "Memory": 4294967296, "CpusetMems": "", "KernelMemory": 0, "MemoryReservation": 4294967296, "MemorySwap": 8589934592, "MemorySwappiness": -1, 结果: total:8157.5M max:18204.5M free:56.26M mayuse:10103.26M

最大使用8G的时候被杀死(被系统杀死)

3、 docker run --rm -m 4G --memory-swap=4G --memory-reservation=4G -it java:testmem sh 结果: total:4037.0M max:18204.5M free:35.76M mayuse:14203.26M

最大使用4G内存被杀死

4、 docker run --rm -m 4G --memory-swap=6G --memory-reservation=4G -it java:testmem sh 结果: total:6147.5M max:18204.5M free:46.26M mayuse:12103.26M 最大使用6G内存被杀死,且容器占用的swap是系统的swap 5、 docker run --rm -m 4G --memory-swap=8G --memory-reservation=4G -it java:testmem sh 结果: total:8157.5M max:14563.5M free:56.26M mayuse:6462.26M 最大使用8G内存= 内存+swap

Rancher平台

1、 内存限制4G 内存预留4G Swap 不填

内存: "Memory": 4294967296, "CpusetMems": "", "KernelMemory": 0, "MemoryReservation": 4294967296, "MemorySwap": 8589934592, "MemorySwappiness": -1, 结果: total:4455.0M max:14563.5M free:1650.27M mayuse:11758.77M

2、 内存限制4G 内存预留4G Swap 0 内存: "Memory": 4294967296, "CpusetMems": "", "KernelMemory": 0, "MemoryReservation": 4294967296, "MemorySwap": 4296015872, "MemorySwappiness": -1, 结果: total:4448.5M max:14563.5M free:1643.77M mayuse:11758.77M

3、 内存限制4G 内存预留4G Swap 4G 内存: "Memory": 4294967296, "CpusetMems": "", "KernelMemory": 0, "MemoryReservation": 4294967296, "MemorySwap": 8589934592, "MemorySwappiness": -1, 结果: total:4441.5M max:14563.5M free:1636.77M mayuse:11758.77M