-
Swapfile은 무엇이길래 메모리 부족을 해결해주는가?인프라/linux 2024. 4. 18. 20:45
EC2 프리티어를 사용하다 보니 메모리 부족을 종종 만나게 됩니다.
사실은.. 이 상황을 만날 때마다 찾아보는 것에 지쳐서 적어두려고 합니다. [ 공식문서 ]
스왑파일 설정하는 김에 스왑파일이 무엇인지도 한번 짚어봅니다.
1. Swapfile은 무엇이길래 메모리 부족을 해결해주는가?
아주 간단하게 이야기하면 메모리에 빈 자리가 없을 때 안 쓰이는 대상을 잠깐 치워두는 공간 정도로 설명할 수 있을 것 같습니다.
아래에 제가 이해하고 있는 메모리와 swapfile의 사용을 간단하게 도식화 해두었습니다.
2. Swapfile 할당하기
스웹 파일은 전통적으로 메모리의 2배를 사용하는 것을 권장한다고 하는 듯 합니다. 실수로 4배를 할당한 김에, 잘못 할당된 것을 정리하고 다시 할당하는 과정을 해보겠습니다 ㅎㅎ
4GB를 할당하는 명령어입니다.
// 4GB 할당하는 명령어 (128 x 32 = 4G) $ sudo dd if=/dev/zero of=/swapfile bs=128M count=32
잘 삭제되었는지 확인하기
$ ls -l /swapfile // No such file or directory (스왑파일이 없는 경우) or $ free -h
정리가 되었다면 다시 2GB를 사용하는 스왑파일을 생성합니다.
$ sudo dd if=/dev/zero of=/swapfile bs=128M count=16
스왑 파일 권한부여
$ sudo chmod 0600 /swapfile
스왑 파일 포맷하기
$ sudo mkswap /swapfile
스왑 파일 활성화 하기
$ sudo swapon /swapfile
최종 확인
$ free -h
감사합니다.
[참고 자료]
https://repost.aws/ko/knowledge-center/ec2-memory-swap-file
'인프라 > linux' 카테고리의 다른 글
yum update와 OOM (1) 2024.04.18 자주 사용하는 리눅스 명령어 정리 (0) 2024.04.18 RHEL에서 port에 실행 중인 프로세스 확인하기 (0) 2024.04.12 scp - secured copy protocol (0) 2024.03.22 Linux에서 사용자에 따른 환경변수 설정 (0) 2024.03.17