How to reduce the size of C runtime heap?

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
 
E

Eric Sosman

amit said:
Hello friends

Is there any way to release unused process address space managed by a
heap?
[...]

I'm pretty sure this is in the comp.lang.c Frequently
Asked Questions (FAQ) list, <http://c-faq.com/>. Unfortunately,
something seems to be amiss at the hosting service and the FAQ
isn't viewable at the moment. Perhaps you can find an archived
copy somewhere, or wait in hopes that the official FAQ will
reappear.

In the meantime, the answer is "No, not unless the C
implementation offers system-specific extensions to do it."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top