Search This Blog

Tuesday, June 13, 2017

Freeup RAM in Linux System - Linux Basics


            Some times linux Servers/Low memory linux boards tends to low memory conditions. Even though linux has excellent memory management system Some un-closed files, network memeroies and caches  causes out of memory or low memory conditions. By deleting and cleaning pagecache, dentries and inodes we can get the unused memory.

we can check ram usage with free command

root@arm:~# free
                   total         used         free      shared    buffers     cached
Mem:         50204      47352       2852          0       6188      20768
-/+ buffers/cache:      20396      29808
Swap:            0              0              0

here i am having only 2852KB of memory free  out of 64MB RAM.

Option 1: Free pagecache

This is suitable for webserver as they tend to store lots of pagecaches.

To free pagecache:

sync; echo 1 > /proc/sys/vm/drop_caches


(
To free pagecache, dentries and inodes(not recommended)
sync; echo 3 > /proc/sys/vm/drop_caches 
)

after this 

root@arm:~# echo 1 > /proc/sys/vm/drop_caches
root@arm:~# free
                      total       used       free     shared    buffers     cached
Mem:         50204      26412      23792          0         80       6720
-/+ buffers/cache:      19612      30592
Swap:            0                0          0

the free memory is 23MB.

Check Memory Usage in Linux - Linux Basics

Commands to Check RAM/Memory usage in Linux

free               shows usage in KiloBytes

root@arm:~# free
                   total        used           free     shared    buffers     cached
Mem:         50204      45392       4812          0       5740        20464
-/+ buffers/cache:      19188      31016
Swap:            0          0          0

free -b               shows usage in Bytes
free -k               shows usage in KiloBytes
free -m              shows usage in MegaBytes
free -g               shows usage in GigaBytes

RAM Problem in Linux Board - Linux Basics

Some small/low cost linux boards having small RAM size. For example olinux single chip computer boards having 64MB ram only.
for some instalations it will show the error  like " Cannot allocate memory" so we need to increase the RAM size virtually.

GNU/Linux supports swapping to a file, with performance depends on your SD card writing and reading speed. This might be useful whenever needing a temporary increase in available memory,

Create a file (following example would create a 1024x1M=1G swap size) will zero fill

dd if=/dev/zero of=/.swapfile bs=1024 count=1M

it will take minutes depends on flsah/memory card speed

command to Setup a swap area in it:

mkswap /.swapfile

To activate swap to this file (would not survive a reboot):

swapon /.swapfile

To check it did it:

swapon -s

Disable swap to this file:

swapoff /.swapfile