[Interest] memory fragmentation?

Thiago Macieira thiago.macieira at intel.com
Mon Aug 20 17:23:38 CEST 2012


On segunda-feira, 20 de agosto de 2012 07.56.36, Jason H wrote:
> In comparing technologies (Qt vs .NET) one of the biggest architectural
> differences is the garbage collection model.
> 
> 
> Qt uses the standard C++ new/free, where .NET uses a garbage collector.
> Interestingly the GC not only manages overall usage, but can move objects
> around in memory. This is the prupose fo the ^ operator in their C++/CLI -
> it's a memory handle that is used to reference an object instead of a
> pointer, because the GC can change the address.
> 
> In the entirety of my computing experience I have never seen a malloc or new
> fail because of memory fragmentation. Is this some low-level MS FUD?

No. It's a non-theoretical situation. It can happen.

Take, for example, a 32-bit application running on a 32-bit Linux. It has 3 GB 
of addressing space. Since it's using some of it when it starts up, it has 
less than 3 GB available for use.

Try this:
	malloc(1 GB);
	malloc(100 MB);
	free(the 1 GB block)
	malloc(2 GB);

There's a big chance it will fail, even though there's still roughly 2.8 GB of 
addressing space available.

The above is, of course, an academic exercise. If your application is going to 
use blocks in the order of gigabytes, you should be using 64-bit.

In a real-world case, memory fragmentation usually influences the total memory 
usage of an application. Even if large chunks of memory are freed, they can't 
be returned to the operating system because the application is still using 
blocks further down the heap. Also, it might increase its consumption if it 
does operations like:

	ptr1 = malloc(BIG);
	ptr2 = malloc(SMALL);
	free(ptr1);
	ptr3 = malloc(SMALL);
	ptr4 = malloc(BIG);
	free(ptr2);
	free(ptr3);
	return ptr4;

In a zero-overhead allocator, ptr1 = ptr2 + BIG and ptr3 = ptr1. That means 
the room between ptr3 and ptr1 is only BIG - SMALL, which isn't big enough to 
satisfy the fourth malloc. That code above will extend the heap by at least 
2*BIG + SMALL, even though it only had BIG + 2*SMALL amount of memory 
allocated at any given time.

E.g., for BIG = 65536 (64k) and SMALL = 16, the heap was extended by at least 
128k + 16 bytes, even though the function actually allocated only 64k + 32 
bytes.

If that operation is repeated in a loop, the heap might grow a lot. This is 
known to happen under certain circumstances for Qt, for example: memory 
allocations for QStrings, QByteArrays and QEvents, though small, happen quite 
often.


-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center
     Intel Sweden AB - Registration Number: 556189-6027
     Knarrarnäsgatan 15, 164 40 Kista, Stockholm, Sweden
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120820/6e89ce12/attachment.sig>


More information about the Interest mailing list