-
Notifications
You must be signed in to change notification settings - Fork 0
/
ram-eater.c
53 lines (47 loc) · 1.29 KB
/
ram-eater.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int GB_TO_ALLOCATE = 10;
int delay = 0;
if (argc < 2)
{
printf("argument(s) missing:\n\n%s AMOUNT_OF_RAM_IN_GB [DELAY]\n\nif DELAY is provided, it will wait DELAY seconds or infinity if omitted or equal 0\n", argv[0]);
return 1;
}
printf("will allocate %d GB\n", GB_TO_ALLOCATE);
long long int allocated = 0LL;
int inc = 1024 * 1024 * sizeof(char);
int *p;
// Fill a block with random value
int *random_values_filled = (int *)calloc(1, inc);
int rnd = open("/dev/urandom", O_RDONLY);
read(rnd, p, inc);
close(rnd);
for (int i = 0; i < GB_TO_ALLOCATE * 1024; i++)
{
p = (int *)calloc(1, inc);
if (!p)
{
printf("calloc failed!\n");
return 2;
}
// Copy the random values to force allocation
memcpy(p, random_values_filled, inc);
}
printf("allocation done: %d GB.\n", GB_TO_ALLOCATE);
// Wait until killed
if (delay != 0)
{
printf("now wait for %d seconds\n", delay);
sleep(delay);
}
else
{
printf("now wait until killed\n");
select(0, NULL, NULL, NULL, 0);
}
}