1

Possible Duplicate:
maximum memory which malloc can allocate!

How can I know the maximum size of the heap I can occupy by malloc(). I use MS Visual Studio 2010.

trincot
  • 317,000
  • 35
  • 244
  • 286
Aan
  • 12,247
  • 36
  • 89
  • 150
  • It's not clear what you're asking. Do you want the max size that malloc() could allocate given unlimited memory, or are you asking how to figure out how much memory is available in the system your running on? – Caleb Nov 02 '12 at 20:24
  • 2
    You can *ask* for up to `SIZE_MAX`, but that will almost certainly fail. How much you can *successfully get* depends on many factors (32- vs. 64-bit, C runtime version, OS version, max page file size, physical memory available, etc.). – Adam Rosenfield Nov 02 '12 at 20:26

2 Answers2

8

There are operating system-dependent ways of finding out how much virtual memory is available for your process, but I do not know how to do this on windows. You can, however, find it out by doing a hunt+halving search, caling malloc with ever larger arguments until it fails, and then homing in on the value it balks at. Something like

for(i=1; v=malloc(i); i<<=1) free(v);

By this point you know that i/2 bytes is ok, while i bytes is not ok. Now do a bisection search for the actual maximum:

for(a=(i>>1), b=i; a < b-1;)
{
    c=(a+b)>>1;
    if(v=malloc(c)) { a=c; free(v); }
    else b=c;
}

At this point, a is the largest amount you can successfully allocate.

amaurea
  • 4,950
  • 26
  • 35
  • I up-voted this because I've done exactly this in the past (although not quite so succinctly - great job!). I did it in C++ on Windows 7 using GCC 4.8 using new/delete instead of malloc/free. Results? I allocated 1,953,169,376 bytes in one chunk and successfully filled it with values and then accessed the values - in short, it works. – rmcghee May 18 '14 at 04:28
  • Note that the Linux kernel will often over-commit memory, which could cause errors or delays for swapping. This is not a reliable way to do this on Linux. – Tyzoid Aug 26 '19 at 15:44
5

Well, why not just look at its argument... size_t. So, you can allocate anything in the range of size_t (SIZE_MAX works as well, but it is based on size_t anyway and, event if it weren't, mallocs signature is still what you have to deal with).

Your question is a bit unclear, but if you're actually asking what the maximum heap size is then, theoretically, it is determined by the native pointer size. For example, on a 32-bit system a pointer can hold any address between 0 and 2^32-1 (of course, address 0 is not commonly valid).

Of course, in practice, there are details which must be considered, most of them OS specific. How much physical memory (RAM) is installed in the machine? If you're OS uses virtual addresses then this complicates things as you are no longer limited by physical RAM (though performance will drop of a cliff with incessant paging).

It helps to understand how virtual address space works when thinking about how much your program can allocate on a given platform.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • 2
    I think the OP wants to know how much memory he/she can actually allocate -- that is, how much memory is available to allocate safely. – Caleb Nov 02 '12 at 20:23
  • @Caleb: Yeah, I was wondering about that. I'll add something, – Ed S. Nov 02 '12 at 20:24
  • 2
    @Ed http://stackoverflow.com/questions/2513505/how-to-get-available-memory-c-g – Austin Nov 02 '12 at 20:25