Recently, when verifying the memory allocation rules of some machines, I learned some skills. I took advantage of the time to write something on the weekend and share it with you.

You may have encountered a similar scenario, and you want to perform stress testing on the machine to simulate the OOM scenario, but the specifications of the machine are too high. If you use code to implement it, you can imagine how to achieve it? Personally, it is still a bit troublesome.

So is there a good way to apply for memory directly to the machine with a few simple commands without writing code? Or even more extreme, just squeeze the machine's memory to dry. .

If you use Linux frequently, you will find that df -Th , there must be a file system of type tmpfs /dev/shm , although you will not pay attention to it.

$ df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  910M     0  910M   0% /dev
tmpfs          tmpfs     919M     0  919M   0% /dev/shm
tmpfs          tmpfs     919M  896K  918M   1% /run
tmpfs          tmpfs     919M     0  919M   0% /sys/fs/cgroup
/dev/vda1      ext4       40G   11G   27G  28% /
tmpfs          tmpfs     184M     0  184M   0% /run/user/0

And this tmpfs is the protagonist Ming brother will introduce today.

tmpfs, as the name suggests, is a temporary file system, a memory-based file system.

It is similar to the virtual disk ramdisk, but not exactly the same. Like ramdisk, tmpfs can use RAM, but it can also use swap partitions for storage. Moreover, the traditional ramdisk is a block device, which must be formatted with mkfs. Really use it; and tmpfs is a file system, not a block device, just install it, you can use it. tmpfs is the best RAM-based file system.

This means that all files you write to the directory where tmpfs is mounted will be written directly to the memory.

If you want to take up 10G of the machine's memory, I just need to create a temporary directory /tmp/memory , and specify the file system type and size of tmpfs 10240M to mount to this directory.

$ mount -t tmpfs -o size=10240M tmpfs /tmp/memory

Then we use the dd command, how much memory is written into the directory, because our purpose is to occupy memory, so if we directly use /dev/zero

$ dd if=/dev/zero of=/tmp/memory/block

When the dd writing is completed, you can use free to check the available memory, and you will find that the remaining memory can be allocated 10G less.

If you want to use up all the machine's memory, you can specify size as the machine's memory size when mounting, but you must be clear about what you are doing, otherwise your machine may hang after executing dd.

Using the above method, you can actually do more things. For example, if you have two NUMA Nodes on the machine, but you only want to occupy the memory of NUMA Node 0, you can specify the memory of NUMA Node 0. What should I do?

First use lscpu to find all cpu cores on NUMA Node 0

$ node0_cpus=$(lscpu | grep "NUMA node0" | awk '{print $NF}')

Then use the taskset tool to specify the corresponding cpu core to execute the process of creating the tmpfs directory and dd

$ cat > /root/mem_alloc.sh <<EOF
#!/bin/bash
tmpdir=`mktemp`
mount -t tmpfs -o size=1024M tmpfs ${tmpdir}
dd if=/dev/zero of=${tmpdir}/block
EOF

$ taskset -c "${node0_cpus}" sh /root/mem_alloc.sh

After the execution is complete, if the memory you occupy does not exceed the local memory of NUMA Node 0, then you will find that the above commands only occupy the memory of NUMA Node 0 when you use numactl.

Talk a bit

I have written a lot of Python-related articles on SegmentFault, including Python utility tools, Python high-efficiency skills, and PyCharm usage skills. I am very happy to get the recognition and support of many friends who know.

With their encouragement, I sorted the past articles into three PDF e-books

PyCharm Chinese Guide

"PyCharm Chinese Guide" uses more than 300 GIF dynamic images to explain in detail 105 PyCharm efficient use skills that are most suitable for actual development. The content is easy to understand and suitable for all Python developers.

Online experience address: https://pycharm.iswbm.com

Python Black Magic Guide

"Python Black Magic Guide" currently ushered in the v3.0 version, which contains more than 100 development tips, which is very suitable for reading fragments in leisure time.

Online experience address: https://magic.iswbm.com

Python Chinese Guide

The best learning material for learning Python is always the official Python documentation. Unfortunately, most of the official documentation is now in English. Although there is a Chinese translation, the progress is really worrying. In order to take care of classmates who are not good at English, I wrote an online Python document for friends with zero foundation - "Python Chinese Guide"

Online experience address: https://python.iswbm.com

It is helpful, remember to help point a praise yo ~


Python编程时光
135 声望26 粉丝