Advertisement
Guest User

Memory

a guest
Jun 23rd, 2021
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include "windows.h"
  4.  
  5. const size_t SIZE_ARR = 800; //change for bigger memory size (800 = 2GB)
  6.  
  7. int*** createMatrix() {
  8.  
  9.     std::mt19937 gen(time(0));
  10.     std::uniform_int_distribution<int> randomNum(0, 9);
  11.  
  12.     int*** arr3D = new int** [SIZE_ARR] {}; // for 3 pointers -> 2D
  13.     for (int i = 0; i < SIZE_ARR; ++i) {
  14.        
  15.         arr3D[i] = new int* [SIZE_ARR] {};  // for 3 x 3 pointers -> 1D
  16.         for (int j = 0; j < SIZE_ARR; ++j) {
  17.  
  18.             arr3D[i][j] = new int[SIZE_ARR] {}; // for each 1D pointer
  19.             for (int k = 0; k < SIZE_ARR; ++k) {
  20.                 arr3D[i][j][k] = randomNum(gen);
  21.             }
  22.         }
  23.     }
  24.     return arr3D;
  25. }
  26.  
  27. void releaseMemory(int*** arr3D) {
  28.     for (int i = 0; i < SIZE_ARR; ++i) {
  29.         for (int j = 0; j < SIZE_ARR; ++j) {
  30.             delete[] arr3D[i][j]; // delete 1D arrays
  31.             arr3D[i][j] = nullptr;
  32.         }
  33.         delete[] arr3D[i]; // delete 3 x 2D array
  34.         arr3D[i] = nullptr;
  35.     }
  36.     delete[] arr3D; // delete the 3D array
  37.     arr3D = nullptr;
  38. }
  39.  
  40. void printMemoryUsage(MEMORYSTATUSEX memInfo) {
  41.     GlobalMemoryStatusEx(&memInfo);
  42.     DWORDLONG totalPhysMem = memInfo.ullTotalPhys;
  43.     DWORDLONG physMemUsed = memInfo.ullTotalPhys - memInfo.ullAvailPhys;
  44.     totalPhysMem = totalPhysMem / 1024; // in kb
  45.     physMemUsed = physMemUsed / 1024; // in kb
  46.     std::string totalMemoryBar(50, '|'); // 50 x | represent 100% of the memory
  47.     std::string usedMemoryBar(((double)physMemUsed / totalPhysMem) * 100 / 2, '|');
  48.     std::cout << " RAM total:" << totalMemoryBar << ' ' << totalPhysMem << " Kb" << std::endl;
  49.     std::cout << " RAM used: "<< usedMemoryBar << ' ' << physMemUsed << " Kb" << std::endl;
  50.     std::cout << " RAM used: " << ((double)physMemUsed / totalPhysMem) * 100 << " %" << std::endl;
  51. }
  52.  
  53. int main()
  54. {
  55.     MEMORYSTATUSEX memInfo;
  56.     memInfo.dwLength = sizeof(MEMORYSTATUSEX);
  57.     std::cout << " Current memory status: " << std::endl;
  58.     printMemoryUsage(memInfo);
  59.     std::cout << std::endl;
  60.  
  61.     std::cout << " Size of the 3D int matrix: " << SIZE_ARR << std::endl;
  62.     std::cout << " Requested memory: ~" << SIZE_ARR * SIZE_ARR * SIZE_ARR * 4 / 1000 / 1000 // int = 4 byte
  63.               << " MB"<< std::endl;
  64.     std::cout << " Allocation of memory . . . " << std::endl;
  65.     int*** arr3D = createMatrix();
  66.     printMemoryUsage(memInfo);
  67.     std::cout << std::endl;
  68.  
  69.     std::cout << " Deallocation of memory . . . " << std::endl;
  70.     releaseMemory(arr3D);
  71.     std::cout << " Current memory status: " << std::endl;
  72.     printMemoryUsage(memInfo);
  73.  
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement