linux - Does the 4 GB RAM limit on 32-bit systems also affect the swap space?

20
2014-01
  • Sekhemty

    It is well known that 32-bit systems can only handle 4 GB as maximum amount of RAM.

    Under Linux, does this limit also apply to swap partitions? If my RAM + swap exceeds 4 GB, will it be fully used?

    If the answer is "no, it doesn't apply to swap", why?

  • Answers
  • Darth Android

    No, Imagine RAM is like your desk, and swap space/ virtual memory is like a filing cabinet. You can put lots of information into a filing cabinet, but it's difficult to access. Any papers you want to use, you have to pull out and put them on the desk; the problem is the desk can only hold so many papers at once.

    32-bit machines can only have a desk that's 4GB (2^32, or about 4 billion bytes) big, but they can still have nearly unlimited filing cabinets. They just have to "swap" out papers when the desk is full and they need to look at another page that's in a filing cabinet.


    The reason it doesn't apply to swap is because accessing the contents of RAM or executing code from it is limited to the size of the numbers that it can work with. Think of it like you can only have 4 billion words on the desk, because that's as high as you can count, and you need to work directly with words. "Word 3,547 is mispelled. Change word 3,452,780 to 'Hello'. Delete word 6."

    Now, each page might have 4,000 words on it. The catch is when you're referencing swap space, you don't reference it by word, only by page. So you can still only count to 4 billion, but you can count to 4 billion pages. 4 billion pages actually holds 16 trillion words - much, much higher than you can count.

    64-bit machines can just count higher, so they can put all the pages on the desk at once, if they have a large enough desk. Even if you put a super-large desk in a 32-bit machine, you can still only count to 4 billion, so you have no way to reference all the extra words on the desk.

  • David Marshall

    You are confusing physical and virtual memory.

    The RAM is physical memory. The swap space can contain many 4GB virtual address spaces.


  • Related Question

    linux - Releasing swap space
  • Robert Munteanu

    At times the kernel seems to be intent on keeping e.g. firefox in swap, even though there is enough physical memory available and I'm using firefox:

    robert@rm:~> free -m
                 total       used       free     shared    buffers     cached
    Mem:          3009       1904       1104          0        109        679
    -/+ buffers/cache:       1116       1892
    Swap:         4102        885       3216
    

    Is there a command to instruct the kernel to drop swap space and use the available free memory?


    Update: although in the short term I've used swapon/swapoff, I now set

    sudo sysctl vm.swappiness=30
    

    as suggested in another response, and get very good results.


  • Related Answers
  • nik

    You could do a swapoff -- will need root privileges,
    but, I guess that is not a problem for you.

  • nagul

    You could do what nik suggested and use swapoff. However, there is another, more elegant way to tweak "swappiness", or how aggressively the kernel swaps programs out to disk in systems running the 2.6 kernels.

    There have been heated debates on the linux kernel mailing lists about the policy the kernel should follow regarding swapping behaviour. The upshot is that we now have a patch in 2.6 kernels that lets us tweak this behaviour to a large extent.

    Note that you need root privileges to do this, as you would to run the swapoff/swapon commands.

    The current value of "swappiness" can be inspected in the /proc/sys/vm/swappiness file, or by running this sysctl command:

    sudo sysctl vm.swappiness
    

    The "swappiness" values can range from 0 (no swapping) to 100 (swap to disk as much as possible). Ubuntu ships with the default swappiness set to 60.

    To change this for a session, call sysctl again and pass it a swappiness value to use:

    sudo sysctl vm.swappiness=30
    

    Besides free, you can of course monitor the effects of doing this via the excellent htop or iotop utilities.

    If you like what you see, and want to retain this value across reboots, just place "vm.swappiness=30" in the /etc/sysctl.conf file.

    $ sudo sysctl vm.swappiness
    vm.swappiness = 30
    $ sudo sysctl vm.swappiness=40
    vm.swappiness = 40
    $ sudo sysctl vm.swappiness
    vm.swappiness = 40
    $ sudo tail /etc/sysctl.conf 
    #net.ipv4.conf.all.accept_source_route = 0
    #net.ipv6.conf.all.accept_source_route = 0
    #
    # Log Martian Packets
    #net.ipv4.conf.all.log_martians = 1
    #
    # The contents of /proc/<pid>/maps and smaps files are only visible to 
    # readers that are allowed to ptrace() the process
    # kernel.maps_protect = 1
    vm.swappiness=30
    

    You can play with different values till you find one that entails an acceptable level of swapping on your machine.

  • David Spillett

    Remember that the stuff in swap was put there at a busier time than now.

    You might well find that the data is also in RAM - having been loaded back for use but not wiped from swap. The kernel does this to save time in the next busy period - if the data is already copied there it can just release the RAM it is using instead of having to do disk writes first. So unless you know the machine is not going to need to swap again in the near future, forcing the freeing of swap space may achieve nothing except to slow things down a little more next time swapping is needed.

    This is managed on a delete-on-write basis, so if the copy in RAM is updated the blocks in swap will be released.

    If the swap space is needed for something else, which it won't be at the moment as you have plenty free, it will of course be released for that use.

    You can't see how much data is in this state (of being both in disk-based pages in swap areas, and in RAM) in the output from free. You can see it via the /proc special filesystem though. Look at the output of cat /proc/meminfo for the SwapCached count.

    Another two reasons for there to be swap in use when there is apparently spare memory are

    • when under heavy I/O load the kernel has decided that pages that haven't been used for ages is better swapped out so it can use the RAM for cache/buffers, though looking at your free output that is probably not the case here as you have a chunk of truly unallocated RAM as well as that used by IO cache/buffers.
    • the pages were swapped out for some reason earlier, and just hasn't been needed again since - perhaps it is memory in use by a process that has been inactive for some time. In this case releasing the swap (so loading the pages back into RAM) may improve the response time of that process next time it is needed to do something other than sleep, but if it has been inactive for some time already it might not be needed at any time in the near future anyway.
  • mas

    Remember that free is just a snapshot of memory usage. The result you are seeing may imply that, at some point in the past, the system was getting short of RAM so swapped-out RAM-resident material. Since then, RAM has become free but the material on swap has not been required so is still held on swap rather than just cluttering up RAM. If that is the case, turning off swap would be a bad idea. Are there any processes that could demand such a large amount of RAM since the last reload?

    Additionally, you may want to check that you don't have a restriction on maximum resident size set via ulimit (usually in /etc/profile but maybe it varies with distributions and it can be set per-process (e.g. in a launch script)).

    Finally, on Microsoft Windows there are reports of specific issues with Firefox being swapped out to disk, e.g. when minimised (e.g. http://joeabiraad.com/mozilla-firefox/control-your-firefox-ram-usage/113). I've not heard of that on GNU/Linux systems but it may be worth exploring.