Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <random>
- #include "windows.h"
- const size_t SIZE_ARR = 800; //change for bigger memory size (800 = 2GB)
- int*** createMatrix() {
- std::mt19937 gen(time(0));
- std::uniform_int_distribution<int> randomNum(0, 9);
- int*** arr3D = new int** [SIZE_ARR] {}; // for 3 pointers -> 2D
- for (int i = 0; i < SIZE_ARR; ++i) {
- arr3D[i] = new int* [SIZE_ARR] {}; // for 3 x 3 pointers -> 1D
- for (int j = 0; j < SIZE_ARR; ++j) {
- arr3D[i][j] = new int[SIZE_ARR] {}; // for each 1D pointer
- for (int k = 0; k < SIZE_ARR; ++k) {
- arr3D[i][j][k] = randomNum(gen);
- }
- }
- }
- return arr3D;
- }
- void releaseMemory(int*** arr3D) {
- for (int i = 0; i < SIZE_ARR; ++i) {
- for (int j = 0; j < SIZE_ARR; ++j) {
- delete[] arr3D[i][j]; // delete 1D arrays
- arr3D[i][j] = nullptr;
- }
- delete[] arr3D[i]; // delete 3 x 2D array
- arr3D[i] = nullptr;
- }
- delete[] arr3D; // delete the 3D array
- arr3D = nullptr;
- }
- void printMemoryUsage(MEMORYSTATUSEX memInfo) {
- GlobalMemoryStatusEx(&memInfo);
- DWORDLONG totalPhysMem = memInfo.ullTotalPhys;
- DWORDLONG physMemUsed = memInfo.ullTotalPhys - memInfo.ullAvailPhys;
- totalPhysMem = totalPhysMem / 1024; // in kb
- physMemUsed = physMemUsed / 1024; // in kb
- std::string totalMemoryBar(50, '|'); // 50 x | represent 100% of the memory
- std::string usedMemoryBar(((double)physMemUsed / totalPhysMem) * 100 / 2, '|');
- std::cout << " RAM total:" << totalMemoryBar << ' ' << totalPhysMem << " Kb" << std::endl;
- std::cout << " RAM used: "<< usedMemoryBar << ' ' << physMemUsed << " Kb" << std::endl;
- std::cout << " RAM used: " << ((double)physMemUsed / totalPhysMem) * 100 << " %" << std::endl;
- }
- int main()
- {
- MEMORYSTATUSEX memInfo;
- memInfo.dwLength = sizeof(MEMORYSTATUSEX);
- std::cout << " Current memory status: " << std::endl;
- printMemoryUsage(memInfo);
- std::cout << std::endl;
- std::cout << " Size of the 3D int matrix: " << SIZE_ARR << std::endl;
- std::cout << " Requested memory: ~" << SIZE_ARR * SIZE_ARR * SIZE_ARR * 4 / 1000 / 1000 // int = 4 byte
- << " MB"<< std::endl;
- std::cout << " Allocation of memory . . . " << std::endl;
- int*** arr3D = createMatrix();
- printMemoryUsage(memInfo);
- std::cout << std::endl;
- std::cout << " Deallocation of memory . . . " << std::endl;
- releaseMemory(arr3D);
- std::cout << " Current memory status: " << std::endl;
- printMemoryUsage(memInfo);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement