-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMemCleaner.c
173 lines (134 loc) · 4.13 KB
/
MemCleaner.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <Windows.h>
#define CHUNKS 8
#define B_TO_KB(b) (b/1024)
typedef unsigned long long ULL;
ULL getMem(void);
char cleanMem(ULL freeMemInB);
void printUsage(char **argv);
// Main function
int main(int argc, char **argv) {
char exitCode = -1;
printf("Josemi's MemCleaner\n"
"https://github.com/josemirm/MemoryCleaner\n");
ULL avail = getMem();
// Only compile these lines if the machine is a 32-bit one
#ifndef _WIN64
const ULL MAX_4GB_IN_KB = (uint32_t) -1;
if (avail > MAX_4GB_IN_KB) {
printf("\nWARNING: For this amount of memory you should be using the 64-bit version of this program\n");
char cont = 0;
do {
printf("Do you want to continue? (y/n): ");
fflush(stdout);
cont = tolower(getchar());
getchar(); // This is to remove the '\n' after entering the key
} while (cont != 'n' && cont != 'y');
if (cont == 'n') {
return 0;
}
}
#endif
if (argc > 1) {
if (argv[1][0] == '-') {
switch (argv[1][1]) {
// Clean a determined percentage of memory
case 'p': {
int perc = atoi(argv[1] + 3);
if (perc < 1 || perc > 100) {
printf("%i is an invalid percentage.\n", perc);
printUsage(argv);
exit(-1);
} else {
printf("%i%% of the memory will be cleaned.\n\n", perc);
avail /= 100;
avail *= perc;
exitCode = cleanMem(avail);
if (exitCode != 0) {
printf("Warning: The program was not available to clean all the memory requested.\n");
}
}
break;
}
// Only shows free memory (already done previously)
case 'f':
exitCode = 0;
break;
// Show help
case 'h':
exitCode = 0;
default:
printUsage(argv);
}
} else {
printUsage(argv);
}
} else {
printf("No options selected.\n\n80%% of the available memory will be cleaned\n\n");
avail /= 10;
avail *= 8;
exitCode = cleanMem(avail);
}
return exitCode;
} // int main()
// Prints help
void printUsage(char **argv) {
printf("\nUsage: %s -p <percentage from 1 to 100 of memory to clean>\n"
"Use %s -h to show this help.\n"
"Use %s -f to get the free memory available.\n\n", argv[0], argv[0], argv[0]);
} // void printUsage()
// Returns the amount of free memory and prints it on screen
ULL getMem(void) {
ULL phys;
MEMORYSTATUSEX mem;
mem.dwLength = sizeof(mem);
GlobalMemoryStatusEx(&mem);
printf("\nPhysical memory used (percentage): %ld%%\n", mem.dwMemoryLoad);
phys = mem.ullAvailPhys;
printf("Physical memory available: \t%llu KB\n", B_TO_KB(phys));
return phys;
} // unsigned long long getMem()
// Clean the given amount of memory
char cleanMem(ULL freeMem) {
if (freeMem < 1) {
fprintf(stderr, "\n 0 KB available. No memory to clean\n");
return -1;
}
char ret = 0;
printf("\n%llu KB of the available memory will be cleaned up\n", B_TO_KB(freeMem));
printf("Occupying memory...\n");
// The amount of memory to use would be divided in chunks
char** ptr = (char**) calloc(CHUNKS, sizeof(char*));
if (ptr) {
ULL chunkSize = freeMem / CHUNKS;
int i;
for (i = 0; i < CHUNKS; ++i) {
ptr[i] = calloc(1, chunkSize);
// When there isn't available memory, the pointer returned is null. In
// this case memory allocation must stop to start freeing memory.
if ( !(ptr[i]) ) {
ret = -1;
fprintf(stderr, "Error: Only allocated %llu KB of RAM", chunkSize * i);
break;
}
// Accessing some part of the memory, so the compiler won't optimize it
ptr[i][chunkSize - 1] = 0xFF;
}
// This is to being able to watch the process in the Resourse Monitor
Sleep(1000);
printf("Freeing memory...\n");
// If this try to free a non allocated part of memory (that would have 0x0 value due to
// using "calloc") it won't have any kind of problem. Trying to free a null pointer have
// no effect.
for (i=0; i < CHUNKS; ++i) {
free(ptr[i]);
}
free(ptr);
} else {
fprintf(stderr, "Error allocating %llu KB of memory\n", B_TO_KB(freeMem));
return -1;
}
return ret;
}