Hi,

I am using a QHash and I am not sure how to release the virtual memory used by the QHash.

Here is the code of a function using a QHash.

Qt Code:
  1. void testMemory(){
  2. QMap<long , int> distances;
  3. for(long x=0; x<2073600; x++){
  4. distances.insert(x, 2);
  5. }
  6. distances.clear();
  7. }
To copy to clipboard, switch view to plain text mode 

Here is the code to test the memory before and after the call to testMemory() function.

Qt Code:
  1. ...
  2. #define DIV 1024
  3. #define WIDTH 7
  4. ...
  5. MEMORYSTATUSEX statex;
  6. statex.dwLength = sizeof (statex);
  7. GlobalMemoryStatusEx (&statex);
  8. printf ("There is %*ld percent of memory in use.\n", WIDTH, statex.dwMemoryLoad);
  9. printf ("There are %*I64d total Kbytes of physical memory.\n", WIDTH, statex.ullTotalPhys/DIV);
  10. printf ("There are %*I64d free Kbytes of physical memory.\n", WIDTH, statex.ullAvailPhys/DIV);
  11. printf ("There are %*I64d total Kbytes of paging file.\n", WIDTH, statex.ullTotalPageFile/DIV);
  12. printf ("There are %*I64d free Kbytes of paging file.\n", WIDTH, statex.ullAvailPageFile/DIV);
  13. printf ("There are %*I64d total Kbytes of virtual memory.\n", WIDTH, statex.ullTotalVirtual/DIV);
  14. printf ("There are %*I64d free Kbytes of virtual memory.\n", WIDTH, statex.ullAvailVirtual/DIV);
  15.  
  16. cout << endl << "Call testMem function..." << endl << endl;
  17.  
  18. testMemory();
  19.  
  20. statex.dwLength = sizeof (statex);
  21. GlobalMemoryStatusEx (&statex);
  22. printf ("There is %*ld percent of memory in use.\n", WIDTH, statex.dwMemoryLoad);
  23. printf ("There are %*I64d total Kbytes of physical memory.\n", WIDTH, statex.ullTotalPhys/DIV);
  24. printf ("There are %*I64d free Kbytes of physical memory.\n", WIDTH, statex.ullAvailPhys/DIV);
  25. printf ("There are %*I64d total Kbytes of paging file.\n", WIDTH, statex.ullTotalPageFile/DIV);
  26. printf ("There are %*I64d free Kbytes of paging file.\n", WIDTH, statex.ullAvailPageFile/DIV);
  27. printf ("There are %*I64d total Kbytes of virtual memory.\n", WIDTH, statex.ullTotalVirtual/DIV);
  28. printf ("There are %*I64d free Kbytes of virtual memory.\n", WIDTH, statex.ullAvailVirtual/DIV);
  29. ...
To copy to clipboard, switch view to plain text mode 

Here is the console output for this test :

Qt Code:
  1. There is 59 percent of memory in use.
  2. There are 2095848 total Kbytes of physical memory.
  3. There are 843764 free Kbytes of physical memory.
  4. There are 4037636 total Kbytes of paging file.
  5. There are 2799952 free Kbytes of paging file.
  6. There are 2097024 total Kbytes of virtual memory.
  7. There are 2062472 free Kbytes of virtual memory.
  8.  
  9. Call testMem function...
  10.  
  11. There is 59 percent of memory in use.
  12. There are 2095848 total Kbytes of physical memory.
  13. There are 845320 free Kbytes of physical memory.
  14. There are 4037636 total Kbytes of paging file.
  15. There are 2799572 free Kbytes of paging file.
  16. There are 2097024 total Kbytes of virtual memory.
  17. There are 1801284 free Kbytes of virtual memory.
To copy to clipboard, switch view to plain text mode 


As you can see, there are many Kbytes of virtual memory that aren't released after the function.

What I do wrong? How can I change the testMemory() function to free all virtual memory?

What did I miss?

Note : if you comment the line 4 of the testMemory() function (the line that insert an item), all the virtual memory is released correctly.