Recently, WHILE verifying the memory allocation rules of some machines, I have learned some skills, so I can write some things over the weekend and share them with you.

We may have encountered a similar scenario, want to simulate the OOM scenario on the machine, but the specifications of the machine is too high, if the code to achieve, we can imagine how to achieve? Personally, it’s a bit troublesome.

So is there a good way, do not write code, with a few simple commands can directly apply to the machine memory? Or, more extreme, drain the machine’s memory.

If you’re a regular Linux user, you’ll notice that df-th is bound to have a tmpfs-type filesystem mounted under /dev/shm, although you probably won’t notice 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
Copy the code

And this TMPFS is the protagonist that Mingo is going to introduce today.

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

Like ramdisk, TMPFS can use RAM, but it can also use swap for storage. Ramdisk is a block device, and you need to format it with MKFS before you can really use it. TMPFS is a file system, not a block device. It’s just installed and ready to use. TMPFS is the best RAM-based file system.

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

If you want to use 10GB of memory on your machine, you can create a temporary directory/TMP /memory and mount TMPFS file system type and size 10240MB to this directory.

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

Then we use the dd command to write as much memory as we want to write to the directory, so if uses /dev/zero directly

$ dd if=/dev/zero of=/tmp/memory/block
Copy the code

After dd writing is complete, you can use free to check the available memory and find that the remaining memory has 10GB less memory to allocate.

If you want to use up all the memory of the machine, you can specify the size of the machine at mount time, but you need to be aware of what you are doing, otherwise your machine may hang after executing dd.

If you have two NUMA nodes on your machine and you only want to use the memory of NUMA Node 0, you can specify the memory of NUMA Node 0.

First use LSCPU to find all CPU cores on NUMA Node 0

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

Then use the Taskset tool to specify the corresponding CPU core to perform 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
Copy the code

After executing this command, if you do not use more memory than the local memory of NUMA Node0, you will find that numactl commands only use the memory of NUMA Node0.

Ramble about the

I have written many Python related articles on Nuggets, including Python utilities, Python Efficiency Tips, and PyCharm. I am very glad to have received the recognition and support from many Zhihu friends.

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

PyCharm Chinese Guide

PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide

Experience online at pycharm.iswbm.com

Python’s Guide to Dark Magic

The Python Guide to Dark Magic is now available on V3.0, packed with over 100 development tips and tricks that are perfect for reading in fragments at leisure.

Online experience: magic.iswbm.com

Python Chinese Guide

The best source for learning Python is always the official Python documentation. Unfortunately, most of the official Python documentation is in English. Although there is a Chinese translation, the progress is rather slow. In order to take care of students who are not good at English, I wrote an online Python document for those who have no basic Knowledge of Python – the Python Chinese Guide.

Experience online at python.iswbm.com

Please give me a thumbs up if ** is helpful