Linux: 2 Ways to Add Swap Space Using dd, mkswap and swapon
===========================================================
Question: I would like to add more swap space to my Linux system. Can you explain with clear examples on how to increase the swap space?
Answer: You can either use a dedicated hard drive partition to add new swap space, or create a swap file on an existing filesystem and use it as swap space.
1. First check system RAM, how much?
free -tm
free -tg
free -g
Swapon command with option -s, displays the current swap space in KB.
# swapon -s
Swapon -s, is same as the following.
# cat /proc/swaps
Output:
# cat /proc/swaps
Filename Type Size Used Priority
/dev/sda2 partition 4192956 0 -1
===========================================================================================
Method 1: Use a Hard Drive Partition for Additional Swap Space:
===============================================================
If you have an additional hard disk, (or space available in an existing disk), create a partition using fdisk command. Let us assume that this partition is called
/dev/sdc1
Now setup this newly created partition as swap area using the mkswap command as shown below.
# mkswap /dev/sdc1
Enable the swap partition for usage using swapon command as shown below.
# swapon /dev/sdc1
To make this swap space partition available even after the reboot, add the following line to the /etc/fstab file.
# cat /etc/fstab
/dev/sdc1 swap swap defaults 0 0
Verify whether the newly created swap area is available for your use.
# swapon -s
# free -tm
# free -g
Note: In the output of swapon -s command, the Type column will say “partition” if the swap space is created from a disk partition.
==================================================================================================================================
Method 2: Use a File for Additional Swap Space:
===============================================
If you don’t have any additional disks, you can create a file somewhere on your filesystem, and use that file for swap space.
The following dd command example creates a swap file with the name “myswapfile” under /root directory with a size of 1024MB (1GB).
# dd if=/dev/zero of=/root/swapmemory bs=1M count=5120 <------- we can increase ram size here instead of 1024=1GB, 2048=2GB, etc......
1024+0 records in
1024+0 records out
# ls -l /root/swapmemory
-rw-r--r-- 1 root root 1073741824 Aug 14 23:47 /root/myswapfile
Change the permission of the swap file so that only root can access it.
# chmod 600 /root/swapmemory
Make this file as a swap file using mkswap command.
# mkswap /root/swapmemory
Setting up swapspace version 1, size = 1073737 kB
Enable the newly created swapfile.
# swapon /root/swapmemory
To make this swap file available as a swap area even after the reboot, add the following line to the /etc/fstab file.
# cat /etc/fstab
/root/swapmemory swap swap defaults 0 0
:wq
Verify whether the newly created swap area is available for your use.
# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 4192956 0 -1
/root/swapmemory file 1048568 0 -2
# free -tm
# free -g
Note: In the output of swapon -s command, the Type column will say “file” if the swap space is created from a swap file.
If you don’t want to reboot to verify whether the system takes all the swap space mentioned in the /etc/fstab, you can do the following, which will disable and enable all the swap partition mentioned in the /etc/fstab
# swapoff -a
# swapon -a
Ref:
http://www.thegeekstuff.com/2010/08/how-to-add-swap-space/
===========================================================
###### How To Test RAM By Using Script ##################
==========================================================
Now, we need to test whether added swam memory working or not without rebooting server.
How to produce high CPU load, memory, I/O or stress test Linux server
2 Memory Regression Perl Scripts for Linux:
===========================================
During performance testing of your application you might want to perform some sort of memory regression testing. This article contains two memory regression scripts that are written in perl which will occupy a specific amount of memory for a certain amount of time, for your testing.
1. Basic Memory Regression Script
Note: Don’t execute this script on any critical system. Be careful while running this script. Don’t give a large memory value to this script. If the amount of memory given is huge or not available, your system might hang.
# vim memtest.pl
# store and validate the command line parameter
$mb = $ARGV[0];
unless ( defined $mb and $mb =~ /^\d+$/ and $mb >= 1) {
die "Usage: $0 <occupy MB>\nEx: $0 100 - occupies 100 MB memory\n"
}
# convert it to bytes.
$b = $mb * 1024 * 1024;
# open in-memory file, and seek to size specified to get memory from OS.
open MEM, '>', \$memfile;
seek MEM, $b - 1, 0;
print MEM 'A';
close MEM;
printf "$mb MB memory is occupied, press ENTER to release: "; <STDIN>;
# till here the memory is occupied by this program.
undef $memfile;
printf "Memory released";
:wq
To execute this script, do the following:
# perl memtest.pl 100 <------- we can add ram load here 200MB=200, 300MB=300
2. Advanced Memory Regression Script:
=====================================
This memtest-adv.pl works the same way as basic script, but you can also specify percentage as an input. When your system has total of 2GB of physical memory, you can specify 25%, which will occupy 500MB of memory for testing.
First argument can be either the amount of memory in MB or percentage of memory to be occupied, where percentage represents the percentage against total primary memory available.
Note: This Perl script occupies approximately the given amount of memory. On the system we tested, it took a maximum of 5MB more. So, do not use this on any critical system. Use this script only on a test system, as the system might hang.
Do ‘vi memtest-adv.pl’ and copy/paste the following perl code to create this file.
# vim memtest-adv.pl
# calculate memory to be occupied from percentage given
sub find_memto_occupy
{
$pc = $_[0];
die "Wrong percentage given $pc\n" if ($pc > 100);
open MEMINFO, '<', '/proc/meminfo' or die "Unable to open /proc/meminfo to find available memory\n";
my $mem = <MEMINFO>;
if ( $mem =~ /^MemTotal:\s+(\d+)\s.*$/ ) {
$mem = $1;
} else {
die "Unable to find the available memory\n";
}
$mem = ( $mem / 100 ) * $pc;
return int($mem / 1024);
}
# main script
{
$num = $ARGV[0];
unless ( defined $num and $num =~ /^\d+%?$/ and $num >= 1) {
die "Usage: $0 <occupy MB>\nEx: $0 100 - occupies 100 MB memory\n"
}
if ( $num =~ /^(\d+)%$/ ) {
# convert percentage to bytes.
$pc = $1;
$mb = find_memto_occupy($pc);
} else {
$mb = $num;
}
$b = $mb * 1024 * 1024;
open MEM, '>', \$memfile;
seek MEM, $b - 1, 0;
print MEM chr(0);
close MEM;
print "$mb MB memory is occupied, press ENTER to release: "; <STDIN>;
undef $memfile;
print "Memory released";
}
:wq
To execute this script, do the following:
# perl memtest-adv.pl 250 <------ we can specify ram here = 250MB,300MB,... 600MB etc.....!!
(or)
# perl memtest-adv.pl 25%
Ref:
www.thegeekstuff.com/2011/11/2-memory-regression-perl-scripts-for-linux/