와탭랩스 블로그 오픈 이벤트 😃
자세히 보기
Tech
2024-03-06
Checking Memory Status with the Linux Free Command
blog main image

The free command makes it easy to understand how much memory is used, how much is free, and how much is used for caching. The actual free command pulls memory information from Linux /proc/meminfo. Let's take a look at the memory information you can see with the free command and the values of meminfo associated with it.

The free command

blog main image
  • [total] : Total installed memory size / total swap size set up
  • [used] : Memory in use, total minus free, buff/cache. / Swap size in use
  • [free] : Total minus used and buff/cahce, the actual amount of free memory available for use / Unused swap size
  • [shared] : Memory used by tmpfs (memory file system), ramfs, etc. Shared memory that can be used by multiple processes.
  • [buffers]: Memory in use as kernel buffers
  • [cache] : Memory used by page cache and slab
  • [buff/cache] : Memory in use plus buffers and cache
  • [available] : Estimated size of memory that can be allocated by a new process without swapping. (New column with old -/+ buffers/cache gone)

Options

  • [-h] : Print in human-readable units.
  • [-b | -k | -m | -g] : Output in bytes, kibibytes, megabytes, and gibibytes.
  • [--tebi | --pebi] : Outputs in tebibytes and pebibytes.
  • [--kilo | --mega | --giga | --tera | --peta] : Print in kilobytes, megabytes, gigabytes, and petabytes.
  • [-w] : Print cache and buffers separately in wide mode.
  • [-c 'repeat'] : Run free continuously for the specified number of iterations.
  • [-s 'seconds'] : Execute free continuously with a delay of the specified number of seconds.
  • [-t] : Print an additional total column line with the total calculated.

Difference between Cache and Buffer

Linux always tries to use free memory space as buffers and caches. By storing data in memory, it improves performance by reducing access to slow disks as much as possible.

Buffer is the size of the buffer cache caching metadata about the device block in memory. It stores the information needed to read data from the block device in memory.

Cache is the amount of memory being used as a page cache and slab.

  • The page cache stores data that was once read to disk in memory so that when the same data is read again, it can be read directly from memory without a request to disk.
  • A slab is a unit of storage for kernel objects managed by the kernel. The kernel uses memory in slabs, which are smaller units than pages, the unit of application allocation. Multiple slabs can exist on a single page of memory.
  • You can cache the inode or dentry information of a file in a slab.

In fact, if you create a file of about 1M in size and read it, you can see the cache size grow.

blog main image

Cache size before reading files

blog main image

Read a file 1M in size

blog main image

Cache size increased by 1M

More information about the amount of recently unused memory in cache and buffer can be found in /proc/meminfo.

blog main image

The sizes shown above, Inactive(anon), Inactive(file), and SRelaimable, are the sizes that can be moved to the swap area and allocated as new memory because they have been referenced for a long time.

  • Inactive(anon) is the size of the least referenced portion of anonymous memory.
  • Inactive(file) is the size of the least referenced memory in the page cache.
  • SRelaimable is the area of the slab that is available for reallocation.

These values are referenced by the free command to show how much of the data cached in memory can be pushed out to the swap area and freed up for new allocations, with available in the free command. available is only an estimate calculated by the system, and should not be relied upon too heavily.

Swap memory

Swap is a portion of disk that is set aside to be used as memory when memory space is low.

When the kernel runs out of memory, it attempts to free up things that are allocated as buffers and caches that are not used often and allocate them to memory. It also moves data in memory that has not been used recently to SWAP space (SWAP-OUT).

And when the process reads the data moved to SWAP, it brings the data back into memory to read it.(SWAP-IN) This increases latency because it brings data that was moved from disk back into memory, which eventually leads to performance degradation.

You should constantly monitor your SWAPs to determine when you are using a certain amount of SWAP due to a temporary increase in memory usage, and when your SWAPs are growing due to an ongoing memory shortage, and consider when to increase memory.

Wrapping up

Programs that load-balance or distribute workloads also consult /proc/meminfo for information about how much memory can be allocated by emptying the caching area. You can also manually use commands to empty caching. However, proper use of caching can improve system performance, and emptying caching too frequently can lead to performance degradation. Therefore, you should consider setting the kernel parameters for caching appropriately based on the characteristics of your system.

Reference

https://www.usna.edu/Users/cs/aviv/classes/ic221/s16/lec/21/lec.html

https://cs.stackexchange.com/questions/45159/can-someone-explain-this-diagram-about-slab-allocation

와탭 모니터링을 무료로 체험해보세요!