1

I know that there is a lot of questions regarding the issue I'm pointing here, but I couldn't find any complex answer (neither on StackOverflow nor in other sources).

I would like to ask about heap (RAM) fragmentation problem.

As I understood there are two kind of fragmentation: internal - related with difference between allocation unit size (AU) and the size of the allocated memory AM (waste memory is equal to AM % AU), external - related with noncontinuous areas of a free memory, so even if the sum of the free memory areas can handle the new allocation request, it fails if there is no continues area that can handle it.

This is quite clear. The problems start when the "paging" appears.

Sometimes I can find an information that paging solves the external fragmentation issue. Indeed I agree that thanks to paging the OS is able to create the virtually continues areas of the memory, assigned to the process, even if physically the parts of the memory are scattered.

But how exactly does it help with the external fragmentation? I mean, assuming that the size of a page has 4kB, and we want to allocate 16 kB, then of course we just need to find four empty pages frames, even if physically the frames are not a part of a continues area.

But what in case of the smaller allocation ? I believe the page itself can still be fragmented and (in worst case) the OS still needs to provide a new frame if the old one cannot be used to allocate the requested memory.

So is it that (assuming the worst case) sooner or later, with paging or without, the long working application that allocates and releases the heap memory (different sizes) will fall into low-memory condition, because of external fragmentation ?

So the question is how to deal with the external fragmentation? Own implementation of allocation algorithm ? Paging (as I wrote, not sure it helps) ? What else ? Does OS (Windows, Linux) provides some defragmentation methods ?

The most radical solution is to forbid using of the heap, but is it really necessary for the platforms with paging, virtual address spaces, virtual memory etc ... and the only issue is that the applications need to run unstoppable for a years ?

One more issue.. is internal fragmentation an ambiguous term ? Somewhere I have spotted the definition that internal fragmentation points to the part of page frame, that is wasted because the process does not need more memory, but the single frame cannot be owned by more than a single processes.

I have bolded the questions, so the people who are in hurry could find the question without reading everything.

Regards!

user1770426
  • 133
  • 1
  • 12
  • `... even if physically the frames are not a part of a continues area.` Physical memory is not relevant. four consecutive "pages" of unused *address space* are needed. – wildplasser Jul 05 '17 at 19:58
  • Yes, consecutive in the process (virtual) address space. But this is organized by mmu, and physially the position of the frames is irrelevant. – user1770426 Jul 06 '17 at 16:42

1 Answers1

5

"Fragmentation" is indeed not a very precise term. But we can say for sure that when a running application needs a block of n bytes and there are n or more bytes not in use, yet we can't get the required block, then "memory is too fragmented."

But how exactly does it [paging] help with the external allocation [I assume you mean fragmentation] ?

There's really nothing complicated here. External fragmentation is free memory between allocated blocks that's "too small" to satisfy any application requirement. This is a general concept. The definition of "too small" is application-dependent. Nonetheless, if allocated blocks can fall on any boundary, then it's easy, after many allocations and deallocations, for lots of such fragments to occur. Paging helps with external fragmentation in two ways.

  • First, it subdivides memory into fixed-size adjacent chunks - the pages - that are "large enough" so they're never useless. Again the definition of "large enough" is not precise. But most applications will have lots of requirements satisfiable by a single 4k page. Since no external fragmentation problem can occur for allocations of a page or less, the problem has been mitigated.

  • Second, the paging hardware provides a level of indirection between application pages and physical memory pages. Therefore any free physical memory page can be used to help satisfy any application request, no matter how large. For example, suppose you have 100 physical pages with every other physical page (50 of them) allocated. Without page-mapping hardware, the biggest request for contiguous memory that can be satisfied is 1 page. With mapping, it's 50 pages. (I'm disregarding virtual pages allocated initially with no mapped physical page. That's another discussion.)

But what in case of the smaller allocation ?

Again it's pretty simple. If the unit of allocation is a page, then any allocation smaller than a page yields an unused portion. This is internal fragmentation: unusable memory within an allocated block. The bigger you make allocation units (they don't have to be a single page), the more memory will be unusable due to internal fragmentation. On average, this will tend toward half of an allocation unit. Consequently, though OS's tend to allocate in units of pages, most application-side memory allocators request a very small number (often one) of big blocks (of pages) from the OS. They use much smaller allocation units internally: 4-16 bytes is pretty common.

So the question is how to deal with the external allocation [I assume you mean fragmentation] ? So is it that (assuming the worst case) sooner or later, with paging or without, the long working application that allocates and releases the heap memory (different sizes) will fall into low-memory condition, because of external fragmentation ?

If I understand you correctly, you're asking if fragmentation is inevitable. Except under very special conditions (e.g. the application only needs blocks of one size), the answer is yes. But that doesn't mean it's necessarily a problem.

Memory allocators use smart algorithms that limit fragmentation pretty effectively. For example, they may maintain "pools" with different block sizes, using the pool with block size most closely matching a given request. This tends to limit both internal and external fragmentation. A real world example that's very well documented is dlmalloc. The source code is also very clear.

Of course any general purpose allocator can fail under specific conditions. For this reason, modern languages (C++ and Ada are two I know) let you supply special-purpose allocators for objects of a given type. Typically - for a fixed-size object - these might simply maintain a pre-allcoated free list, so fragmentation for that particular case is zero, and allocation/deallocation are very fast.

One more note: It's possible to totally eliminate fragmentation with copying/compacting garbage collection. Of course this requires underlying language support, and there's a performance bill to pay. A copying garbage collector compacts the heap by moving objects to eliminate unused space completely whenever it runs to reclaim storage. To do this it must update every pointer in the running program to the corresponding object's new location. While this may sound complex, I've implemented a copying garbage collector, and it's not so bad. The algorithms are extremely cool. Unfortunately, the semantics of many languages (e.g. C and C++) don't allow finding every pointer in the running program, which is required.

The most radical solution is to forbid using of the heap, but is it really necessary for the platforms with paging, virtual address spaces, virtual memory etc ... and the only issue is that the applications need to run unstoppable for a years ?

Though general purpose allocators are good, they're not guaranteed. It's not unusual for safety-critical or hard real time constrained systems to avoid heap use completely. On the other hand, when no absolute guarantee is needed, a general purpose allocator is often fine. There are many systems that run perfectly with tough loads for extended periods using general purpose allocators: fragmentation reaches an acceptable steady state and doesn't cause a problem.

One more issue.. is internal fragmentation an ambiguous term ?

The term isn't ambiguous, but is used in different contexts. The invariant is that it's referring to unused memory inside allocated blocks.

OS literature tends to assume the allocation unit is pages. For example, Linux sbrk lets you request the end of the data segment be set anywhere, but Linux allocates pages, not bytes, so the unused part of the last page is internal fragmentation from the OS's point of view.

Application-oriented discussions tend to assume allocation is in "blocks" or "chunks" of arbitrary size. dlmalloc uses about 128 discrete chunk sizes, each maintained in its own free list. Plus, it will custom allocate very large blocks using OS memory mapping system calls, so there's at most a page size (minus 1 byte) of mismatch between request and actual allocation. Clearly it's going to a lot of trouble to minimize internal fragmentation. The fragmentation caused a given allocation is the difference between the request and the chunk actually allocated. Since there are so many chunk sizes, that difference is strictly limited. On the other hand, the many chunk sizes increase chances of external fragmentation problems: free memory may consist entirely of chunks that are well-managed by dlmalloc, yet too small to honor an application requirement.

Gene
  • 42,664
  • 4
  • 51
  • 82
  • Hi Gene, BIG thx for your answer. There is still something I do not understand. So as you have written: "_They use much smaller allocation units internally: 4-16 bytes is pretty common._". Assuming the application allocates blocks with sizes less than a page, and the default memory allocator has the allocation unit size also less than a page (so the multiple allocations can be done inside a single page), after a period of allocations and deallocations, the single page, provided by OS, can be so defragmentated that the application allocator would ask the OS for the new one ? – user1770426 Jul 13 '17 at 07:43
  • 1
    @user1770426 Yes. You can see this happen in dlmalloc. All the chunk lists are already "coalesced". That is, each chunk is bounded by currently allocated memory. If everything is freed, then entire arena becomes a single chunk in one list. So if a request arrives and no chunk is big enough, it calls the OS for more. Also note that when I say "application" above, I'm including `malloc` and similar default allocators because it runs in an application thread (vice OS allocation that typically doesn't). In my experience, it's rare for an application to implement custom allocators itself. – Gene Jul 13 '17 at 18:25
  • 2
    I checked. dlmalloc's minimum block is (with the most common C data type conventions) 16 bytes on a 32-bit machine and 32 bytes on a 64-bit machine – Gene Jul 13 '17 at 18:32
  • Thx Gene! I definitely got all of the answers :-) – user1770426 Jul 14 '17 at 06:39