1. 程式人生 > >linux 程序記憶體佔用高分析

linux 程序記憶體佔用高分析

通過free命令可以檢視系統記憶體使用情況:

free -m
              total        used        free      shared  buff/cache   available
Mem:           7983        2427        5155          26         400        5272
Swap:          8191          21        8170

buff/cache為快取部分,在統計memory佔用情況時,需要注意這一個值。通過以下命令可以釋放快取:

Writing to this will cause the kernel to drop
clean caches, dentries and inodes from memory, causing that memory to become free. To free pagecache: $ echo 1 > /proc/sys/vm/drop_caches To free dentries and inodes: $ echo 2 > /proc/sys/vm/drop_caches To free pagecache, dentries and inodes: $ echo 3 > /proc/sys/vm/drop_caches As this is a non-destructive operation, and
dirty objects are notfreeable, the user should run "sync" first in order to make sure allcached objects are freed. This tunable was added in 2.6.16.

上面方法需要使用者具有root許可權,如果不是root,但有sudo許可權,可以通過sysctl命令進行設定:

$sync
$sudo sysctl -w vm.drop_caches=3
$sudo sysctl -w vm.drop_caches=0 #恢復預設cache策略

使用這樣的cache策略後,進行信用證的壓力測試,Linux下該程序的記憶體佔用率不再隨時間繼續上升。