Translate

[Linux] 왜 리눅스의 메모리사용량(used)는 항상 100%에 가까운걸까?





top 명령어의 메모리 used 의미와 실제 메모리 사용량에 대해서..


그렇지 않은 시스템도 있지만 대부분의 리눅스에서 top 명령어를 통해 모니터링을 해보면 메모리사용량(used)는 거의 100% 가까이 사용하는것을 경험상 많이 봐왔다.


인터넷을 찾아봐도 궁금증을 해소시켜줄만한 글을 찾아보지 못하다가 최근에 우연히 실제 메모리사용량과 가용메모리의 진실에 대해서 알게 되었다.




 리눅스의 실제 사용메모리/가용메모리 확인하려면 free 명령어를 통해 확인해야 하며, buffers/cache 부분의 used, free 항목을 기준으로 확인해야 한다.


>$ free -h
             total       used       free     shared    buffers     cached
Mem:          3.7G       2.6G       1.2G       176K       6.4M        31M
-/+ buffers/cache:       2.5G       1.2G
Swap:         3.9G         0B       3.9G




왜 그런지 아래의 실습에서 확인이 가능하다.


1004lucifer@WAS01-DEV01:/home/1004lucifer>$ top
top - 10:53:08 up 108 days, 19:56,  2 users,  load average: 0.13, 0.06, 0.02
Tasks: 145 total,   1 running, 144 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.2%us,  0.2%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   3924392k total,  2705100k used,  1219292k free,     6276k buffers
Swap:  4063228k total,        0k used,  4063228k free,    31940k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  SWAP DATA COMMAND
16924 1004lucifer      20   0 17120 1312  980 R  0.3  0.0   0:00.49    0  496 top
    1 root      20   0 21440 1068  744 S  0.0  0.0   0:01.84    0  404 init
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.00    0    0 kthreadd
    3 root      RT   0     0    0    0 S  0.0  0.0   0:05.99    0    0 migration/0
    4 root      20   0     0    0    0 S  0.0  0.0   0:06.98    0    0 ksoftirqd/0


1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
# 위의 top의 메모리 사용량과 같은것을 볼 수 있다.
# free 명령어는 /proc/meminfo 파일의 정보를 기반으로 보여준다.
#
# [Mem]
#  - total: 전체 메모리
#  - used: 사용 메모리
#  - free: 가용 메모리
#  - shared: Shmem 값 (램디스크처럼 사용되는 tmpfs 사용량)
#  - buffers: Buffer로 사용되는 메모리
#  - cached: Cache로 사용되는 메모리
#
# [-/+ buffers/cache]
#  - used: used - (buffers+cached) : 실제 사용 메모리
#  - free: free + (buffers+cached) : 실제 가용 메모리
#
# [Swap]
#  - total: 스왑 전체 메모리
#  - used: 스왑 사용 메모리
#  - free: 스왑 가용 메모리
1004lucifer@WAS01-DEV01:/home/1004lucifer>$ free -k
             total       used       free     shared    buffers     cached
Mem:       3924392    2707252    1217140        176       6588      31980
-/+ buffers/cache:    2668684    1255708
Swap:      4063228          0    4063228
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$ free -h
             total       used       free     shared    buffers     cached
Mem:          3.7G       2.6G       1.2G       176K       6.4M        31M
-/+ buffers/cache:       2.5G       1.2G
Swap:         3.9G         0B       3.9G
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
# 테스트에 사용할 로그파일 복사
1004lucifer@WAS01-DEV01:/home/1004lucifer>$ cp ~user01/tmp.log ./
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
# 용량은 약 90MB
1004lucifer@WAS01-DEV01:/home/1004lucifer>$ du -sh tmp.log
90M     tmp.log
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
# 90MB파일 복사만 했을뿐인데 cached에 파일의 2배(180MB)정도 용량의 메모리가 늘어남.
1004lucifer@WAS01-DEV01:/home/1004lucifer>$ free -h
             total       used       free     shared    buffers     cached
Mem:          3.7G       2.8G       1.0G       176K       7.8M       210M
-/+ buffers/cache:       2.5G       1.2G
Swap:         3.9G         0B       3.9G
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
# cat 으로 신규파일 생성
1004lucifer@WAS01-DEV01:/home/1004lucifer>$ cat tmp.log >> dump.log
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
# 90MB만큼 cached가 추가로 늘어남.
1004lucifer@WAS01-DEV01:/home/1004lucifer>$ free -h
             total       used       free     shared    buffers     cached
Mem:          3.7G       2.9G       913M       176K       8.0M       299M
-/+ buffers/cache:       2.5G       1.2G
Swap:         3.9G         0B       3.9G
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
# cat으로 파일 덧붙임
1004lucifer@WAS01-DEV01:/home/1004lucifer>$ cat tmp.log >> dump.log
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
# 90MB만큼 cached가 추가로 늘어남.
1004lucifer@WAS01-DEV01:/home/1004lucifer>$ free -h
             total       used       free     shared    buffers     cached
Mem:          3.7G       2.9G       822M       176K       8.1M       389M
-/+ buffers/cache:       2.6G       1.2G
Swap:         3.9G         0B       3.9G
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
# cat으로 여러번 파일 덧붙임
1004lucifer@WAS01-DEV01:/home/1004lucifer>$ cat tmp.log >> dump.log
1004lucifer@WAS01-DEV01:/home/1004lucifer>$ cat tmp.log >> dump.log
1004lucifer@WAS01-DEV01:/home/1004lucifer>$ cat tmp.log >> dump.log
1004lucifer@WAS01-DEV01:/home/1004lucifer>$ cat tmp.log >> dump.log
1004lucifer@WAS01-DEV01:/home/1004lucifer>$ cat tmp.log >> dump.log
1004lucifer@WAS01-DEV01:/home/1004lucifer>$ cat tmp.log >> dump.log
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
# 540MB(90*6) 만큼 cached가 추가로 늘어남.
1004lucifer@WAS01-DEV01:/home/1004lucifer>$ free -h
             total       used       free     shared    buffers     cached
Mem:          3.7G       3.5G       261M       176K       8.1M       925M
-/+ buffers/cache:       2.6G       1.2G
Swap:         3.9G         0B       3.9G
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
# 다시한번 cat으로 여러번 파일 덧붙임
1004lucifer@WAS01-DEV01:/home/1004lucifer>$ cat tmp.log >> dump.log1004lucifer@WAS01-DEV01:/home/1004lucifer>$ cat tmp.log >> dump.log1004lucifer@WAS01-DEV01:/home/1004lucifer>$ cat tmp.log >> dump.log1004lucifer@WAS01-DEV01:/home/1004lucifer>$ cat tmp.log >> dump.log1004lucifer@WAS01-DEV01:/home/1004lucifer>$ cat tmp.log >> dump.log1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
# free 메모리가 150MB남음.
1004lucifer@WAS01-DEV01:/home/1004lucifer>$ free -h
             total       used       free     shared    buffers     cached
Mem:          3.7G       3.6G       150M       176K       1.2M       1.0G
-/+ buffers/cache:       2.6G       1.2G
Swap:         3.9G       400K       3.9G
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
# 다시한번 cat으로 여러번 파일 덧붙임
1004lucifer@WAS01-DEV01:/home/1004lucifer>$ cat tmp.log >> dump.log1004lucifer@WAS01-DEV01:/home/1004lucifer>$ cat tmp.log >> dump.log1004lucifer@WAS01-DEV01:/home/1004lucifer>$ cat tmp.log >> dump.log1004lucifer@WAS01-DEV01:/home/1004lucifer>$ cat tmp.log >> dump.log1004lucifer@WAS01-DEV01:/home/1004lucifer>$ cat tmp.log >> dump.log1004lucifer@WAS01-DEV01:/home/1004lucifer>$
1004lucifer@WAS01-DEV01:/home/1004lucifer>$
# free, cached 메모리의 변화가 거의 없음.
1004lucifer@WAS01-DEV01:/home/1004lucifer>$ free -h
             total       used       free     shared    buffers     cached
Mem:          3.7G       3.6G       150M       176K       944K       1.0G
-/+ buffers/cache:       2.6G       1.2G
Swap:         3.9G       400K       3.9G
1004lucifer@WAS01-DEV01:/home/1004lucifer>$



위와 같이 OS에서 뭔가 작업을 하게되면 OS의 cached부분을 사용하게 되는데 그렇게되면 used 사용량이 증가하며 free 가용량이 줄어들게된다.






한번 올라간 cached는 특별한일이 없다면 줄어들지 않으며..
free 가용량이 거의 없는 상태에서 free + buffers + cached 용량을 넘어서는 작업을 하지 않는이상 스왑메모리를 사용하지 않는다.

free + buffers + cached 용량을 초과하는 1GB이상의 파일을 vi로 열었을때 그제서야 스왑메모리가 사용되는 것을 확인했다.



위와같은 이유 때문에 대부분의 리눅스 시스템에서 top 명령어로 메모리를 확인 시 100%에 가까운 사용량을 보이는 것이었다.



댓글

  1. 그래서 그냥 둬도 괜찮을걸까요?

    답글삭제
    답글
    1. 네~ 저렇게 100% 가 지속된다고 해서 특별히 문제될건 없습니다.
      리눅스 자체에서 알아서 메모리 관리 할텐데요 뭐,,ㅎ

      삭제

댓글 쓰기