Showing posts with label swap. Show all posts
Showing posts with label swap. Show all posts

Friday, August 21, 2015

what is virtual memory, paging , swap space ?

A process normally runs on physical memory where the memory is divided into sets of pages. A page is a 4kb area of memory and is the basic unit of memory with which both kernel and CPU deal.

There might be a situation when all the pages in physical memory goes full. In such cases all the inactive pages inside physical memory is shifted to the secondary storage or the swap space using the paging technique. By doing this physical memory gets free pages which can again be utilized by new processes. This entire process is termed as swapping.

NOTE: Swapping is a good idea as it gives you an additional space to store data files and programs when your physical memory is out of space but accessing a hard disk is hundred times slower than accessing memory.

Virtual memory is a memory management technique that is implemented using both hardware and software which gives an application program the impression that it has contiguous working memory (an address space).

In simple terms Virtual memory is a logical combination of RAM memory and swap space which is used by running process

NOTE: It is NOT just an additional space used in hard disk to make it act as physical memory

This is one of the memory management technique schemes by which a computer can store and retrieve data from secondary storage for use in main memory.

Swap space
This is a space on the hard disk which is used by the operating system to store data pages that are currently not needed. This swap sapce can be a partition as well as swap file. Generally swap space is double of the RAM .

Amount of RAM in the system recommended amount of swap space
4GB of RAM or less  a minimum of 2GB of swap space
4GB to 16GB of RAM  a minimum of 4GB of swap space
16GB to 64GB of RAM  a minimum of 8GB of swap space
64GB to 256GB of RAM  a minimum of 16GB of swap space

256GB to 512GB of RAM  a minimum of 32GB of swap space

Monday, October 27, 2014

How to Increase Swap Space Size in Linux System

Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory.
To check swap status, use:
# swapon –s
# free –h

Swap partition
A swap partition can be created with most GNU/Linux partitioning tools (e.g. fdisk, cfdisk). Swap partitions are typically designated as type 82, however it is possible to use any partition type as swap.
To set up a Linux swap area, the mkswap command is used. For example:
#mkswap /dev/sda2
The mkswap utility generates an UUID for the partition by default
, use the -U flag in case you want to specify custom UUID:
#mkswap -U custom_UUID /dev/sda2
To enable the device for paging:
# swapon /dev/sda2
To enable this swap partition on boot, add an entry to fstab:
vi /etc/fstab
/dev/sda2    swap    swap    defaults   0  0

Swap file

As an alternative to creating an entire partition, a swap file offers the ability to vary its size on-the-fly, and is more easily removed altogether. This may be especially desirable if disk space is at a premium (e.g. a modestly-sized SSD).

Swap file creation

As root use fallocate to create a swap file the size of your choosing (M = Megabytes, G = Gigabytes) (dd can also be used but will take longer). For example, creating a 512 MB swap file: 

#dd  if=/dev/zero   of=/swapfile  bs=1M   count=512
# chmod 600 /swapfile
After creating the correctly sized file, format it to swap:
#mkswap /swapfile
Activate the swap file:
# swapon /swapfile
Finally, edit fstab to add an entry for the swap file:
vi /etc/fstab
/swapfile    swap    swap    defaults   0  0

Remove swap file

To remove a swap file, the current swap file must be turned off.
As root

# swapoff -a
Remove swap file: 

# rm -f /swapfile