#include #include #include #include #include "JSystem/JShell.hh" #include "JSystem/JSystemToolkit.hh" #include "Jeep/JPrint.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" /** * \file * * Example program to show memory usage. * \author mdejong */ int main(int argc, char **argv) { using namespace std; using namespace JPP; size_t size_B; int debug; try { JParser<> zap("Example program to show memory usage."); zap['M'] = make_field(size_B) = 1<<25; zap['d'] = make_field(debug) = 3; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } typedef long long int data_type; const size_t size = size_B / sizeof(data_type); { cout << endl; cout << "Memory usage (start) " << FIXED(5,2) << getMemoryUsage() << "%" << endl; data_type* buffer = new data_type[size]; cout << "Memory usage (new) " << FIXED(5,2) << getMemoryUsage() << "%" << endl; for (size_t i = 0; i != size; ++i) { buffer[i] = i; } cout << "Memory usage (assign) " << FIXED(5,2) << getMemoryUsage() << "%" << endl; delete[] buffer; cout << "Memory usage (delete) " << FIXED(5,2) << getMemoryUsage() << "%" << endl; } { cout << endl; cout << "Memory usage (start) " << FIXED(5,2) << getMemoryUsage() << "%" << endl; vector buffer(size); cout << "Memory usage (vector) " << FIXED(5,2) << getMemoryUsage() << "%" << endl; buffer.clear(); cout << "Memory usage (clear) " << FIXED(5,2) << getMemoryUsage() << "%" << endl; { vector zbuf; buffer.swap(zbuf); } cout << "Memory usage (swap) " << FIXED(5,2) << getMemoryUsage() << "%" << endl; } { cout << endl; cout << "Memory usage (start) " << FIXED(5,2) << getMemoryUsage() << "%" << endl; size_t N = 10; vector< vector > buffer(N); for (size_t i = 0; i != N; ++i) { buffer[i].resize(size / N); } cout << "Memory usage (vector) " << FIXED(5,2) << getMemoryUsage() << "%" << endl; buffer.clear(); cout << "Memory usage (clear) " << FIXED(5,2) << getMemoryUsage() << "%" << endl; { vector< vector > zbuf; buffer.swap(zbuf); } cout << "Memory usage (swap) " << FIXED(5,2) << getMemoryUsage() << "%" << endl; } }