forked from julman99/eatmemory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eatmemory.c
98 lines (88 loc) · 2.71 KB
/
eatmemory.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* File: eatmemory.c
* Author: Julio Viera <[email protected]>
*
* Created on August 27, 2012, 2:23 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include <unistd.h>
#if defined(_SC_PHYS_PAGES) && defined(_SC_AVPHYS_PAGES) && defined(_SC_PAGE_SIZE)
#define MEMORY_PERCENTAGE
#endif
#ifdef MEMORY_PERCENTAGE
size_t getTotalSystemMemory(){
long pages = sysconf(_SC_PHYS_PAGES);
long page_size = sysconf(_SC_PAGE_SIZE);
return pages * page_size;
}
size_t getFreeSystemMemory(){
long pages = sysconf(_SC_AVPHYS_PAGES);
long page_size = sysconf(_SC_PAGE_SIZE);
return pages * page_size;
}
#endif
bool eat(long total,int chunk){
long i;
for(i=0;i<total;i+=chunk){
short *buffer=malloc(sizeof(char)*chunk);
if(buffer==NULL){
return false;
}
memset(buffer,0,chunk);
}
return true;
}
int main(int argc, char *argv[]){
#ifdef MEMORY_PERCENTAGE
printf("Currently total memory: %zd\n",getTotalSystemMemory());
printf("Currently avail memory: %zd\n",getFreeSystemMemory());
#endif
int i;
for(i=0;i<argc;i++){
char *arg=argv[i];
if(strcmp(arg, "-h")==0 || strcmp(arg,"-?")==0 || argc==1){
printf("Usage: eatmemory <size>\n");
printf("Size can be specified in megabytes or gigabytes in the following way:\n");
printf("# # Bytes example: 1024\n");
printf("#M # Megabytes example: 15M\n");
printf("#G # Gigabytes example: 2G\n");
#ifdef MEMORY_PERCENTAGE
printf("#%% # Percent example: 50%%\n");
#endif
printf("\n");
}else if(i>0){
int len=strlen(arg);
char unit=arg[len - 1];
long size=-1;
int chunk=1024;
if(!isdigit(unit) ){
if(unit=='M' || unit=='G'){
arg[len-1]=0;
size=atol(arg) * (unit=='M'?1024*1024:1024*1024*1024);
}
#ifdef MEMORY_PERCENTAGE
else if (unit=='%') {
size = (atol(arg) * (long)getFreeSystemMemory())/100;
}
#endif
else{
printf("Invalid size format\n");
exit(0);
}
}else{
size=atoi(arg);
}
printf("Eating %ld bytes in chunks of %d...\n",size,chunk);
if(eat(size,chunk)){
printf("Done, press any key to free the memory\n");
getchar();
}else{
printf("ERROR: Could not allocate the memory");
}
}
}
}