메모리 사용량 확인 (free, top, sar, vmstat, dstat) :: 행복한 프로그래머

posted by 쁘로그램어 2018. 5. 30. 21:07

리눅스에서 메모리 사용량을 확인하는 방법이다.


# Ubuntu or CentOS 6이하 버전 free

$ free -m

             total       used       free     shared    buffers     cached

Mem:        245998      24545     221453         83         59        541

-/+ buffers/cache:      23944     222053

Swap:            0          0          0 


# CentOS 7.X free

CentOS7에서 free 명령에 -/+ buffers/cache이 출력되지 않는다.

Refer: https://www.centos.org/forums/viewtopic.php?t=54403


[root@private6 ~]# free -m

              total        used        free      shared  buff/cache   available

Mem:         31934       26963        1604        1308        3366        2873

Swap:         32767        5625       27142


* total:  31934MB는 물리적인 메모리 총 사이즈

* used: 26963M은 실제 사용한 메모리 (단, buffer, cache 메모리 미포함)

* free: 1604M은 남은 메모리 사이즈 (단, buffer, cache 메모리 포함되어 있음)


리눅스에서는 실행중인 어플리케이션에서 요구하는 메모리를 제외한 대부분의 메모리를 디스크 캐쉬로 사용한다.

free(1604M)이 작지만 buff/cached(3366M) 메모리는 어플리케이션에서 메모리가 필요한 경우 반환이 가능하다.

따라서 리눅스에서 가용이 가능한 메모리는 free + buffers + cached이다.


# top과 free에서 available 메모리 (man free: available)

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)


# 참고 명령어

$ sar -r 1

$ free -k; cat /proc/meminfo | grep Mem; top -n1 | grep "KiB Mem"; vmstat -s | grep memory;

$ dstat --time --cpu --mem --net --disk


# 참고 스크립트

#!/bin/bash


while :

do

    TOTAL=`free | grep ^Mem | awk '{print $2}'`

    USED=`free | grep ^Mem | awk '{print $3}'`

    FREE=`free | grep ^Mem | awk '{print $4}'`

    BUFFER=`free | grep ^Mem | awk '{print $6}'`


    USAGE=`echo "100-($FREE/$TOTAL*100)" | bc -l`

    USED=`echo "$USED/$TOTAL*100" | bc -l`

    ACTUAL=`echo "($TOTAL-$FREE-$BUFFER)/$TOTAL*100" | bc -l`


    echo USAGE=${USAGE:0:5}% USED=${USED:0:5}% ACTUAL=${ACTUAL:0:5}%

    sleep 1;

done 


# 메모리 사용량 (free, top, sar, dmstat, vmstat, /proc/meminfo)



※ 참고 사이트※

https://zetawiki.com/wiki/%EB%A6%AC%EB%88%85%EC%8A%A4_%EB%A9%94%EB%AA%A8%EB%A6%AC_%EC%82%AC%EC%9A%A9%EB%A5%A0_%ED%99%95%EC%9D%B8

https://zetawiki.com/wiki/%EB%A6%AC%EB%88%85%EC%8A%A4_%EB%AA%85%EB%AA%A9%EB%A9%94%EB%AA%A8%EB%A6%AC%EC%82%AC%EC%9A%A9%EB%A5%A0,_%EC%8B%A4%EC%A7%88%EB%A9%94%EB%AA%A8%EB%A6%AC%EC%82%AC%EC%9A%A9%EB%A5%A0

https://zetawiki.com/wiki/%EB%A6%AC%EB%88%85%EC%8A%A4_%EB%A9%94%EB%AA%A8%EB%A6%AC_%EC%82%AC%EC%9A%A9%EB%9F%89%EC%88%9C_%ED%94%84%EB%A1%9C%EC%84%B8%EC%8A%A4_%EB%B3%B4%EA%B8%B0

http://bloodguy.tistory.com/entry/Linux-%EC%8B%9C%EC%8A%A4%ED%85%9C%ED%94%84%EB%A1%9C%EC%84%B8%EC%8A%A4-%EB%A9%94%EB%AA%A8%EB%A6%AC-%EC%82%AC%EC%9A%A9%EB%9F%89-%ED%99%95%EC%9D%B8-check-systemprocess-memory-usage


http://mclee.tistory.com/340

http://tigerbum.tistory.com/44

https://www.howtoforge.com/linux-free-command/

http://mkil.tistory.com/402

http://tod2.tistory.com/124

http://klero.tistory.com/entry/linuxfreememory

https://www.linuxatemyram.com/

http://codedragon.tistory.com/5139

http://dollipolly.tistory.com/entry/Linux-%EB%A6%AC%EB%88%85%EC%8A%A4-%EB%A9%94%EB%AA%A8%EB%A6%AC-%EC%82%AC%EC%9A%A9%EB%9F%89Memory-Usage-%ED%99%95%EC%9D%B8-%EC%8B%A4%EC%A0%9C-%EB%A9%94%EB%AA%A8%EB%A6%AC-%EC%82%AC%EC%9A%A9%EB%9F%89