Linux: How to put a load on system memory?

2019-03-21 11:15发布

I'm working on a small function, that gives my users a picture of how occupied the CPU is. I'm using cat /proc/loadavg, wich returns the wellknown 3 numbers.

My problem is that the CPU doesn't do anything right now, while I'm developing.

Is there a good way to generate som load on the CPU, I was thinking something like makecpudosomething 30, for a load of 0.3 or similar. Does an application like this exist?

Also, are there any way to eat up RAM in a controlled fashion?

Thanks

Michael

标签: linux load cpu
13条回答
Juvenile、少年°
2楼-- · 2019-03-21 11:59

The simplest way I have found to load the RAM (and SWAP) is by using Perl:

my $allocation = "A" x (1024 * 1024 * $ARGV[0]);
print "\nAllocated " . length($allocation) . "\n";
查看更多
Explosion°爆炸
3楼-- · 2019-03-21 12:02

Use "memtester" to do your memory regression tests in Linux.

查看更多
Luminary・发光体
4楼-- · 2019-03-21 12:03

you can stress utility, as it is a workload generator tool designed to subject your system to a configurable measure of CPU, memory, I/O and disk stress.

To run 1 vm stressor using 1GB of virtual memory for 60s, enter:

stress --vm 1 --vm-bytes 1G --vm-keep -t 60s

查看更多
聊天终结者
5楼-- · 2019-03-21 12:06

I took this program and modify the line: mem = 1024*1024*512; //512 mb to say this: mem = 1*1024*1024*1024; //1 GB and compile it.

$ gcc iss_mem.c -o iss_mem

And write a bash wrapper around the compiled version of the C program above. It helps me generate a lot of memory load in my server.

#!/bin/bash
# Author: Mamadou Lamine Diatta
#         Senior Principal Consultant / Infrastructure Architect
# Email:  diatta at post dot harvard dot edu
# --------------------------------------------------------------------------------------
# *************************************************************************************

memsize_kb=`grep -i MemTotal /proc/meminfo | awk '{print $2}'`
MemTotal=$(($memsize_kb*1024))
for i in `seq 1 50`
do

        echo "`date +"%F:%H:%M:%S"` ----------------- Running [ $i ] iteration(s)"
        MemToAlloc=$((1*1024*1204*1204))
            # 1Gb of memory per iss_mem call
        TRESHOLD=$(($MemTotal/$MemToAlloc))
             # We are not supposed to make the system
             # run out of memory
        rand=1000
            # High enough to force a new one
        while (( $rand > $TRESHOLD ))
        do
                rand=$(($RANDOM/1000))
        done
        if [ $rand -eq 0 ]
        then
                rand=1
        fi

        echo `date +"%F:%H:%M:%S"` Running $rand iss_mem in parallel ...
        for j in `seq 1 $rand`
        do
             ${ISSHOME}/bin/iss_mem > /dev/null &
                # NOTE: gcc iss_mem.c -o iss_mem
        done

        sleep 180
        jobs -p
        kill `jobs -p`

        sleep 30


done
# -------------------------------------------------------------------------------------
# *************************************************************************************
查看更多
我想做一个坏孩纸
6楼-- · 2019-03-21 12:07

1G memory

python -c 'a="a"*1024**3;raw_input()'
查看更多
霸刀☆藐视天下
7楼-- · 2019-03-21 12:12
mark@localhost$ time pi 1048576 | egrep '.*total$'

Is a simple benchmarking command that will give your cpu a rousting, post your times :D

查看更多
登录 后发表回答