+1  A: 

I don't actually know why that failed, but one thing to note is that `malloc(4)" may not actually give you 4 bytes, so this technique is not really an accurate way to find your maximum heap size.

I found this out from my question here.

For instance, when you declare 4 bytes of memory, the space directly before your memory could contain the integer 4, as an indication to the kernel of how much memory you asked for.

Chris Cooper
@Chris: indeed, malloc usually give a multiple of 16 bytes. There is two reasons. One is that standard says malloc should return a pointer compatible with any data alignment. Thus addresses separated by less than 16 bytes coulnd't be returned. The other reason is that freed blocks usually store some data used for internal memory management and a block too short - say 4 bytes - couldn't store it.
kriss
@kriss[i]freed blocks usually store some data used for internal memory management and a block too short - say 4 bytes - couldn't store it.[/i]Can you mention what kind of data?
Vikas
@Vikas: See the update to my answer.
Chris Cooper
+11  A: 

I read that 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 HD also?

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

What was the reason for above behaviour? Why didn't loop breaked at any point of 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, the computer stops working because other applications do not get enough memory 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 -- what would normally take a minute would take a week, etc;-).

Alex Martelli
Did you mean "thrashing"?
RichieHindle
@Alex:Thanx for the info about malloc allocating Disk space.I suspected that, but in many article there was no mention of disk space and was written that malloc alllocates on heap and physical memory. :)
Vikas
@RichieI also suppose that Alex meant 'thrashing' there.
Vikas
@Richie and @Vikas, oops, yes, edited to fix the typo, thanks!-)
Alex Martelli
your memory size is 1GB doesnt mean that malloc will go all the way there. It really depends upon the amount of memory your OS assigns to your process. Which by looking at the code in this case will be very low. From there on it goes on to allocate memory on your virtual memory.
Ram Bhat
+2  A: 

malloc does it's own memory management, managing small memory blocks itself, but ultimately it uses the Win32 Heap functions to allocate memory. You can think of malloc as a "memory reseller".

The windows memory subsystem comprises physical memory (RAM) and virtual memory (HD). When physical memory becomes scarce, some of the pages can be copied from physical memory to virtual memory on the hard drive. Windows does this transparently.

By default, Virtual Memory is enabled and will consume the available space on the HD. So, your test will continue running until it has either allocated the full amount of virtual memory for the process (2GB on 32-bit windows) or filled the hard disk.

mdma