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.
Best Answer
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:
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.