CPP
cpp
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
//based on code generated by ChatGPT
#include <iostream>
#include <sys/resource.h>
#include <fstream>
#include <string>
int main() {
// Show how much virtual space is allocated
struct rusage usage;
if (getrusage(RUSAGE_SELF, &usage) == 0) {
std::cout << "Allocated Virtual Memory Size: " << usage.ru_maxrss / 1024 << " MB" << std::endl;
} else {
std::cerr << "Failed to get memory info!" << std::endl;
}
// Show how much virtual space is used
std::ifstream status_file("/proc/self/status");
std::string line;
while (std::getline(status_file, line)) {
if (line.find("VmSize:") == 0) { // Look for "VmSize"
std::cout << "Used " << line << std::endl;
break;
}
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run