[Interest] memory fragmentation?

Lukas Geyer lgeyer at gmx.at
Wed Aug 22 08:44:09 CEST 2012


Am 22.08.2012 05:45, schrieb Graeme Gill:
> Till Oliver Knoll wrote:
>> Folks, I gave up checking for NULL pointers (C, malloc) or bad_alloc
>> exceptions (new, C++) a long time ago. I remember a discussion several
>> years ago (here on Qt interest?) about desktop memory managers actually
>> never returning a NULL pointer (or throwing an exception) when they
>> cannot allocate memory.
>
> This is simply not true when it comes to malloc. Malloc can and does
> return NULL on MSWin, OS X and Linux.

Both of you are right, and both of you are wrong, due to what is 
referred to as 'overcommit'.

The address space is expanded immediately but physical memory pages are 
assigned at the moment the memory is accessed; either never, always or 
at kernels discretion, depending on the implemented overcommit strategy [1].

Therefore there is usually no point in doing a nullptr validation, 
because it might trigger, or not. Altough the kernel might refuse to 
assign additional memory (which will be more likely the larger the 
requested memory area is) and your allocation will return a nullptr, it 
is also valid that the kernel actually _does_ assign additional 
overcommitted memory but terminates your application once you access it 
due to having no physical memory pages available.

This is aggravated by the fact that the kernel is allowed to terminate 
(almost) any process in case of out-of-memory situations. This means 
that _your_ process might be killed because _another_ process requests a 
physical memory page.

Yes, nullptr validation can be used to determine if the kernel can 
provide 'that much' memory, but you will have to be aware that this 
might not necessarily result in a physical memory page and if your 
system is that out-of-memory so it cannot support even smaller memory 
allocations your process might be long-gone.

See also this discussion [2] at the QtDN about handling dynamic memory 
allocation failure, including overcommitting.


[1] 
http://git.kernel.org/?p=linux/kernel/git/stable/linux-stable.git;a=blob;f=Documentation/vm/overcommit-accounting

[2] http://qt-project.org/forums/viewthread/6593/



More information about the Interest mailing list