A
amit
Hello friends
Is there any way to release unused process address space managed by a
heap?
For example, please see a code below:
void ShowAvailableVirtualMemory(){
MEMORYSTATUSEX status;
status.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&status);
cout << "Avail Virtual: " << status.ullAvailVirtual << endl;
}
int main(int argc, char** argv){
vector<char*> blockList;
cout << "Allocate 100KB * 15000 in C Heap" << endl;
for(int i = 0; i < 15000; i++){
blockList.push_back(new char[100 * 1024]);
}
ShowAvailableVirtualMemory();
cout << "Release them" << endl;
for(vector<char*>::iterator it = blockList.begin(); it !=
blockList.end(); ++it){
delete [] (*it);
}
ShowAvailableVirtualMemory();
try{
char* p = new char[512 * 1024 * 1024];
}catch(bad_alloc&){
cout << "512MB allocation failed" << endl;
}
}
The output is:
Allocate 100KB * 15000 in C Heap
Avail Virtual: 529141760
Release them
Avail Virtual: 529137664
512MB allocation failed
So, you can see that the available virtual address space is 500MB even
after deleting the memory blocks.
Attaching the debugger just after deletion, you can see the size of
heap is NOT changed but it keeps a lot of RESERVED pages.
0:000> !address -summary
-------------------- Usage SUMMARY --------------------------
TotSize ( KB) Pct(Tots) Pct(Busy) Usage
1e6000 ( 1944) : 00.09% 00.12% : RegionUsageIsVAD
1f89e000 ( 516728) : 24.64% 00.00% : RegionUsageFree
427000 ( 4252) : 00.20% 00.27% : RegionUsageImage
100000 ( 1024) : 00.05% 00.06% : RegionUsageStack
1000 ( 4) : 00.00% 00.00% : RegionUsageTeb
60040000 ( 1573120) : 75.01% 99.54% : RegionUsageHeap
0 ( 0) : 00.00% 00.00% : RegionUsagePageHeap
1000 ( 4) : 00.00% 00.00% : RegionUsagePeb
1000 ( 4) : 00.00% 00.00% :
RegionUsageProcessParametrs
2000 ( 8) : 00.00% 00.00% :
RegionUsageEnvironmentBlock
Tot: 7fff0000 (2097088 KB) Busy: 60752000 (1580360 KB)
-------------------- Type SUMMARY --------------------------
TotSize ( KB) Pct(Tots) Usage
1f89e000 ( 516728) : 24.64% : <free>
427000 ( 4252) : 00.20% : MEM_IMAGE
1d5000 ( 1876) : 00.09% : MEM_MAPPED
60156000 ( 1574232) : 75.07% : MEM_PRIVATE
-------------------- State SUMMARY --------------------------
TotSize ( KB) Pct(Tots) Usage
dd6000 ( 14168) : 00.68% : MEM_COMMIT
1f89e000 ( 516728) : 24.64% : MEM_FREE
5f97c000 ( 1566192) : 74.68% : MEM_RESERVE
Since it's reserved, available process address space for non-heap
allocation (VirtualAlloc, for example) still fails. Is there any way
to release these MEM_RESERVE pages and get more virtual address space?
Thanks
Is there any way to release unused process address space managed by a
heap?
For example, please see a code below:
void ShowAvailableVirtualMemory(){
MEMORYSTATUSEX status;
status.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&status);
cout << "Avail Virtual: " << status.ullAvailVirtual << endl;
}
int main(int argc, char** argv){
vector<char*> blockList;
cout << "Allocate 100KB * 15000 in C Heap" << endl;
for(int i = 0; i < 15000; i++){
blockList.push_back(new char[100 * 1024]);
}
ShowAvailableVirtualMemory();
cout << "Release them" << endl;
for(vector<char*>::iterator it = blockList.begin(); it !=
blockList.end(); ++it){
delete [] (*it);
}
ShowAvailableVirtualMemory();
try{
char* p = new char[512 * 1024 * 1024];
}catch(bad_alloc&){
cout << "512MB allocation failed" << endl;
}
}
The output is:
Allocate 100KB * 15000 in C Heap
Avail Virtual: 529141760
Release them
Avail Virtual: 529137664
512MB allocation failed
So, you can see that the available virtual address space is 500MB even
after deleting the memory blocks.
Attaching the debugger just after deletion, you can see the size of
heap is NOT changed but it keeps a lot of RESERVED pages.
0:000> !address -summary
-------------------- Usage SUMMARY --------------------------
TotSize ( KB) Pct(Tots) Pct(Busy) Usage
1e6000 ( 1944) : 00.09% 00.12% : RegionUsageIsVAD
1f89e000 ( 516728) : 24.64% 00.00% : RegionUsageFree
427000 ( 4252) : 00.20% 00.27% : RegionUsageImage
100000 ( 1024) : 00.05% 00.06% : RegionUsageStack
1000 ( 4) : 00.00% 00.00% : RegionUsageTeb
60040000 ( 1573120) : 75.01% 99.54% : RegionUsageHeap
0 ( 0) : 00.00% 00.00% : RegionUsagePageHeap
1000 ( 4) : 00.00% 00.00% : RegionUsagePeb
1000 ( 4) : 00.00% 00.00% :
RegionUsageProcessParametrs
2000 ( 8) : 00.00% 00.00% :
RegionUsageEnvironmentBlock
Tot: 7fff0000 (2097088 KB) Busy: 60752000 (1580360 KB)
-------------------- Type SUMMARY --------------------------
TotSize ( KB) Pct(Tots) Usage
1f89e000 ( 516728) : 24.64% : <free>
427000 ( 4252) : 00.20% : MEM_IMAGE
1d5000 ( 1876) : 00.09% : MEM_MAPPED
60156000 ( 1574232) : 75.07% : MEM_PRIVATE
-------------------- State SUMMARY --------------------------
TotSize ( KB) Pct(Tots) Usage
dd6000 ( 14168) : 00.68% : MEM_COMMIT
1f89e000 ( 516728) : 24.64% : MEM_FREE
5f97c000 ( 1566192) : 74.68% : MEM_RESERVE
Since it's reserved, available process address space for non-heap
allocation (VirtualAlloc, for example) still fails. Is there any way
to release these MEM_RESERVE pages and get more virtual address space?
Thanks