Memory Fragmentation

Here’s a question for the people here who are more of a computer whiz than I am: is fragmentation of objects in memory really a big deal? Both the MySQL query cache and the APC cache become fragmented over time, as various objects get cached and then purged.

Does this really make a big difference? You see a huge rise in access times when it happens on disk, but in RAM? Or will the applications require a contiguous allocation and thus just not use it at all? (And, for bonus points, why has no one written a ‘memory defragmenter’ if such a thing exists? Given that my needs are for managing small chunks of memory, there’s no reason, actually, that it couldn’t simply reconstitute the 16MB cache in a wholly separate, contiguous chunk of RAM?

5 thoughts on “Memory Fragmentation

  1. This one is a complicated one. This is where a course in hardware and memory addressing is helpful. Long story short the answer is – it depends. It depends on how memory addressing is handled in the hardware. It depends on how the hardware handles Direct memory Access. It depends on how the hardware and software cache work together and independently. And of course how big a chunk of memory you are dealing with at a time. I suspect that most often it doesn’t matter much. A lot of the fragmentation is being handled by the way the hardware translates logical addresses to physical addresses. I think that some operating systems do defrag memory but I’m not sure and I couldn’t name one off hand. But I’m old and I’ve heard a lot with some of it never being said. 🙂

  2. Typically when you talk about disk fragmentation you’re talking about files that are split into multiple chunks which, when stored, aren’t contiguous. Obviously, this becomes painful because of disk seek times.

    However, most objects in memory are stored in a contiguous block, so you can get hurt in a different way — when you’re left with lots of small chunks of free memory all over the place and a big object to store.

  3. Will APC (among others) fragment the object, or will the insert simply fail? I’ve seen people refer to both happening.

    You should write an APC Memory Defragmenter. 😉

  4. APC allocates a bunch of memory up front and then manages that block by itself, and it won’t split a single object (cached file, user variable, etc.) into multiple segments of memory. If a large enough free block of contiguous memory can’t be found, APC will attempt to find enough contiguous memory by freeing items that have expired (apc.ttl in the INI). If it still can’t find enough contiguous memory, the insert will fail (regardless of how much total memory is free).

    APC includes a script (creatively named apc.php) that will show the status of your memory segment. (Note that since APC doesn’t appear to free anything until it needs more memory, you won’t be able to see any holes until you run out of space.)

Leave a Reply

Your email address will not be published. Required fields are marked *