tags:

views:

91

answers:

5

I have a machine with 128M memory, and this is the program.

while(1)
{    
    ptr = malloc(1024 * 1024 * 100);
    if (NULL == ptr)
    {
        printf("malloc 100M Failed\n");
       return 1;
    }
    n+=100;
    printf("malloc %dM\n", n);
}

I found from output that malloc run 20 times, why?

+1  A: 

Hard to say - it depends on how the underlying OS gives you memory.

Its not necessarily physical memory, it could reside in swap space, or it may not allocate anything at all, choosing instead to do the allocation when you use the memory, rather than when you ask to allocate it.

Visage
A: 

The basic notion here is that malloc is assigning free memory blocks from virtual memory.

So do not expect to have only 128M of free memory, but more than that, depending on where your operating system is allocating memory from.

Yuval A
+4  A: 

Because your program is allocating virtual memory. In a 32-bit process (on Windows - other operating systems are slightly different), you typically get 2GB of virtual address space in which to allocate memory. The operating system them maps that memory between a "swap file" and physical memory (in your case, since you only have 128MB of physical memory, the majority of the memory you allocate will be in the swap file only).

So 100MB * 20 = "around" 2GB, and that explains why you can run through 20 loops.

Dean Harding
A 32 bit address space is 4 GB, not 2 GB, but not all of this will necessarily be available for contiguous allocation. Most operating systems will start to fail somewhere between the 2 and 3 GB mark.
Paul R
But I have no swap file indeed.
David Ge
@Paul: good point. The exact ratio differs depending on the OS/settings. @David: Depending on the platform, the operating system may not try to commit the memory (that is, actually reserve memory for it) until you actually use it. In such situations, you'll be able to "allocate" the entire virtual address space and only when you try to read/write it will you run into problems.
Dean Harding
+2  A: 

Since you don't actually do anything with the memory, you can probably get the kernel to over-commit (i.e. give you more memory than it could actually provide) by using virtual memory.

And 20 times 100M is pretty near 2GB, so it looks like you're using a 32 bit OS that reserves 2GB for OS use, leaving a 2GB address space for user-space processes. So 2GB is the absolute maximum amount of memory you could possibly address in such a process, no matter how much memory your computer actually has.

Joachim Sauer
sounds reasonable
David Ge
@David: e.g. *NIX systems traditionally allocation virtual memory only on first access. you test on *NIX systems is literally a nop. to make a real test, read or write e.g. every first byte of every 4K (usual page size) block in the allocated memory.
Dummy00001
A: 

Your virtual address space should be 2^32 bytes on a 32-bit platform without PAE enabled. Seems like your platform is allowing you to allocate about half of that before putting its foot down.

manneorama