here are the codes gcc test.c -std=c99
#include <stdio.h>
#include <stdlib.h>
void main() {
size_t size = (long) 40 * 1024 * 1024 * 1024;
int* buffer = malloc(size * sizeof(int));
for (size_t i = 0; i < size; i++) {
buffer[i] = 1;
}
printf("hello\n");
for (size_t i = 0; i < size; i++) {
buffer[i] = 2;
}
printf("hello\n");
}
160G ram allocated in one go, and traversed twice
the first loop runs happily
however the program kinda stuck inside the 2nd loop
with perf top
showing this
Samples: 7M of event 'cpu-clock', Event count (approx.): 14127849698
74.95% [kernel] [k] change_protection
23.52% [kernel] [k] vm_normal_page
0.40% [kernel] [k] _raw_spin_lock
0.34% [kernel] [k] _raw_spin_unlock
with top
showing this
top - 10:52:36 up 55 min, 4 users, load average: 1.16, 1.18, 1.04
Tasks: 240 total, 2 running, 238 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 3.1%sy, 0.0%ni, 96.9%id, 0.0%wa, 0.0%hi, 0.0%s
Mem: 251913536k total, 170229472k used, 81684064k free, 27820k b
Swap: 0k total, 0k used, 0k free, 352816k cac
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
12825 dapeng 20 0 160g 160g 376 R 100.0 66.6 30:38.55 a.out
and the best part is now this program doesn't answer to kill
command any more
gcc version gcc (GCC) 4.8.2 20140120 (Red Hat 4.8.2-16)
server AWS EC2 r3.8xlarge
Operating System Amazon Linux AMI release 2014.09
This is related question to Why does `change_protection` hog CPU while loading a large amount of data into RAM?