Skip to content

Instantly share code, notes, and snippets.

@d-shimizu
Last active September 21, 2018 02:57
Show Gist options
  • Save d-shimizu/5920547 to your computer and use it in GitHub Desktop.
Save d-shimizu/5920547 to your computer and use it in GitHub Desktop.
malloc()でLinuxの仮想メモリ空間のメモリ確保をし続けるだけのプログラム ref: http://qiita.com/d_shimizu/items/700e2eaf7408e7bc7a4c
### コンパイル
% gcc -o get_vmem_test get_vmem_test.c
### 実行
% ./get_vmem_test
122216 GB
### OS
% cat /etc/redhat-release
CentOS release 6.4 (Final)
### Kernel Version
% uname -r
2.6.32-358.11.1.el6.x86_64
### Memory
% free
total used free shared buffers cached
Mem: 1922564 92680 1829884 0 7156 26016
-/+ buffers/cache: 59508 1863056
Swap: 2097144 11176 2085968
#include <stdio.h>
#include <stdlib.h>
int main() {
char *p;
unsigned long i = 1073741824;
// 文字列変数に1GB(1073741824 B)のメモリを確保し続ける
// メモリを確保できなくなるとmallocはnullを返すため、nullが返されるまで繰り返す
do {
p = (char *)malloc(1073741824);
i=i+1073741824;
} while (p != NULL);
// iの値が確保できたメモリの総量となる(GB単位で出力)
printf("%lu GB \n", i/1024/1024/1024);
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment