- Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMemoryUsage.cpp
More file actions
Latest commit
147 lines (126 loc) · 5.04 KB
/
Copy pathMemoryUsage.cpp
File metadata and controls
147 lines (126 loc) · 5.04 KB
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
// from https://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process
#include<stddef.h>
#include<stdio.h>
#include<chrono>
#include<execution>
using std::chrono::duration;
using std::chrono::duration_cast;
using std::chrono::high_resolution_clock;
using std::milli;
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#include"windows.h"
#else
#include"sys/types.h"
#include"sys/sysinfo.h"
#endif
unsignedlonglongphysical_memory_used_in_megabytes()
{
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
MEMORYSTATUSEX memInfo;
memInfo.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&memInfo);
//DWORDLONG totalVirtualMem = memInfo.ullTotalPageFile;
//DWORDLONG virtualMemUsed = memInfo.ullTotalPageFile - memInfo.ullAvailPageFile;
//DWORDLONG totalPhysMem = memInfo.ullTotalPhys;
DWORDLONG physMemUsed = memInfo.ullTotalPhys - memInfo.ullAvailPhys; // physical memory used by the system
//SIZE_T physMemUsedByMe = pmc.WorkingSetSize; // by current process
return (physMemUsed / (1024ULL * 1024));
#else
structsysinfo memInfo;
sysinfo(&memInfo);
longlong totalVirtualMem = memInfo.totalram;
//Add other values in next statement to avoid int overflow on right hand side...
totalVirtualMem += memInfo.totalswap;
totalVirtualMem *= memInfo.mem_unit;
longlong virtualMemUsed = memInfo.totalram - memInfo.freeram;
//Add other values in next statement to avoid int overflow on right hand side...
virtualMemUsed += memInfo.totalswap - memInfo.freeswap;
virtualMemUsed *= memInfo.mem_unit;
longlong totalPhysMem = memInfo.totalram;
//Multiply in next statement to avoid int overflow on right hand side...
totalPhysMem *= memInfo.mem_unit;
longlong physMemUsed = memInfo.totalram - memInfo.freeram;
//Multiply in next statement to avoid int overflow on right hand side...
physMemUsed *= memInfo.mem_unit; // total physical memory used by the whole system
return (physMemUsed / (1024ULL * 1024));
#endif
}
unsignedlonglongphysical_memory_total_in_megabytes()
{
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
MEMORYSTATUSEX memInfo;
memInfo.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&memInfo);
//DWORDLONG totalVirtualMem = memInfo.ullTotalPageFile;
//DWORDLONG virtualMemUsed = memInfo.ullTotalPageFile - memInfo.ullAvailPageFile;
DWORDLONG totalPhysMem = memInfo.ullTotalPhys;
//DWORDLONG physMemUsed = memInfo.ullTotalPhys - memInfo.ullAvailPhys; // physical memory used by the system
//SIZE_T physMemUsedByMe = pmc.WorkingSetSize; // by current process
return (totalPhysMem / (1024ULL * 1024));
#else
structsysinfo memInfo;
sysinfo(&memInfo);
longlong totalVirtualMem = memInfo.totalram;
//Add other values in next statement to avoid int overflow on right hand side...
totalVirtualMem += memInfo.totalswap;
totalVirtualMem *= memInfo.mem_unit;
longlong virtualMemUsed = memInfo.totalram - memInfo.freeram;
//Add other values in next statement to avoid int overflow on right hand side...
virtualMemUsed += memInfo.totalswap - memInfo.freeswap;
virtualMemUsed *= memInfo.mem_unit;
longlong totalPhysMem = memInfo.totalram;
//Multiply in next statement to avoid int overflow on right hand side...
totalPhysMem *= memInfo.mem_unit;
longlong physMemUsed = memInfo.totalram - memInfo.freeram;
//Multiply in next statement to avoid int overflow on right hand side...
physMemUsed *= memInfo.mem_unit; // total physical memory used by the whole system
return (totalPhysMem / (1024ULL * 1024));
#endif
}
// Test memory allocation
intTestMemoryAllocation()
{
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
constsize_tNUM_TIMES = 1000;
constsize_tSIZE_OF_ARRAY = 100'000'000;
unsignedchar* array_of_pointers[NUM_TIMES]{};
size_t sum = 0;
for (size_t i = 0; i < NUM_TIMES; ++i)
{
array_of_pointers[i] = newunsignedchar[SIZE_OF_ARRAY];
for (size_t j = 0; j < SIZE_OF_ARRAY; ++j)
array_of_pointers[i][j] = (unsignedchar)j;
for (size_t j = 0; j < SIZE_OF_ARRAY; ++j)
sum += array_of_pointers[i][j];
printf("Allocated array: %zu sum = %zu\n", i, sum);
std::this_thread::sleep_for(std::chrono::milliseconds(300));
}
printf("Final sum = %zu\n", sum);
for (size_t i = 0; i < NUM_TIMES; ++i)
{
delete[] array_of_pointers[i];
}
#endif
return0;
}
voidprint_current_memory_space()
{
printf("physical memory used = %llu physical memory total = %llu\n",
physical_memory_used_in_megabytes(), physical_memory_total_in_megabytes());
}
voidtest_lazy_memory_allocation()
{
constsize_tSIZE_OF_ARRAY = 10'000'000'000;
//size_t sum = 0;
print_current_memory_space();
unsignedchar* my_array = newunsignedchar[SIZE_OF_ARRAY];
print_current_memory_space();
for (size_t i = 0; i < (SIZE_OF_ARRAY / 2); ++i)
my_array[i] = (unsignedchar)i;
print_current_memory_space();
for (size_t i = (SIZE_OF_ARRAY / 2); i < SIZE_OF_ARRAY; ++i)
my_array[i] = (unsignedchar)i;
print_current_memory_space();
delete[] my_array;
print_current_memory_space();
}