๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ธฐํƒ€

[๋ฆฌ๋ˆ…์Šค] free ๋ช…๋ น์–ด

by applemango2021 2022. 3. 11.

๋ฆฌ๋ˆ…์Šค ์„œ๋ฒ„์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ๊ฒ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์€ ๋ณดํ†ต ์–ด๋–ค ๋ช…๋ น์–ด๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š”์ง€ ์ฐพ์•„๋ดค๋‹ค.
(์˜ˆ์‹œ์— ๋‚˜์˜ค๋Š” ์ˆ˜์น˜๋“ค์€ ์ž„์˜๋กœ ๋„ฃ์€ ์ˆซ์ž๋ผ ํ•ฉ๊ณ„๊ฐ€ ์•ˆ ๋งž๋Š”๋‹ค)

free ๋ช…๋ น์–ด

man free ๋ช…๋ น์–ด๋กœ free๋ช…๋ น์–ด๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์„ค๋ช…์ด ๋‚˜์˜จ๋‹ค.

displays the total amount of free and used physical and swap memory in the system, as well as the buffers and caches used by the kernel. The information is gathered by parsing /proc/meminfo

๋ฌผ๋ฆฌ ๋ฉ”๋ชจ๋ฆฌ์™€ ์Šค์™‘ ๋ฉ”๋ชจ๋ฆฌ์˜ free/used ๊ด€๋ จ๋œ ๋ฉ”๋ชจ๋ฆฌ ์–‘์„ ๋ณด์—ฌ์ค€๋‹ค. ๋˜ํ•œ ์ปค๋„์— ์˜ํ•ด์„œ ์‚ฌ์šฉ๋œ buffers์™€ cache์˜ ์–‘๋„ ๋ณด์—ฌ์ค€๋‹ค. ํ•ด๋‹น ์ •๋ณด๋Š” /proc/meminfo ํŒŒ์ผ์— ๊ธฐ๋ก๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•˜์—ฌ ๋ณด์—ฌ์ค€๋‹ค.

์‹ค์ œ๋กœ /proc ๊ฒฝ๋กœ๋กœ ์ด๋™ํ•ด์„œ meminfo ํŒŒ์ผ์„ ์—ด์–ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋“ค์ด ๋‚˜์˜จ๋‹ค.
์—ฌ๋‹ด์ด์ง€๋งŒ, /proc ๊ฒฝ๋กœ๋Š” ๋””์Šคํฌ์— ์‹ค์ œ๋กœ ์กด์žฌํ•˜์ง€ ์•Š๊ณ  ์ปค๋„๋‹จ์—์„œ ๋งŒ๋“ค์–ด๋‚ธ๋‹ค๊ณ  ํ•œ๋‹ค.(์ฐธ๊ณ )
์ด๊ฒŒ ์ •ํ™•ํžˆ ๋ญ˜ ์˜๋ฏธํ•˜๋Š” ์ง€๋Š” ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค. ์—ฌํŠผ ์•„๋ž˜ ์ •๋ณด๋Š” ์ปค๋„๋‹จ์—์„œ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ผ๊ณ  ์ดํ•ดํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

# /proc/meminfo ํŒŒ์ผ

MemTotal:        8000000 kB
MemFree:         3000000 kB
MemAvailable:    6000000 kB
Buffers:          160000 kB
Cached:          1900000 kB

์ถ”๊ฐ€๋กœ ์Šค์™‘ ๋ฉ”๋ชจ๋ฆฌ๋Š”, ์‚ฌ์‹ค์€ ํ•˜๋“œ๋””์Šคํฌ ์šฉ๋Ÿ‰์ธ๋ฐ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•  ๋•Œ ๋Œ์–ด๋‹ค ์“ฐ๋Š” ํ•˜๋“œ๋””์Šคํฌ ์šฉ๋Ÿ‰์ด๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.
ํ•˜๋“œ ๋””์Šคํฌ์˜ ์šฉ๋Ÿ‰๊นŒ์ง€ ๋Œ์–ด๋‹ค ์“ด๋‹ค๋Š” ๊ฑด ๋ฌผ๋ฆฌ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ฑฐ์˜ ๋‹ค ์“ฐ๊ณ  ์žˆ๋‹ค๋Š” ๋œป์ด๊ธฐ ๋•Œ๋ฌธ์—,
๋ญ”๊ฐ€ ํ•œ๋ฒˆ์€ ์ข€ ์ž์„ธํžˆ ๋“ค์—ฌ๋‹ค ๋ด์•ผ ํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.
ํ•˜์ง€๋งŒ ๋ฌผ๋ฆฌ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋‹ค ์“ฐ๊ณ  ๋‚˜์„œ๋งŒ swap ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋„ ์•„๋‹ˆ๋ผ๊ณ  ํ•œ๋‹ค.


๊ฒฐ๊ณผ ํ•ด์„ํ•˜๊ธฐ

$ free
              total        used        free      shared  buff/cache   available
Mem:        8000000     4810000       90000         900     3100000     3180000
Swap:       4000000           0     4000000

$ free -h #human-readable ์˜ต์…˜(-h)์„ ์ฃผ๋ฉด ๋‹จ์œ„๊นŒ์ง€ ์ž˜ ํ‘œ์‹œํ•ด์ค€๋‹ค
              total        used        free      shared  buff/cache   available
Mem:           7.6G        4.5G         90M        900K        3.0G        3.0G
Swap:          4.0G          0B        4.0G

free ๋ช…๋ น์–ด์˜ ๊ฒฐ๊ณผ๋ฌผ์€ ์ง๊ด€์ ์ด๋‹ค.
๋ฌผ๋ฆฌ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰๊ณผ ์Šค์™‘ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. Mem์€ ๋ฌผ๋ฆฌ ๋ฉ”๋ชจ๋ฆฌ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ ,
Swap์€ ์Šค์™‘ ๋ฉ”๋ชจ๋ฆฌ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.

  • total : ์ด ํ•ฉ๊ณ„

  • used : ํ˜„์žฌ ์‚ฌ์šฉ์ค‘์ธ ๋ฉ”๋ชจ๋ฆฌ ํฌ๊ธฐ

    • used = total - free - buff/cache
    • (ex) Mem์˜ used : 8000000 - 90000 - 3100000
  • free : ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ž”์—ฌ ๋ฉ”๋ชจ๋ฆฌ ํฌ๊ธฐ

    • /proc/meminfo์˜ MemFree/SwapFree ํ•ญ๋ชฉ
  • shared

    • man free์— ๋”ฐ๋ฅด๋ฉด tmpfs์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”๋ชจ๋ฆฌ์˜ ํฌ๊ธฐ๋‹ค (tmpfs์— ๋Œ€ํ•ด์„œ๋Š” ์•„๋ž˜์—์„œ ๋‹ค๋ฃฌ๋‹ค)

    • /proc/meminfo์˜ Shmem ํ•ญ๋ชฉ

    • kernels 2.6.32 ์ด์ƒ์˜ ๋ฒ„์ „๋ถ€ํ„ฐ ์ธก์ •๊ฐ€๋Šฅํ•˜๋ฉฐ, ์ธก์ •์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์—๋Š” 0์œผ๋กœ ํ‘œ๊ธฐ๋œ๋‹ค

  • buff/cache : ์ปค๋„์ด ์‚ฌ์šฉํ•˜๋Š” ์บ์‹œ์˜์—ญ์˜ ๋ฉ”๋ชจ๋ฆฌ ํฌ๊ธฐ

    • buff : /proc/meminfo์˜ Buffers ํ•ญ๋ชฉ
    • cache : page cache + slabs์˜ ํฌ๊ธฐ์ด๋ฉฐ, /proc/meminfo์˜ Cached์™€ Slab ํ•ญ๋ชฉ์˜ ํ•ฉ
  • available

    • man free์˜ ๋‚ด์šฉ์„ ์ฝ์–ด๋ณด๋Š” ๊ฒŒ ์ข‹๋‹ค

      Estimation of how much memory is available for starting new applications, without swapping. Unlike the data provided by the cache or free fields, this field takes into account page cache and also that not all
      reclaimable memory slabs will be reclaimed due to items being in use (MemAvailable in /proc/meminfo, available on kernels 3.14, emulated on kernels 2.6.27+, otherwise the same as free)

    • ์ด ๋‚ด์šฉ์„ ๋ช…ํ™•ํžˆ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์šฐ์„  page cache์™€ slab์— ๋Œ€ํ•ด์„œ ์•Œ์•„์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค.

    • ํ•˜์ง€๋งŒ ๋Œ€๋žต์ ์œผ๋กœ๋งŒ ์ดํ•ดํ•˜์ž๋ฉด ๋ง ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ๋ฉ”๋ชจ๋ฆฌ์˜ ์šฉ๋Ÿ‰์œผ๋กœ ๋ณด๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค.
      free์™€ ์–ด๋–ค ์ฐจ์ด์ ์ด ์žˆ์„๊นŒ ํ–ˆ๋Š”๋ฐ, free๋Š” ํ˜„์žฌ ์ง„์งœ ๋†€๊ณ  ์žˆ๋Š”(์•„๋ฌด๊ฒƒ๋„ ํ•˜์ง€ ์•Š๋Š”) ๋ฉ”๋ชจ๋ฆฌ์˜ ์šฉ๋Ÿ‰์ด๊ณ ,available์€ ํ˜„์žฌ ๋ฌด์–ธ๊ฐ€๋ฅผ ํ•˜๊ณ  ์žˆ๊ธฐ๋Š” ํ•˜์ง€๋งŒ ์ƒˆ๋กœ์šด ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋Œ์•„๊ฐ€๊ธฐ ์‹œ์ž‘ํ•  ๋•Œ ๊ทธ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์œ„ํ•ด ์ฐจ์ถœ๋  ์ˆ˜ ์žˆ๋Š” ๋ฉ”๋ชจ๋ฆฌ๋ผ๊ณ  ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์ฐจ์ถœ๋  ์ˆ˜ ์žˆ๋Š” ๋ฉ”๋ชจ๋ฆฌ๋Š” ๋Œ€๊ฐœ buffer/cache์— ํ•ด๋‹น๋˜์—ˆ๋˜ ๋ฉ”๋ชจ๋ฆฌ๋‹ค. (ํ•˜์ง€๋งŒ buffer/cache์˜ ๋ชจ๋“  ๋ฉ”๋ชจ๋ฆฌ๋“ค์ด availableํ•œ ๋ฉ”๋ชจ๋ฆฌ๋กœ ์นด์šดํŠธ๋˜์ง€๋Š” ์•Š๋Š”๋‹ค)

    • ์ถ”๊ฐ€๋กœ, free์˜ ์šฉ๋Ÿ‰์ด ๋„ˆ๋ฌด ์—†์–ด๋„ ๋ฌธ์ œ๊ฒ ์ง€๋งŒ ๋„ˆ๋ฌด ์ปค๋„ ๋ฌธ์ œ๋‹ค. ๊ทธ๋งŒํผ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋†€๊ณ  ์žˆ๋‹ค๋Š” ๋œป์ด๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋ฒ„์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋น„ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.


tmpfs๋ž€?

tmpfs๋Š” Temporary File System์˜ ์•ฝ์ž๋‹ค. ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ์ž„์‹œ ํŒŒ์ผ์‹œ์Šคํ…œ์ด๋ฉฐ, ๊ทธ๋ง์ธ์ฆ‰์Šจ ๋””์Šคํฌ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ํŒŒ์ผ ์‹œ์Šคํ…œ์ด ์•„๋‹ˆ๋ผ, ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐ€์žˆ๋Š” ํŒŒ์ผ์‹œ์Šคํ…œ์ด๋ผ๋Š” ๋œป์ด๋‹ค. ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋ฒ„๋ฅผ ๋‚ด๋ฆฌ๋ฉด tmpfs์— ์ €์žฅ๋˜์–ด ์žˆ๋˜ ๋‚ด์šฉ๋“ค์€ ๋ชจ๋‘ ํœ˜๋ฐœ๋œ๋‹ค.

tmpfs์— ์ƒ์„ฑ๋˜๋Š” ํŒŒ์ผ๋“ค์€ ์ผ๋‹จ ๋Œ€๋ถ€๋ถ„์€ ๋ฆฌ๋ˆ…์Šค์—์„œ ๋””ํดํŠธ๋กœ ์„ค์ •ํ•ด๋†“์€ ๋ช‡๋ช‡ ๊ฒฝ๋กœ๋“ค์— ์ƒ๊ธด ํŒŒ์ผ๋“ค์ด๋‹ค. /run ๋“ฑ์˜ ๊ฒฝ๋กœ์— ์ƒ๊ธด ํŒŒ์ผ๋“ค์€ tmpfs์— ์ €์žฅ๋œ๋‹ค.์™œ ๊ตณ์ด ๋‚ ๋ผ๊ฐˆ ์—ฌ์ง€๊ฐ€ ์žˆ๋Š” ๊ณณ์— ํŒŒ์ผ์„ ๋งŒ๋“œ๋Š” ๊ฑธ๊นŒ ์‹ถ์€๋ฐ, ์•„๋งˆ ์ ‘๊ทผ ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค๋Š” ์žฅ์ ์ด ๊ฐ€์žฅ ํฐ ์ด์œ ์ผ ๋“ฏ ํ•˜๋‹ค.

๋‹ค๋ฅธ ๊ธ€๋“ค์„ ์ฝ์–ด๋ณด๋ฉด free ๋ช…๋ น์–ด์˜ shared์—๋Š” ์ฃผ๋กœ ํ”„๋กœ์„ธ์Šค๋“ค ์‚ฌ์ด์—์„œ ๊ณต์œ ๋˜๋Š” ๋ฉ”๋ชจ๋ฆฌ์˜ ํฌ๊ธฐ๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค๊ณ  ํ–ˆ๋‹ค.
(๊ทผ๋ฐ ์‚ฌ์‹ค ๊ณต์œ ๋˜๋Š” ๋ฉ”๋ชจ๋ฆฌ๋ผ๋Š” ๊ฒŒ ์ •ํ™•ํžˆ ๋ฌด์Šจ ์˜๋ฏธ์ธ์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค.)
์—ฌํŠผ ์กฐ๊ธˆ ๋” ์ •ํ™•ํžˆ ์ด์•ผ๊ธฐํ•ด์„œ shared๋Š” tmpfs์— ์˜ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”๋ชจ๋ฆฌ์˜ ํฌ๊ธฐ๋ผ๊ณ  ๊ธฐ์–ตํ•˜๋Š” ๊ฒŒ ์ข‹์•„ ๋ณด์ธ๋‹ค.


์ฐธ๊ณ