[Interest] memory fragmentation?

Jason H scorp1us at yahoo.com
Mon Aug 20 17:51:02 CEST 2012


What can be done to combat this in C++/Qt?

Initially I thought there might be some kind of QObject d-ptr magic where the large allocations can be in the private class and re-allocate and copy the private class, without affecting the pointers that the application uses. Then have a function that runs on a QObject tree,  This does not fix the overall issue, but at least it could decrease the the chances of a failed allocation. But for me, this raises as many questions as it solves. Can we guarantee the new allocations are more compact than the old ones? That seems up to the heap manager. I don't know much about that. Also, when to run it? Periodically, or only when an allocation fails?

I've got a very long process that I want to protect against this. Switching to 64bit is not an option for at least another 10 years. 




________________________________
 From: Thiago Macieira <thiago.macieira at intel.com>
To: interest at qt-project.org 
Sent: Monday, August 20, 2012 11:23 AM
Subject: Re: [Interest] memory fragmentation?
 
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

_______________________________________________
Interest mailing list
Interest at qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120820/a2fea25f/attachment.html>


More information about the Interest mailing list