Linux 에서 캐시된 메모리를 어떻게 정리할 수 있을까? 결론적으로 세 가지 방법으로 캐시된 메모리 삭제가 가능하다.

1. 페이지 캐시 해제

2. 해제 가능 오브젝트(dentry, inode) 해제
– dentry에 대해서는 여기(http://unix.stackexchange.com/questions/4402/what-is-a-superblock-inode-dentry-and-a-file)를 참조하자.

3. 해제 가능 오브젝트 + 페이지 캐시 해제

아래는 캐시된 메모리 해제와 관련한 자세한 내용이다.(https://www.kernel.org/doc/Documentation/sysctl/vm.txt)

drop_caches

Writing to this will cause the kernel to drop clean caches, as well as
reclaimable slab objects like dentries and inodes.  Once dropped, their
memory becomes free.

To free pagecache:
	echo 1 > /proc/sys/vm/drop_caches
To free reclaimable slab objects (includes dentries and inodes):
	echo 2 > /proc/sys/vm/drop_caches
To free slab objects and pagecache:
	echo 3 > /proc/sys/vm/drop_caches

This is a non-destructive operation and will not free any dirty objects.
To increase the number of objects freed by this operation, the user may run
`sync' prior to writing to /proc/sys/vm/drop_caches.  This will minimize the
number of dirty objects on the system and create more candidates to be
dropped.

This file is not a means to control the growth of the various kernel caches
(inodes, dentries, pagecache, etc...)  These objects are automatically
reclaimed by the kernel when memory is needed elsewhere on the system.

Use of this file can cause performance problems.  Since it discards cached
objects, it may cost a significant amount of I/O and CPU to recreate the
dropped objects, especially if they were under heavy use.  Because of this,
use outside of a testing or debugging environment is not recommended.

You may see informational messages in your kernel log when this file is
used:

	cat (1234): drop_caches: 3

These are informational only.  They do not mean that anything is wrong
with your system.  To disable them, echo 4 (bit 3) into drop_caches.

실제로 캐시를 줄이는 명령어는 원하는 모드에 따라 아래와 같은 방식으로 입력하면 된다.

$ echo 1 > /proc/sys/vm/drop_caches
$ echo 2 > /proc/sys/vm/drop_caches
$ echo 3 > /proc/sys/vm/drop_caches

다음은 테스트 결과이다. 확실히 캐시된 메모리가 줄어드는 것을 확인할 수 있다.

 

root@mywork:~# free -m
total       used       free     shared    buffers     cached
Mem:          7703       3618       4084        433        309       1239
-/+ buffers/cache:       2069       5633
Swap:         7627          0       7627

root@mywork:~# echo 1 > /proc/sys/vm/drop_caches

root@mywork:~# free -m
total       used       free     shared    buffers     cached
Mem:          7703       2700       5003        432          0        641
-/+ buffers/cache:       2058       5644
Swap:         7627          0       7627

 

참조: http://unix.stackexchange.com/questions/58553/how-to-clear-memory-cache-in-linux

Tags: , , ,

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.