# ZRAM SWAP ON - 2GBytes
mknod -m 600 /dev/zram0 b 251 0
echo 2048M > /sys/block/zram0/disksize
mkswap /dev/zram0
swapon -p 100 /dev/zram0
## DISK SWAP ON - 1GBytes
#dd if=/dev/zero of=/swap.bin bs=1024M count=1
#mkswap /swap.bin
#swapon -p 0 /swap.bin
# cat /proc/swaps
Filename Type Size Used Priority
/dev/zram0 partition 2097148 0 100 <= ZRAM swap
/swap.bin file 1048572 0 0 <= Disk swap
# cat /proc/meminfo
...
SwapTotal: 3145720 kB
SwapFree: 3145720 kB
...
$ sudo apt install zram-config ... $ sudo systemctl enable zram-config.service ... $ sudo systemctl start zram-config.service ... => 라즈베리파이에서 Ubuntu 사용중인 경우 다음과 같이 추가 모듈을 설치해야 합니다. $ sudo apt install linux-modules-extra-raspi
swap_tendency = mapped_ratio / 2 + distress + swappiness
= mapped_ratio / 2 + (100 / (2 ^ priority)) + swappiness
= 80% / 2 + (100 / 2 ^ (12 ~ 0)) + 60
= 80% / 2 + (0 ~ 100) + 60
= 40 + (0 ~ 100) + 60
= 100 ~ 200
swap_tendency = mapped_ratio / 2 + distress + swappiness
= mapped_ratio / 2 + (100 / (2 ^ priority)) + swappiness
= 40% / 2 + (100 / 2 ^ (12 ~ 0)) + 80
= 40% / 2 + (0 ~ 100) + 80
= 20 + (0 ~ 100) + 80
= 100 ~ 200
swap_tendency = mapped_ratio / 2 + distress + swappiness
= mapped_ratio / 2 + (100 / (2 ^ priority)) + swappiness
= 20% / 2 + (100 / 2 ^ (12 ~ 0)) + 90
= 20% / 2 + (0 ~ 100) + 90
= 10 + (0 ~ 100) + 90
= 100 ~ 200
참고링크 - 리눅스 스왑 메모리 사용 제어
(http://blog.naver.com/seuis398/70128624124)
#!/bin/sh
###
### Copyright (C) MINZKN.COM
### All rights reserved.
### Author: JAEHYUK CHO <mailto:minzkn@minzkn.com>
###
def_path_proc_devices="/proc/devices"
def_path_dev_zram_name="zram0"
def_path_dev_zram="/dev/${def_path_dev_zram_name}"
def_path_sys_block_zram_size="/sys/block/${def_path_dev_zram_name}/disksize"
def_path_sys_block_zram_reset="/sys/block/${def_path_dev_zram_name}/reset"
def_zram_size="2048M"
def_zram_swap_priority=100
def_zram_major=$(awk '$2 == "zram" {print $1}' "${def_path_proc_devices}")
if [ -z "${def_zram_major}" ]
then
echo "not supported zram."
exit 1
fi
hwport_zram_usage()
{
echo "usage: ${0} <enable|disable|status>"
}
hwport_zram_enabled_check()
{
if grep "^${def_path_dev_zram}\s.*$" "/proc/swaps" > /dev/null
then
return 0
else
return 1
fi
}
hwport_zram_make_block_node()
{
if [ ! -b "${def_path_dev_zram}" ]
then
echo "creating node : ${def_path_dev_zram}"
rm -f "${def_path_dev_zram}" > /dev/null 2>&1
mknod -m 0600 "${def_path_dev_zram}" b "${def_zram_major}" 0
return ${?}
fi
return 0
}
hwport_zram_enable()
{
hwport_zram_make_block_node
if [ "${?}" != "0" ]
then
echo "make zram block node failed !"
return 1
fi
echo 1 > "${def_path_sys_block_zram_reset}"
if [ "${?}" != "0" ]
then
echo "reset zram size failed ! (try zram_size is ${def_zram_size})"
return 1
fi
echo "${def_zram_size}" > "${def_path_sys_block_zram_size}"
if [ "${?}" != "0" ]
then
echo "setup zram size failed ! (try zram_size is ${def_zram_size})"
return 1
fi
mkswap "${def_path_dev_zram}" > /dev/null
if [ "${?}" != "0" ]
then
echo "mkswap failed !"
return 1
fi
swapon -p "${def_zram_swap_priority}" "${def_path_dev_zram}" > /dev/null
if [ "${?}" != "0" ]
then
echo "swapon failed ! (try priority is ${def_zram_swap_priority})"
return 1
fi
echo "enabled (size=${def_zram_size}, priority=${def_zram_swap_priority}, device=${def_path_dev_zram})"
return 0
}
hwport_zram_disable()
{
hwport_zram_make_block_node
if [ "${?}" != "0" ]
then
echo "make zram block node failed !"
return 1
fi
swapoff "${def_path_dev_zram}" > /dev/null
if [ "${?}" != "0" ]
then
echo "swapoff failed !"
return 1
fi
echo 1 > "${def_path_sys_block_zram_reset}"
if [ "${?}" != "0" ]
then
echo "reset zram size failed ! (try zram_size is 0)"
fi
echo "4K" > "${def_path_sys_block_zram_size}"
echo "disabled (device=${def_path_dev_zram})"
return 0
}
case "${1}"
in
"enable"|"start")
hwport_zram_enabled_check
if [ "${?}" == "0" ]
then
echo "zram swap already enabled"
exit 1
fi
hwport_zram_enable
if [ "${?}" != "0" ]
then
echo "zram enable failed !"
exit 1
fi
;;
"disable"|"stop")
hwport_zram_enabled_check
if [ "${?}" != "0" ]
then
echo "zram swap already disabled"
exit 1
fi
hwport_zram_disable
if [ "${?}" != "0" ]
then
echo "zram disable failed !"
exit 1
fi
;;
"status"|"state"|"stat")
hwport_zram_enabled_check
if [ "${?}" != "0" ]
then
echo "zram swap disabled"
else
echo "zram swap enabled"
fi
;;
*)
hwport_zram_usage
;;
esac
# End of hwport_zram_ctrl.sh
https://www.kernel.org/doc/Documentation/blockdev/zram.txt
https://os.korea.ac.kr/publication_papers/domestic_confer/2013_spring_kcc_im.pdf
http://bahndal.egloos.com/532783
https://muritzy.tistory.com/1653https://github.com/StuartIanNaylor/zram-config
https://github.com/StuartIanNaylor/zram-config
http://blog.naver.com/seuis398/70128624124
리눅스 커널에서는 아래와 같은 공식으로 swap_tendency를 계산하는데, 이 swap_tendency 값이 100을 넘어서는 시점부터 스왑을 시작
swap_tendency = mapped_ratio / 2 + distress + swappiness
distress = 100 / (2 ^ priority)
priority = priority 값은 메모리 회수단계를 의미하며 12 -> 0 으로 떨어지면서 회수비중을 높이는 기준 레벨 (즉, 초기값은 12부터 시작)
mapped_ratio = 전체 메모리 사용률을 의미 (만약 전체 16G 중에 12G의 메모리가 사용이 되고 있다면 mapped_ratio 값은 75)
swappiness = 기본값은 60으로 약 80% 사용률 메모리 시점부터 swap 이 발생되는 것이 기본이며 50으로 설정하는 경우 100%되기전까지 스왑을 사용하지 않음.
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 2) into drop_caches.
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# vim: set fileencoding=UTF-8 :
import os
import sys
import ctypes
if __name__ == "__main__":
if sys.version_info[:2] < (3, 3):
libc = ctypes.CDLL(u"libc.so.6")
os.posix_fadvise = libc.posix_fadvise
os.posix_fadvise64 = libc.posix_fadvise64
os.POSIX_FADV_NORMAL = 0
os.POSIX_FADV_RANDOM = 1
os.POSIX_FADV_SEQUENTIAL = 2
os.POSIX_FADV_WILLNEED = 3
os.POSIX_FADV_DONTNEED = 4
os.POSIX_FADV_NOREUSE = 5
with open(sys.argv[1], u"rb") as s_fd:
os.posix_fadvise(s_fd.fileno(), 0, 0, os.POSIX_FADV_DONTNEED)
#include <fcntl.h> int posix_fadvise(int fd, off_t offset, off_t len, int advice); int posix_fadvise64(int fd, off64_t offset, off64_t len, int advice);
https://doc.ubuntu-fr.org/zram