[Interest] memory fragmentation?

Bo Thorsen bo at fioniasoftware.dk
Tue Aug 21 12:01:49 CEST 2012


Hi Jason,

I'm going back to the original question, the thread seems to have 
wandered off :)

Den 20-08-2012 17:51, Jason H skrev:
> 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?

Memory fragmentation is defined as the problem where you allocate a 
bigger chunk of memory than what is available, even if the total amount 
of free memory is available.

Do you know if the ^ implementation in .NET actually does the realloc 
stuff, or do they only say that it's a possibility? I ask because this 
sounds hard to do well. You either have a really slow operation running 
often (just moving stuff back) or an almost impossible task (move things 
*you will keep for a long time* to the front of temporary objects.

First thing you should ask yourself is if you really have a problem or 
not. All the Qt widgets and application objects are usually instantiated 
at startup and never removed again. So they are not a problem.

If your objects are never big, then memory fragmentation is a 
theoretical problem. Sure, you'll loose some memory between objects, but 
if this is the problem, you are too close to the memory available on 
your target machine anyway.

The one case where you might have a problem is if you do have 
allocs/deallocs done often (for example list views that change content 
often) and you sometimes alloc a big chunk of the memory in one go.

Things you can do to combat this is mostly to make your objects smaller. 
For example, use linked lists instead of arrays. The lists will add 4 
bytes to each object, but the the object sizes are much smaller.

If you want to go even further, you should add code to do memory 
intensive stuff on the disc - much much slower, but at least it will run.

So, going back to the original question again: Yes, it's possible 
there's a problem. But it's incredibly rare, and I doubt Microsofts 
"solution" will really solve the problem, if you have it. But you have 
just proven that their marketing with the problem/solution works :)

Bo Thorsen,
Fionia Software.

-- 

Expert Qt and C++ developer for hire
Contact me if you need expert Qt help
http://www.fioniasoftware.dk



More information about the Interest mailing list