Archived

This topic is now archived and is closed to further replies.

Dynamic memory fragmentation

This topic is 5235 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I hear about watching for memory fragmentation in many discussions of dynamic memory, but don''t know when to recognize it is even happening. It has always been glazed on, but never discussed. 1. Is it even an issue nowadays with 256MB of RAM becoming the norm? 2. Does the C++ standard library exacerbate fragmentation through its heavier reliance on new/delete (as compared to C)? Do the default allocators for STL help things at all? I guess I''m wondering if I should be stingy about reusing std::string''s. 3. How do I know when it happens? How long does it take to finish defragmenting the RAM? (also I wonder how it compares to time spent garbage collecting) 4. What sorts of apps are more prone to it? The obvious answer is "apps that use dynamic memory" but this isn''t always the case (games use a lot of memory, but can allocate everything they need up front and release it in a fairly orderly fashion).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Is it even an issue nowadays with 256MB of RAM becoming the norm?


it could be. depends on many factors, one being the allocation techniques used, how long the application is running, sizes of blocks allocated <large differences may result in more fragmentation>..

quote:
Does the C++ standard library exacerbate fragmentation through its heavier reliance on new/delete (as compared to C)?



no. new & delete aren''t anything special, except for the fact that they call constructors/destructors. i bet a good deal of new/delete definitions just simply call malloc

quote:
I guess I''m wondering if I should be stingy about reusing std::string''s.


if you think it''s necessary.

quote:
How do I know when it happens?

when your system reports alot of ''free total memory'', yet memory allocation fails the only way, really, would be to implement (or use existing) diagnostic routines, which of course depends on the allocator.

quote:
How long does it take to finish defragmenting the RAM?


well, you won''t be able to defragment memory unless you use another layer of indirection in referencing the memory. defragmenting the memory generally means you have to copy data around, so it''s not exactly going to be speedy. actual figures would depend on how such a system is implemented, obviously.
the only method i know of that wouldn''t require copying data is coalescing blocks which are adjacent; any half-decent allocator will do this.



Share this post


Link to post
Share on other sites
> Is it even an issue nowadays with 256MB of RAM becoming the norm?

You won''t notice anything with a "Hello World!" program. A MMORPG server will need to be careful since it has to survice for a long period of time witout its memory footprint growing without bounds. In that case, memory leaks are more of an issue than fragmentation.

> 3. How do I know when it happens?

Your app starts swapping and frame rate drops. Take a look at "Task Manager" at how your process grows with time until it kinda stabilizes.

> What sorts of apps are more prone to it?

Apps that allocate and free tons of ill-sized memory blocks. A 3D CAD system or a word processing system are more likely to have problems; that''s why you''d see ''multiple heaps'' use in them. Games typically load large chunks of geometry and alloc/free other structs using only a fraction of what it has loaded. As noted above, this is rarely an issue.

> Does the C++ standard library exacerbate fragmentation

That''s a loaded question! You could design classes that use pooled memory buffers and thus STL allocators will not be too much affected except for tree structures and other colateral structs. Like any tool, performance lies on the shoulders of the programmer that uses them. STL in general, however, promotes code bloat as developpers include template header files all over, duplicating code everywhere for minor class differences.

> I hear about watching for memory fragmentation

It is an important issue for widely distributed software, but I wouldn''t worry about it for indie projects.

-cb

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It can be pretty hard to diagnoze memory fragmentation. It''s not uncommon to see it in applications that run for a long time, such as server applications. I''ve seen it a few times in poorly written IIS extensions (DLL:s that you plugin to the IIS) that have been running for many days with high load. The solution is to avoid frequent heap allocations/deallocations, re-use buffers by pooling them.

VC++ has a small block heap, that "caches" allocations to the OS. You might want to use that, see _set_sbh_threshold in MSDN.

As for re-using objects such as STL strings, I''d say you should if it''s something you allocate and deallocate frequently. It''s not much harder, won''t take much longer time, and your application will benefit both in terms of performnance and fragmentation point of view.


MSDN has some articles about this, that also tells how the OS works to prevent fragmentation.

http://msdn.microsoft.com/library/en-us/dngenlib/html/heap3.asp

http://support.microsoft.com/support/kb/articles/q139/6/38.asp

http://msdn.microsoft.com/library/en-us/memory/base/low_fragmentation_heap.asp

http://support.microsoft.com/default.aspx?kbid=816542

Share this post


Link to post
Share on other sites
With virtual memory, physical memory fragmentation doesn''t really occur, and all notable memory occurs inside the process. (this isn''t entirely true, but close enough)

There are many different dynamic memory allocation algorithms that are used. AFAIK there are different implementations of malloc from "stdlib" in windows and linux.

A good allocator should only waste something like an order of magnitude less bytes than it actually uses, even after a very long time.

If you are worried about the performance of your malloc''ing, you might want to look up DLMalloc, AKA Doug Lea''s malloc. It''s way more efficient than the default Windows stdlib version, and it''s easy to incorporate into your program. I''m fairly sure the licence is free, haven''t checked yet.

Share this post


Link to post
Share on other sites