Maximum memory which malloc can allocate Maximum memory which malloc can allocate c c

Maximum memory which malloc can allocate


I read that the maximum memory malloc can allocate is limited to physical memory (on heap).

Wrong: most computers/OSs support virtual memory, backed by disk space.

Some questions: does malloc allocate memory from HDD also?

malloc asks the OS, which in turn may well use some disk space.

What was the reason for above behavior? Why didn't the loop break at any time?

Why wasn't there any allocation failure?

You just asked for too little at a time: the loop would have broken eventually (well after your machine slowed to a crawl due to the large excess of virtual vs physical memory and the consequent super-frequent disk access, an issue known as "thrashing") but it exhausted your patience well before then. Try getting e.g. a megabyte at a time instead.

When a program exceeds consumption of memory to a certain level, thecomputer stops working because other applications do not get enoughmemory that they require.

A total stop is unlikely, but when an operation that normally would take a few microseconds ends up taking (e.g.) tens of milliseconds, those four orders of magnitude may certainly make it feel as if the computer had basically stopped, and what would normally take a minute could take a week.


I know this thread is old, but for anyone willing to give it a try oneself, use this code snipped

#include <stdlib.h>int main() {int *p;while(1) {    int inc=1024*1024*sizeof(char);    p=(int*) calloc(1,inc);    if(!p) break;    }}

run

$ gcc memtest.c$ ./a.out

upon running, this code fills up ones RAM until killed by the kernel. Using calloc instead of malloc to prevent "lazy evaluation". Ideas taken from this thread:Malloc Memory Questions

This code quickly filled my RAM (4Gb) and then in about 2 minutes my 20Gb swap partition before it died. 64bit Linux of course.


Try this

#include <stdlib.h>#include <stdio.h>main() {    int Mb = 0;    while (malloc(1<<20)) ++Mb;    printf("Allocated %d Mb total\n", Mb);}

Include stdlib and stdio for it.
This extract is taken from deep c secrets.