#include <iomanip>
#include <fstream>
using namespace std;
void formatValue(const char msg[], double value, int index) {
string memory_unit[5] = {" [B]", " [KB]", " [MB]", " [GB]", " [TB]"};
while (value > 900) {
value /= 1024;
index++;
}
value < 2 ? cout << fixed << setprecision(2) : cout << fixed << setprecision(1);
cout << msg << value << memory_unit[index] << endl;
}
static int printMemoryInfo() {
ifstream fin("/proc/self/status");
if (!fin) {
cout << "memory info unknown!\n";
} else {
string part;
double value;
cout << fixed;
while (fin >> part) {
if (part == "VmHWM:") {
fin >> value;
formatValue("peak real memory usage: ", value, 1);
} else if (part == "VmPeak:") {
fin >> value;
formatValue("peak virtual memory usage: ", value, 1);
} else if (part == "VmRSS:") {
fin >> value;
formatValue("current real memory usage: ", value, 1);
} else if (part == "VmSize:") {
fin >> value;
formatValue("current virtual memory usage: ", value, 1);
}
}
fin.close();
cout.unsetf(ios::fixed);
cout.precision(6);
}
return 0;
}