本文共 3140 字,大约阅读时间需要 10 分钟。
Docker 内存占用测试
测试目的:合理设置内存、交换分区、内存预留测试程序:
JDK环境直接复制粘贴即可cat >OutOfMemeber.java<<EOFpackage 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(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); } }
}
EOFjavac OutOfMemeber.java -Xlint:uncheckedjava -Xmx20g OutOfMemeber #内存设置要大于内存限制才能申请到所有内存,否则jvm会预留内存无法占用全部测试:
1、命令如下:-m 4G:内存限制为4G --memory-swap=-1 :swap值为-1--memory-reservation=4G:预留内存为4Gdocker 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.0Mmax:5461.5Mfree:35.76Mmayuse: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.5Mmax:18204.5Mfree:56.26Mmayuse:10103.26M最大使用8G的时候被杀死(被系统杀死)
3、
docker run --rm -m 4G --memory-swap=4G --memory-reservation=4G -it java:testmem sh结果:total:4037.0Mmax:18204.5Mfree:35.76Mmayuse:14203.26M最大使用4G内存被杀死
4、
docker run --rm -m 4G --memory-swap=6G --memory-reservation=4G -it java:testmem sh结果:total:6147.5Mmax:18204.5Mfree:46.26Mmayuse:12103.26M最大使用6G内存被杀死,且容器占用的swap是系统的swap5、docker run --rm -m 4G --memory-swap=8G --memory-reservation=4G -it java:testmem sh结果:total:8157.5Mmax:14563.5Mfree:56.26Mmayuse:6462.26M最大使用8G内存= 内存+swapRancher平台
1、
内存限制4G内存预留4GSwap 不填内存:
"Memory": 4294967296,"CpusetMems": "","KernelMemory": 0,"MemoryReservation": 4294967296,"MemorySwap": 8589934592,"MemorySwappiness": -1,结果:total:4455.0Mmax:14563.5Mfree:1650.27Mmayuse:11758.77M2、
内存限制4G内存预留4GSwap 0内存:"Memory": 4294967296,"CpusetMems": "","KernelMemory": 0,"MemoryReservation": 4294967296,"MemorySwap": 4296015872,"MemorySwappiness": -1,结果:total:4448.5Mmax:14563.5Mfree:1643.77Mmayuse:11758.77M3、
内存限制4G内存预留4GSwap 4G内存:"Memory": 4294967296,"CpusetMems": "","KernelMemory": 0,"MemoryReservation": 4294967296,"MemorySwap": 8589934592,"MemorySwappiness": -1,结果:total:4441.5Mmax:14563.5Mfree:1636.77Mmayuse:11758.77M转载于:https://blog.51cto.com/2183087/2309024