Linux memory usage how to know what is used with basic commands

Preamble

Linux memory usage: how to know how many free memory remain on your Linux server ? With the Linux default caching mechanism it might not be so easy to answer this question… Let see with few examples how to sort this out.

/dev/shm device

/dev/shm is a mount point that is created by default on most Linux operating system when you install a new server.

/dev/shm is the Linux implementation of shared memory concept. Shared because multiple programs will access the same memory portion and so sharing at high speed information (much faster than file on standard disks).

shm / shmfs is also known as tmpfs and so is a temporary file storage and even if it appears as a mount point it uses (virtual) memory and not your disk device.

Example of a 4GB /dev/shm:

[root@server1 ~]# df |grep shm
tmpfs                  4089276         0   4089276   0% /dev/shm
[root@server1 ~]# grep shm /etc/fstab
tmpfs                   /dev/shm                tmpfs   defaults        0 0

By default /dev/shm is created to use half size of your server physical memory:

[root@server1 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          7986       7938         48          0         61       2155
-/+ buffers/cache:       5720       2266
Swap:         5012        362       4649

To know if it is in use you can simply list its contain to see if files have been created. So far the only application I have seen using it is Oracle 11gR2 when using MEMORY_MAX_SIZE and MEMORY_TARGET initialization parameters (maybe SAP is also using it, to be verified).

You can obviously change its size with:

[root@server1 ~]# grep shm /etc/fstab
tmpfs                   /dev/shm                tmpfs   defaults,size=2g        0 0
[root@server1 ~]# mount -o remount /dev/shm

You can also dynamically use something like:

[root@server1 ~]# mount -o remount,size=2G /dev/shm

This filesystem is also an open door for hackers attack that’s why many articles on Internet strongly suggest to secure it…

Remark:
For the ones wondering why not using ramfs instead (not swappable) then answer is clear in My Oracle Support (MOS) note 749851.1: not supported by Oracle and Automatic Memory Management (AMM).

I have been dubious on this /dev/shm for a long time and have always considered than the size specified when mounting it was pre-allocated and reserved for further usage: this is not true. Size is effectively allocated only when you really need it (starting an Oracle instance with AMM for example). So in clear no harm to set it half of server memory.

Free command

From Redhat support:

The philosophy in Linux is that an unused resource is a wasted resource. The kernel therefore will use as much RAM as it can to cache information from your local and remote filesystems and disks. This builds up over time as reads and writes are done on the system trying to keep the data stored in RAM as relevant as possible to the processes that have been running on your system. This caching is reported by the system as the sum of two numbers, buffers and pagecache. The cache is reclaimed, not at the time of process exit (you might start up another process soon that needs the same data), but upon demand – i.e. When you start a process that needs a lot of memory to run, the Linux kernel will reclaim memory that had been storing cached data and give it to the new process.
There are some things which get reported as cache which are not directly freeable by the kernel, such as anonymous mmaps and shm regions. These will however, report against all processes attached to them unlike normal cache which is not part of the address space of any running process but is simply a kernel mapping.

For example (Units are in megabytes):

# free -m
             total       used       free     shared    buffers     cached
Mem:          1000        900        100          0        350        350
-/+ buffers/cache:        200        800

In this example, as far as applications are concerned the system is using only 200MB of memory and has 800MB free and available for use if needed:

  • Total Physical Memory = 1000 MB
  • Physically Used Memory = 900 MB
  • Actual used memory = 200 MB
  • buffers = 350 MB
  • cached = 350 MB
  • Physically Free Memory = 100 MB
  • Memory free for Applications = 800MB

Mathematical Calculations:

  • Physically Used Memory = Actual used memory + buffers + cache = 200 + 350 + 350 = 900 MB
  • Physically Free Memory = Total Physical Memory – Actual used memory – buffers – cache = 1000 – 200 – 350 – 350 = 100 MB
  • Memory free for Applications = Total Physical Memory – Actual used memory = 1000 – 200 = 800 MB
  • Memory used by Applications = Physically Used Memory – buffers – cache = 900 – 350 – 350 = 200 MB

Example:

[root@server1 /]# free -mlt
             total       used       free     shared    buffers     cached
Mem:          8007       7819        187          0        232       5804
Low:           711        625         86          0          0          0
High:         7295       7194        101          0          0          0
-/+ buffers/cache:       1782       6225
Swap:         2000          0       2000
Total:       10007       7819       2187

/proc/meminfo file

This file provide detailed information on memory usage, what is not obvious is fact that file is automatically updated so by clubbing it with watch you create an interesting memory monitor:

[root@server1 /]# watch cat /proc/meminfo
Every 2s: cat /proc/meminfo                                                                                                                                                            Tue Mar  8 17:57:54 2011
 
        total:    used:    free:  shared: buffers:  cached:
Mem:  8396120064 8197312512 198807552        0 244072448 6085550080
Swap: 2097405952        0 2097405952
MemTotal:      8199336 kB
MemFree:        194148 kB
MemShared:           0 kB
Buffers:        238352 kB
Cached:        5942920 kB
SwapCached:          0 kB
Active:        3582784 kB
ActiveAnon:    1529756 kB
ActiveCache:   2053028 kB
Inact_dirty:   3175336 kB
Inact_laundry:  801644 kB
Inact_clean:    157720 kB
Inact_target:  1543496 kB
HighTotal:     7470644 kB
HighFree:       105800 kB
LowTotal:       728692 kB
LowFree:         88348 kB
SwapTotal:     2048248 kB
SwapFree:      2048248 kB
CommitLimit:   6147916 kB
Committed_AS:  3295608 kB
HugePages_Total:     0
HugePages_Free:      0
Hugepagesize:     2048 kB

Free and /proc/meminfo reconciliation

[root@server1 ~]# free -m;cat /proc/meminfo
             total       used       free     shared    buffers     cached
Mem:          8007       7817        189          0        232       5804
-/+ buffers/cache:       1780       6226
Swap:         2000          0       2000
        total:    used:    free:  shared: buffers:  cached:
Mem:  8396120064 8197431296 198688768        0 244133888 6086209536
Swap: 2097405952        0 2097405952
MemTotal:      8199336 kB
MemFree:        194032 kB
MemShared:           0 kB
Buffers:        238412 kB
Cached:        5943564 kB
SwapCached:          0 kB
Active:        3581808 kB
ActiveAnon:    1528720 kB
ActiveCache:   2053088 kB
Inact_dirty:   3175740 kB
Inact_laundry:  801884 kB
Inact_clean:    157720 kB
Inact_target:  1543428 kB
HighTotal:     7470644 kB
HighFree:       105744 kB
LowTotal:       728692 kB
LowFree:         88288 kB
SwapTotal:     2048248 kB
SwapFree:      2048248 kB
CommitLimit:   6147916 kB
Committed_AS:  3294440 kB
HugePages_Total:     0
HugePages_Free:      0
Hugepagesize:     2048 kB

MemTotal – MemFree – Buffers – Cached = 8199336 – 194032 – 238412 – 5943564 = 1823328 i.e. 1780MB so memory used of free command.

To go further

[root@server1 ~]# slabtop --sort=c
 Active / Total Objects (% used)    : 798325 / 926847 (86.1%)
 Active / Total Slabs (% used)      : 66730 / 69357 (96.2%)
 Active / Total Caches (% used)     : 48 / 75 (64.0%)
 Active / Total Size (% used)       : 231839.90K / 249148.15K (93.1%)
 Minimum / Average / Maximum Object : 0.01K / 0.27K / 128.00K
 
  OBJS ACTIVE  USE OBJ SIZE  SLABS OBJ/SLAB CACHE SIZE NAME
309911 309911 100%    0.50K  44273        8    177092K inode_cache
316860 316860 100%    0.12K  10562       32     42248K dentry_cache
 82495  29323  35%    0.11K   2357       37      9428K buffer_head
  1678   1558  92%    4.00K   1678        1      6712K size-4096
 44460  16236  36%    0.12K   1482       32      5928K pte_chain
  2098   1378  65%    2.00K   1049        2      4196K size-2048
   821    821 100%    4.00K    821        1      3284K names_cache
  1286   1046  81%    1.38K    643        2      2572K sock
  1124   1124 100%    1.38K    562        2      2248K sighand_cache
  7035   6709  95%    0.25K    469       16      1876K size-256
  3672   2633  71%    0.50K    459        8      1836K size-512
 25578  25518  99%    0.06K    441       64      1764K size-32
 11910   8385  70%    0.12K    397       32      1588K size-128
  1248   1127  90%    1.00K    312        4      1248K size-1024
  3010   1671  55%    0.38K    301       10      1204K nfs_read_data
  6808   2272  33%    0.16K    296       24      1184K skbuff_head_cache
  2051   1821  88%    0.50K    293        8      1172K files_cache
  2660   2164  81%    0.38K    266       10      1064K nfs_write_data
  7710   7381  95%    0.12K    257       32      1028K size-64
  6960   6390  91%    0.13K    240       31       960K sigqueue
  6840   5852  85%    0.12K    228       32       912K tcp_open_request
  6840   3203  46%    0.12K    228       32       912K nfs_page
  2000   1798  89%    0.38K    200       10       800K mm_struct
 10584   8909  84%    0.07K    189       60       756K vm_area_struct
 13167   3269  24%    0.05K    171       85       684K journal_head
  2355   1264  53%    0.25K    157       16       628K ip_dst_cache
  3450   2048  59%    0.12K    115       32       460K blkdev_requests
  4400   2610  59%    0.09K    110       42       440K file_lock_cache
  3030   1875  61%    0.12K    101       32       404K tcp_tw_bucket
    25     20  80%   16.00K     25        1       400K size-16384
  2970   2707  91%    0.12K     99       32       396K filp
  5162   3201  62%    0.06K     89       64       356K signal_cache
  1275    254  19%    0.25K     85       16       340K arp_cache
  4466   3509  78%    0.06K     77       64       308K cdev_cache
    37     11  29%    8.00K     37        1       296K size-8192
  4060   3344  82%    0.06K     70       64       280K fs_cache
  3886   3322  85%    0.06K     67       64       268K pae_pgd
     2      2 100%   64.00K      2        1       128K size-65536
  1798    511  28%    0.06K     31       64       124K bdev_cache
  1798   1263  70%    0.06K     31       64       124K inet_peer_cache
  2576   1883  73%    0.03K     23      128        92K tcp_bind_bucket
  2240   1468  65%    0.03K     20      128        80K revoke_record
  1792   1527  85%    0.03K     16      128        64K uid_cache
  1008     10   0%    0.03K      9      128        36K ip_fib_hash
     1      0   0%   32.00K      1        1        32K size-32768
   112    112 100%    0.24K      7       16        28K kmem_cache
   348     20   5%    0.06K      6       64        24K mnt_cache
  1250      5   0%    0.01K      5      341        20K revoke_table
     0      0   0%    0.06K      0       64         0K size-32(DMA)
     0      0   0%    0.12K      0       32         0K size-64(DMA)

In the above inodes cache (files references, inode_cache) size 172MB and directory names (dentry_cache) size 41MB.

References

About Post Author

One thought on “Linux memory usage how to know what is used with basic commands

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>