Linux Tips


[an error occurred while processing this directive]

No Title

[an error occurred while processing this directive]

Reference: Cisco: Internetworking Basics


Similar to all Unix-like operating systems, Linux uses a virtual memory system which is an addressing schema that uses non-volatile disk storage (swap space) as an extension of physical RAM. This is used so that the effective size of usable memory can grow as demand for memory increases. A process known as swapping copies inactive pages of RAM to a reserved swap file or swap partition in order to free up physical memory for other user processes. The swap space can contain one or more swap files or swap partitions. The amount of virtual memory available is the sum of the physical RAM and the swap space. If no swap space is available, user processes would be limited to the amount of physical memory available on the machine.

In the past, swap space needed to be configured as a separate swap partition either during installation or later by the system administrator. With Linux kernel 2.6, swap files can be used for swap space and are known to be just as fast as swap partitions. Although many system administrators still recommend using a swap partition, creating swap files is an efficient and quick method to increase the amount of virtual memory on a machine without the need to use a raw device or even worse, reformat a hard disk. For example, swap space may need to be added to increase system performance or to successfully install the Oracle Database software. Provided there is adequate space on an existing file system, the swap space can be increased without the need for an outage by creating a swap file.

This guide provides instructions on how to add additional swap space to a Linux system by adding a swap file using command-line options.

The Linux system used in this example is currently not configured with any swap space.

# cat /proc/meminfo | grep SwapTotal SwapTotal: 0 kB # free -m total used free shared buffers cached Mem: 3840 3756 83 0 7 3529 -/+ buffers/cache: 219 3620 Swap: 0 0 0

Configure Additional Swap Space by Creating a Swap File

Use the dd command to create a new file that will be configured later as a swap file. The following example creates a 4GB file named /mnt/swap/swapfile1.

# dd if=/dev/zero of=/mnt/swap/swapfile1 bs=1k count=4096k 4194304+0 records in 4194304+0 records out 4294967296 bytes (4.3 GB) copied, 80.1842 seconds, 53.6 MB/s

Create a Linux swap area within the swap file.

# mkswap /mnt/swap/swapfile1 Setting up swapspace version 1, size = 4294963 kB

For security purposes, set the appropriate permissions on the swap file. Only root should be able to read and write to the swap file.

# chown root:root /mnt/swap/swapfile1 # chmod 0600 /mnt/swap/swapfile1

Finally, activate the swap file which immediately adds it to the swap space.

# swapon /mnt/swap/swapfile1

Verify the new swap space.

# cat /proc/meminfo | grep SwapTotal SwapTotal: 4194296 kB # free -m total used free shared buffers cached Mem: 3840 3755 84 0 7 3530 -/+ buffers/cache: 217 3622 Swap: 4095 0 4095 # swapon -s Filename Type Size Used Priority /mnt/swap/swapfile1 file 4194296 0 -1

Mount Swap File on Boot

The procedure document above will only enable the new swap file until the next reboot of the system. To activate the swap file after each system reboot, add the following entry to the /etc/fstab file.

/mnt/swap/swapfile1 swap swap defaults 0 0

About the Author

Jeffrey Hunter is an Oracle Certified Professional, Java Development Certified Professional, Author, and an Oracle ACE. Jeff currently works as a Senior Database Administrator for The DBA Zone, Inc. located in Pittsburgh, Pennsylvania. His work includes advanced performance tuning, Java and PL/SQL programming, developing high availability solutions, capacity planning, database security, and physical / logical database design in a UNIX / Linux server environment. Jeff's other interests include mathematical encryption theory, tutoring advanced mathematics, programming language processors (compilers and interpreters) in Java and C, LDAP, writing web-based database administration tools, and of course Linux. He has been a Sr. Database Administrator and Software Engineer for over 20 years and maintains his own website site at: Jeff graduated from Stanislaus State University in Turlock, California, with a Bachelor's degree in Computer Science and Mathematics.

Copyright (c) 1998-2017 Jeffrey M. Hunter. All rights reserved.

All articles, scripts and material located at the Internet address of is the copyright of Jeffrey M. Hunter and is protected under copyright laws of the United States. This document may not be hosted on any other site without my express, prior, written permission. Application to host any of the material elsewhere can be made by contacting me at

I have made every effort and taken great care in making sure that the material included on my web site is technically accurate, but I disclaim any and all responsibility for any loss, damage or destruction of data or any other property which may arise from relying on it. I will in no case be liable for any monetary damages arising from such loss, damage or destruction.

Last modified on
Wednesday, 03-Oct-2012 18:13:46 EDT
Page Count: 6216