DevilKing's blog

冷灯看剑,剑上几分功名?炉香无需计苍生,纵一穿烟逝,万丈云埋,孤阳还照古陵

0%

Go Memory Increase

原文链接

Runtime Go 1.12 significantly improves the performance of sweeping when a large fraction of the heap remains live. This reduces allocation latency immediately following a garbage collection.

(中间省略 2 段不太相关的内容) On Linux, the runtime now uses MADV_FREE to release unused memory. This is more efficient but may result in higher reported RSS. The kernel will reclaim the unused data when it is needed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main() {
char *p = NULL;
const int MB = 1024 * 1024;
while (1) {
p = malloc(100 * MB);
memset(p, 0, 100 * MB);
sleep(1);
}
return 0;
}
1
2
3261164 xxx     20   0 52.375g 0.035t  36524 S 579.1 66.0  16711:10 [打码]       
2321665 xxx 20 0 16.358g 0.016t 569088 S 38.4 1.5 1128:45 ./test
  1. Go 1.12 升级能降低内存分配的延迟,但会导致进程 RSS 变高
  2. 因为 Go 1.12 用 MADV_FREE,会让内核延迟回收内存
  3. 通过在页表中做标记的方式,延迟内存的分配和回收,可以提高内存管理的效率
  4. 可以通过气球来让 OS/Hypervisor 挤占内存,另作他用