SWAP usage simulation

can you suggest me how to simulate an high level of swap usage?
Each action I perform does not get that point, just ram usage.


Out of curiosity, could you give a reason why? If you have tested your
applications and system and have enough RAM to service things without swap
(always the best way) then that’s probably the best test, assuming it is
accurate. Using swap will pretty much just make things work as before,
but slower.

If you really want to force swap to be used there is the vm.swappiness
option that you can configure with sysctl, or directly via the /proc
filesystem. Setting to zero means to avoid swappiness at almost all
costs, and setting to 100 means to swap a lot. The default is, I believe,
sixty. sysctl can be used to set things permanently, but running the
following as ‘root’ should set it on the fly for you:


echo 100 > /proc/sys/vm/swappiness

Feel free to see the current setting, before and after the command above,
using the following:


cat /proc/sys/vm/swappiness

sysctl options, to make this permanent, can be changed within Yast.

Good luck.

Hi Stefano,

in addition to ab’s response, there’s the question on what you define as “high swap usage”: Just allocation of swap space, or constant swapping?

When memory above physical memory is requested, the system will start to swap out to disk. You could simulate this with a simple program allocating memory, or by i.e. opening “vim” with a pretty large file.

But what then? If you just let the application sit there, then once everything is swapped out, no more swap activity is required and the system is no longer influenced. So, if you need constant swapping, you’ll have your test program access the memory pages constantly, i.e. by accessing each allocated memory position in a cycle or, in case of the “editor test”, by searching through the opened file for some non-existent pattern (causing the editor to access all parts of the document).

Of course, once the “memory hog” application terminates, the system will swap in pages based on access patterns and system settings. If you want to maximize that activity, just turn off swap via the “swapoff” command - that will make the memory manager immediately swap in everything that’s currently swapped out.

The actual usage pattern really makes a difference and is related to what your production applications are like… heavy swap-in/out when (and right after) starting the application and/or swapping pages by active applications.


Hi all,
yes the request may be quite “strange”, but it due to an alarm test.
I’ve to test few kind of alarm sent from a “system-watcher” in particular situation.
One of these is a swap usage bigger than x%.

The swap monitored is the one get from the “swapon -s” output.


Hmm… didn’t know you could see it there; that’s nice. For an
overall view (in case you have multiple swap partitions/files, and to
avoid any confusion about needing ‘root’ privs to run it) ‘free’ gives
swap usage too. I do not know how they compare, but there you go.


free | tail -1

First field it total, second is used, third is free/unused.

Good luck.

I’ve a swap partition. I’ll check the differences from free and onswap.


Commands to perhaps see this quickly/easily (to be run as ‘root’):


mkdir -p /root/swapfiles

#Create a swap file
dd if=/dev/zero of=/root/swapfiles/0.swap count=10000 bs=1024

#Create another swap file
dd if=/dev/zero of=/root/swapfiles/1.swap count=10000 bs=1024

#fix up permissions:
chmod 600 /root/swapfiles/*

#Format the swap files as, well, swap files
mkswap /root/swapfiles/0.swap
mkswap /root/swapfiles/1.swap

#Add the files to /etc/fstab but this shouldn’t really be necessary
#as long as the swapon command is given the file as an argument
#directly, so commenting them out for now.
#echo ‘/root/swapfiles/0.swap swap swap defaults 0 0’ >> /etc/fstab
#echo ‘/root/swapfiles/1.swap swap swap defaults 0 0’ >> /etc/fstab

swapon /root/swapfiles/0.swap
swapon /root/swapfiles/1.swap

The result on my system from ‘free’ and ‘swapon -s’ is shown below:


#before adding swap files, ‘free’ output regarding swap:
Swap: 753660 452896 300764

#and after:
root@mybox:~# free
total used free shared buffers cached
Mem: 3089384 3008496 80888 0 141284 272940
-/+ buffers/cache: 2594272 495112
Swap: 773652 458720 314932

root@mybox:~# swapon -s
Filename Type Size Used
/dev/sda1 partition 753660 458720 -1
/root/swapfiles/0.swap file 9996 0 -2
/root/swapfiles/1.swap file 9996 0 -3

So you can see that ‘free’ shows a total where ‘swapon -s’ shows
individuals. That’s interesting information, but I think you may be
best-suited using ‘free’ to get a total so you’re not writing logic to
check multiples should they ever exist.

Good luck.

Hi Stefano,

I’ve to test few kind of alarm sent from a “system-watcher” in particular situation.
One of these is a swap usage bigger than x%.

then opening a sufficiently large file in “vi” should be helpful. IIRC “vi” loads the file into memory, thus using swap space to spill over.

It might be a good idea to run that test with a significantly lower swap size than the (later) production setting, thus reaching the trigger percentage much earlier without needing the extra time to swap 10 terabytes of “blanks” to the swap disk :wink:


and btw: “configure / administer” might have been a better place for this question… the hardware sub-forum is more about supporting specific hardware within the OS.