8

One of the answers to Do I need swap space if I have more than enough amount of RAM? got me wondering if there is any relationship between memory fragmentation as measured by cat /proc/buddyinfo and whether or not swap is in use. To be more specific I'm wondering if utilizing swap can reduce memory fragmentation. During a normal days work with swap off on my system I have this:

tvbox@tvbox-G31M-ES2L:~$ cat /proc/buddyinfo
Node 0, zone      DMA      3      3      4     14     16      6      2      0      0      1      0 
Node 0, zone   Normal   1564   1052    462    356    240    109     33     21      6      1      0 
Node 0, zone  HighMem     43   1972    839    285    183    109     98     34     16      0      0 
tvbox@tvbox-G31M-ES2L:~$ free
             total       used       free     shared    buffers     cached
Mem:       2053888    1821904     231984     171376     299908     812940
-/+ buffers/cache:     709056    1344832
Swap:            0          0          0

Note: this system has an uptime that never exceeds 18 hours.

On a more heavily utilized system I have this:

me@me-zippy:~$ cat /proc/buddyinfo
Node 0, zone      DMA    149    106     70     26     15      5      4      0      0      2      0 
Node 0, zone   Normal   2455   3527   4651   1421    367    157     61     19     14      3      0 
Node 0, zone  HighMem      7     43     75    266    166    162     91     43     27      0      0 
me@me-zippy:~$ free -h
             total       used       free     shared    buffers     cached
Mem:          7.4G       7.0G       351M       281M       116M       6.0G
-/+ buffers/cache:       967M       6.4G
Swap:           0B         0B         0B
me@me-zippy:~$ uptime
 12:01:49 up 3 days,  3:20,  2 users,  load average: 0.52, 0.23, 0.17

You'll note that neither of these systems have swap enabled.

2
  • 4
    Out of curiosity, why would it matter if memory were fragmented? It's not like a hard disk where seek time matters. Memory will always appear logically contiguous in process' virtual address space anyway, so user space can't even tell if memory is fragmented at the physical level. Is it because the page translation tables become more complicated?
    – Celada
    Commented Mar 20, 2015 at 2:07
  • Good question. The condition was raised as a reason to use swap. I remain sceptical so I'm looking for any connection between utilization of swap and memory fragmentation. I don't expect to find one but I'm always willing to change my stance in the face of new information. I may end up answering this one myself after further testing.
    – Elder Geek
    Commented Mar 20, 2015 at 13:25

1 Answer 1

4

Swapping allows physical pages to be moved around, in the sense that a page used for one purpose could have its content swapped out then used for another purpose.

Under a garden variety virtual memory management system, there is no such thing as fragmentation of physical memory as far as applications are concerned. Each page allocated by an application can be taken anywhere in physical memory, there is no reason why two consecutive pages in virtual memory would need to have any particular closeness in physical memory.

There are many cases where memory fragmentation is a concern: any circumstances in which physical memory location matters. This includes:

  • Paravirtualization, where virtual machines decide who owns which physical page at a granularity that is coarser than one page.
  • Similarly, clusters where several nodes share the same RAM pool.
  • Memory used by peripherals, which usually does require large contiguous buffers.
  • Optimizations such as huge pages.

In the absence of “complex” scenarios, physical memory fragmentation could lead to problems connecting a new device that requires a pool of contiguous memory (the kernel keeps such pools for that, but they could need to be enlarged if a driver suddenly makes a large request). If device usage is constant, physical fragmentation would not matter and in particular would not cause an application to run slower or run out of space.

It's possible that fragmentation of the physical address space could lead to more memory being used in the kernel to represent the free list. I don't believe that this is the case in Linux but I'm far from being an expert on its memory management.

To summarize, allowing part of an application to swap does not enable this application to allocate more memory, but could enable some hardware drivers to allocate memory that they need.

Adding swap has no effect on the application's virtual memory space. That's the point of swap, after all — it's transparent to applications.

However, it is possible that adding swap to a machine would have an indirect influence on the fragmentation inside an application's virtual memory space. If the system runs out of virtual memory, then the application will have to make do with what it have. If the application is using most of the memory that it allocated from the operating system, this will over time cause fragmentation inside that space as small blocks are freed here and there. If the application has more virtual memory (some of which is swapped out), this gives the memory manager more room to maneuver and so reduces the risk that the application will run out of memory, with three separate 2kB blocks available when it wanted a single 4kB object.

3
  • Isn't it interesting how every answer leads to a new question? Now I find myself curious about memory management order of operations and what techniques to free memory have to fail before an out of memory condition is reached. My assumption is cache is least important and would be dumped first. I know there's a kernel of truth in here somewhere (pun intended). ;-)
    – Elder Geek
    Commented Mar 21, 2015 at 2:11
  • @ElderGeek - Documentation/cgroups/memory.txt is probably a good place to start. Though it is hopelessly out-of-date it does overview the kernel memory controller pretty well.
    – mikeserv
    Commented Mar 21, 2015 at 13:06
  • @mikeserv Thank you for that. I also found - landley.net/writing/memory-faq.txt which also looks to be "hopelessly out-of-date" but at least it seems my theory regarding the logical dumping of cache first to free RAM prior to resorting to killing processes are accurate. I doubt I'll learn much more about it without delving into the code itself. That might be a bit beyond me as I haven't written any code beyond bash scripting for decades...
    – Elder Geek
    Commented Mar 21, 2015 at 14:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .