리눅스를 사용하다보면 Chrome, Firefox 브라우저에서 memory 를 많이 사용하여 Aw, Snap! 이라는 화면이 발생하는 경우가 자주 생긴다. 가볍고 빨라서 사용했던 Chrome 이 이렇게 되다니.....
이런 경우 해결 방법은 아래와 같이 2가지가 있다.
1. swap memory 를 늘려 본다.
2. 프로세스의 사용 Memory 의 Limit 를 설정 한다.
1. swap memory 늘리기
여기서 1번의 Swap Memory 를 늘리는 것은 swapoff, swapon, mkswap 등의 명령어로 간단하게 설정이 가능하다. 자세한 방법은 전에 정리한 Linux Swap Memory 늘리기 를 참고 하면 된다. ( 링크 : https://medium.com/@jjeaby/ubunt-uswap-memory-increase-ea4de4266e40 )
2. 프로세스의 사용 Memory 의 Limit 를 설정
프로세스의 사용 Memory 의 Limit 를 설정하기 위해서는 cgroup
이라는 커널 모듈을 사용하면 된다.
cgroup 이란?
cgroup은 단일 또는 태스크 단위의 프로세스 그룹에 대한 자원 할당을 제어하는 커널 모듈입니다.
이제 cgroup 을 이용해서 프로세스의 Memory Limit 를 설정합니다.
1) cgroup 설치
sudo apt-get install cgroup-bin cgroup-lite cgroup-tools cgroupfs-mount libcgroup1
2) /etc/init/cgroup-lite.conf 설정
description "mount available cgroup filesystems"
author "Serge Hallyn <serge.hallyn@canonical.com>"start on mounted MOUNTPOINT=/sys/fs/cgrouppre-start script
test -x /bin/cgroups-mount || { stop; exit 0; }
test -d /sys/fs/cgroup || { stop; exit 0; }
/bin/cgroups-mount
cgconfigparser -l /etc/cgconfig.conf
end scriptpost-stop script
if [ -x /bin/cgroups-umount ]
then
/bin/cgroups-umount
fi
end script
3) /etc/cgconfig.conf 에 limit 할 그룹 설정
group limitcpu{
cpu {
cpu.shares = 400;
}
}group limitmem{
memory {
memory.limit_in_bytes = 512m;
}
}
group browsers {
cpu {
#Set the relative share of CPU resources equal to 25%
cpu.shares = "256";
}
memory {
#Allocate at most 1 GB of memory to tasks
memory.limit_in_bytes = "1G";
#Apply a soft limit of 768 MB to tasks
memory.soft_limit_in_bytes = "768M";
}
}
4) /etc/cgrules.conf 에 Limit 를 설정할 프로세스 설정
[계정]:/opt/google/chrome/chrome cpu,memory browsers
[계정]:/usr/bin/firefox cpu,memory browsers
5) /etc/init.d/cgconf 서비스 등록 및 확인
[서비스 시작 파일 생성]
#!/bin/sh
### BEGIN INIT INFO
# Provides: cgconf
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Configures CGroups
### END INIT INFOstart_service() {
if is_running; then
echo "cgrulesengd is running already!"
return 1
else
echo "Processing /etc/cgconfig.conf..."
cgconfigparser -l /etc/cgconfig.conf
echo "Processing /etc/cgrules.conf..."
cgrulesengd -vvv --logfile=/var/log/cgrulesengd.log
return 0
fi
}stop_service() {
if is_running; then
echo "Stopping cgrulesengd..."
pkill cgrulesengd
else
echo "cgrulesengd is not running!"
return 1
fi
}status() {
if pgrep cgrulesengd > /dev/null; then
echo "cgrulesengd is running"
return 0
else
echo "cgrulesengd is not running!"
return 3
fi
}is_running() {
status >/dev/null 2>&1
}case "${1:-}" in
start)
start_service
;;
stop)
stop_service
;;
status)
status
;;
*)
echo "Usage: /etc/init.d/cgconf {start|stop|restart|status}"
exit 2
;;
esacexit $?
[서비스 등록 및 확인]
# make the script executable
chmod 755 /etc/init.d/cgconf# register the service
update-rc.d cgconf defaults# start the service
service cgconf start# check the status
service cgconf status
이렇게 Swap Memory 를 늘리고 cgroup 을 이용해 프로세스의 메모리 할당 limit 를 설정하면 Chrome, Firefox 를 사용하면서 발생하는 메모리 부족 문제는 크게 해소가 됩니다.