Ubuntu sometimes starts swapping like crazy for no reason

Every once in a while, my Ubuntu starts using the hard disk like crazy, and for 3 minutes I can't move the mouse nor access any console, even CTRL-ALT-BACKSPACE and CTRL-ALT-F1 do nothing.

It is usually when doing some action (opening a new browser tab, starting Eclipse, etc).

I think that at those times my Ubuntu starts swapping like crazy, for no reason.
It happens more if I have OpenOffice+Eclipse+Firefox open, but sometimes it also happens with only very simple apps open.

I used iotop to record disk activity during one of those freeze. That time I only had Chrome, gnome-terminal, gedit open. I had started iotop with sudo nice -20 sudo iotop -tbod10 > ~/iotop.log. It is supposed to write to the log every 10 seconds, but during the freeze it did not manage to write anything. However, the log just before the freeze (09:42:23) indicates kswapd is probably the culprit at 12.26 M/s:

    TIME  TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN      IO    COMMAND
09:42:23    27 be/4 root        0.00 B/s   12.26 M/s  0.00 % 99.99 % [kswapd0]
09:42:23  1828 be/4 nico     1081.85 K/s    0.00 B/s  1.78 % 61.16 % compiz
09:42:23 31484 be/4 nico      128.22 K/s  668.16 B/s  0.00 % 50.59 % python /usr/bin/indicator-weather
09:42:23 17339 be/4 nico       89.72 K/s  668.16 B/s  0.00 % 50.32 % google-chrome
09:42:23 17397 be/4 nico      806.49 K/s    0.00 B/s  2.13 % 43.42 % --disable-client-side-phishing-detection --enable-print-preview --disable-webgl --disable-gl-multisampling --disable-accelerated-compositing --channel=17322.0x7fc3880c0700.1895579466
09:42:23   315 be/4 root      334.08 B/s    0.00 B/s  0.00 % 37.90 % [flush-8:0]
09:42:23   297 be/3 root        0.00 B/s 1670.40 B/s  0.00 % 32.57 % [jbd2/sda1-8]
09:42:23  1244 be/4 root      502.75 K/s    0.00 B/s  0.77 % 31.90 % X :0 -nr -verbose -auth /var/run/gdm/auth-for-gdm-XEqu73/database -nolisten tcp vt7
09:42:23 17403 be/5 nico      482.52 K/s    0.00 B/s  0.00 % 30.08 % --disable-client-side-phishing-detection --enable-print-preview --disable-webgl --disable-gl-multisampling --disable-accelerated-compositing --channel=17322.0x7fc386e35000.1143953050
09:42:23 17608 be/4 root        0.00 B/s    0.00 B/s  0.00 % 27.27 % [kworker/0:2]
09:42:23  1857 be/4 nico      430.65 K/s    0.00 B/s  0.00 % 25.40 % indicator-multiload
09:42:23  2178 be/4 nico      100.49 K/s    0.00 B/s  0.00 % 23.91 % dropbox
09:42:23 17340 be/4 nico      448.59 K/s    0.00 B/s  3.66 % 18.24 % google-chrome
09:42:23 17322 be/4 nico      408.79 K/s    0.00 B/s  0.25 % 16.66 % google-chrome
09:42:23 17408 be/5 nico      162.47 K/s    0.00 B/s  0.37 % 15.75 % --disable-client-side-phishing-detection --enable-print-preview --disable-webgl --disable-gl-multisampling --disable-accelerated-compositing --channel=17322.0x7fc387166c40.2054812173
09:42:23  1836 be/4 nico      278.62 K/s    0.00 B/s 72.10 % 14.14 % nautilus
09:42:23  9266 be/4 root       78.63 K/s    0.00 B/s  0.00 % 12.74 % adb fork-server server
09:42:23 17417 be/5 nico        0.00 B/s    0.00 B/s  0.00 % 11.63 % --disable-client-side-phishing-detection --enable-print-preview --disable-webgl --disable-gl-multisampling --disable-accelerated-compositing --channel=17322.0x7fc386e35000.1143953050
09:42:23 17414 be/5 nico        9.79 K/s    0.00 B/s  0.00 % 11.63 % --disable-client-side-phishing-detection --enable-print-preview --disable-webgl --disable-gl-multisampling --disable-accelerated-compositing --channel=17322.0x7fc387166c40.2054812173
09:42:23 17416 be/4 nico      668.16 B/s    0.00 B/s  0.00 % 11.63 % --disable-client-side-phishing-detection --enable-print-preview --disable-webgl --disable-gl-multisampling --disable-accelerated-compositing --channel=17322.0x7fc3880c0700.1895579466
09:42:23 17237 be/4 postfix     5.55 K/s    0.00 B/s 20.29 %  9.59 % pickup -l -t fifo -u -c
09:42:23 17500 be/4 nico       27.73 K/s    0.00 B/s  0.00 %  9.39 % npwrapper.libflashplayer.so --lang=en-US --channel=17322.0x7fc38990c2c0.322771554
09:42:23 17399 be/4 nico       17.29 K/s    0.00 B/s  0.00 %  8.67 % --disable-client-side-phishing-detection --enable-print-preview --disable-webgl --disable-gl-multisampling --disable-accelerated-compositing --channel=17322.0x7fc3880c0700.1895579466
09:42:23   584 be/4 root       87.44 K/s    0.00 B/s  0.00 %  6.36 % NetworkManager
09:42:23 17533 be/4 nico       47.96 K/s    0.00 B/s  0.00 %  5.88 % GoogleTalkPlugin
09:42:23  2218 be/4 nico      173.24 K/s    0.00 B/s  0.00 %  5.78 % unity-panel-service
09:42:23  1462 be/4 mysql      38.82 K/s    0.00 B/s  0.00 %  5.76 % mysqld
09:42:23  1463 be/4 mysql       5.87 K/s    0.00 B/s  0.00 %  5.65 % mysqld
09:42:23 29621 be/4 nico       32.95 K/s    0.00 B/s  0.00 %  4.43 % python /usr/bin/indicator-weather
09:42:23  1382 be/4 root       39.48 K/s    0.00 B/s  0.00 %  4.15 % apache2 -k start
09:42:23 17513 be/4 nico      119.41 K/s    0.00 B/s  0.00 %  4.06 % npviewer.bin --plugin /usr/lib/flashplugin-installer/libflashplayer.so --connection /org/wrapper/NSPlugins/libflashplayer.so/17500-2
09:42:23 17335 be/4 nico       21.86 K/s 1670.40 B/s  0.00 %  4.02 % google-chrome
09:42:23  2201 be/4 nico       10.11 K/s    0.00 B/s  0.00 %  3.85 % bamfdaemon
09:42:23  1842 be/4 nico       30.34 K/s    0.00 B/s  0.00 %  3.23 % gnome-power-manager
09:42:23  1812 be/4 nico       26.10 K/s    0.00 B/s  0.00 %  2.89 % gnome-settings-daemon
09:42:23  1848 be/4 nico       38.17 K/s    0.00 B/s  0.00 %  2.55 % indicator-sensors
09:42:23 12233 be/4 root       31.32 K/s 1002.24 B/s  2.06 %  2.54 % python /usr/bin/iotop -tbod10
09:42:23  1915 be/4 nico       10.11 K/s    0.00 B/s  0.00 %  2.27 % gvfs-afc-volume-monitor
09:42:23  1831 be/4 nico       10.44 K/s    0.00 B/s  0.00 %  2.13 % compiz
09:42:23  1464 be/4 mysql      20.88 K/s    0.00 B/s  0.00 %  1.84 % mysqld
09:42:23  2309 be/4 nico       18.92 K/s    0.00 B/s  0.00 %  1.79 % unity-applications-daemon
09:42:23 17337 be/4 nico        9.14 K/s    0.00 B/s  0.00 %  1.74 % google-chrome
09:42:23 17343 be/4 nico       13.38 K/s    0.00 B/s  0.00 %  1.72 % google-chrome
09:42:23  1252 be/4 root       51.55 K/s    0.00 B/s  0.00 %  1.65 % master
09:42:23  1907 be/4 root       18.27 K/s    0.00 B/s  0.00 %  1.21 % udisks-daemon: polling /dev/sr0
09:42:23  2186 be/4 nico       46.00 K/s    0.00 B/s  0.00 %  1.13 % python /usr/share/system-config-printer/applet.py
09:42:23  1862 be/4 nico        5.22 K/s    0.00 B/s  0.00 %  0.98 % indicator-multiload
09:42:23  2199 be/4 nico       26.10 K/s    0.00 B/s  0.00 %  0.90 % unity-window-decorator
09:42:23   906 be/4 root        2.28 K/s    0.00 B/s  0.00 %  0.82 % irqbalance
09:42:23   580 be/4 avahi      32.63 K/s    0.00 B/s  0.00 %  0.59 % avahi-daemon: running [nico.local]
09:42:23  1877 be/4 nico       18.60 K/s    0.00 B/s  0.00 %  0.57 % notify-osd
09:42:23 17360 be/4 nico       43.72 K/s    0.00 B/s 26.23 %  0.51 % --disable-client-side-phishing-detection --enable-print-preview --disable-webgl --disable-gl-multisampling --disable-accelerated-compositing --channel=17322.0x7fc38672e8c0.1339188300
09:42:23  1800 be/4 nico       27.73 K/s    0.00 B/s  0.00 %  0.34 % dbus-daemon --fork --print-pid 5 --print-address 7 --session
09:42:23   627 be/4 root       10.44 K/s    0.00 B/s  0.00 %  0.33 % wpa_supplicant -u -s
09:42:23  2243 be/4 nico        3.59 K/s    0.00 B/s  0.00 %  0.33 % indicator-application-service
09:42:23  2284 be/4 nico        6.20 K/s    0.00 B/s  0.00 %  0.19 % update-notifier
09:42:23  2234 be/4 nico        0.00 B/s    0.00 B/s  0.00 %  0.18 % indicator-application-service
09:42:23  2220 be/4 nico        4.24 K/s    0.00 B/s  0.00 %  0.10 % unity-panel-service
09:42:23 17362 be/4 nico        7.83 K/s    0.00 B/s  2.54 %  0.00 % --disable-client-side-phishing-detection --enable-print-preview --disable-webgl --disable-gl-multisampling --disable-accelerated-compositing --channel=17322.0x7fc38672e540.2001766922
09:42:23 17412 be/5 nico        0.00 B/s    0.00 B/s  9.26 %  0.00 % --disable-client-side-phishing-detection --enable-print-preview --disable-webgl --disable-gl-multisampling --disable-accelerated-compositing --channel=17322.0x7fc3880afa80.2009126862

    TIME  TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN      IO    COMMAND
09:45:22 17596 be/4 root        0.00 B/s    0.00 B/s  0.00 % 99.99 % [kworker/1:0]
09:45:22 17608 be/4 root        0.00 B/s    0.00 B/s  0.00 % 98.44 % [kworker/0:2]
09:45:22   315 be/4 root      808.56 B/s   17.81 K/s  0.00 % 86.79 % [flush-8:0]
09:45:22  1828 be/4 nico      621.79 K/s    0.00 B/s 16.33 % 86.29 % compiz
09:45:22 17340 be/4 nico      815.78 K/s    0.00 B/s 14.61 % 83.30 % google-chrome
09:45:22  1857 be/4 nico      575.28 K/s    0.00 B/s 12.32 % 82.64 % indicator-multiload
09:45:22  1812 be/4 nico      540.14 K/s    0.00 B/s  3.06 % 81.94 % gnome-settings-daemon
09:45:22  1848 be/4 nico      541.02 K/s    0.00 B/s 12.30 % 81.58 % indicator-sensors
09:45:22   584 be/4 root      606.07 K/s    0.00 B/s 11.51 % 78.40 % NetworkManager
09:45:22   906 be/4 root      308.53 K/s    0.00 B/s  0.00 % 76.26 % irqbalance
09:45:22 17335 be/4 nico       18.79 K/s  332.94 B/s  8.56 % 60.25 % google-chrome
09:45:22  1382 be/4 root      361.76 K/s    0.00 B/s  0.00 % 49.17 % apache2 -k start
09:45:22 17533 be/4 nico      282.38 K/s    0.00 B/s  0.00 % 45.30 % GoogleTalkPlugin
09:45:22 17550 be/4 nico      134.88 K/s    0.00 B/s  0.00 % 40.96 % npviewer.bin --plugin /usr/lib/flashplugin-installer/libflashplayer.so --connection /org/wrapper/NSPlugins/libflashplayer.so/17500-2
09:45:22  2309 be/4 nico      260.08 K/s    0.00 B/s  0.00 % 38.69 % unity-applications-daemon
09:45:22  1463 be/4 mysql     171.28 K/s    0.00 B/s  0.00 % 38.61 % mysqld
09:45:22 17360 be/4 nico      377.27 K/s    0.00 B/s  2.14 % 37.14 % --disable-client-side-phishing-detection --enable-print-preview --disable-webgl --disable-gl-multisampling --disable-accelerated-compositing --channel=17322.0x7fc38672e8c0.1339188300
09:45:22  1888 be/4 nico      177.20 K/s    0.00 B/s  8.69 % 35.51 % python /usr/lib/desktopcouch/desktopcouch-service
09:45:22  2172 be/4 nico       64.52 K/s    0.00 B/s  3.93 % 35.19 % dropbox
09:45:22 17536 be/4 nico       71.95 K/s    0.00 B/s  0.00 % 32.79 % npviewer.bin --plugin /usr/lib/flashplugin-installer/libflashplayer.so --connection /org/wrapper/NSPlugins/libflashplayer.so/17500-2
09:45:22  9266 be/4 root      106.53 K/s    0.00 B/s  0.00 % 28.34 % adb fork-server server
09:45:22 17351 be/4 nico      225.76 K/s    0.00 B/s  1.69 % 27.98 % --disable-client-side-phishing-detection --enable-print-preview --disable-webgl --disable-gl-multisampling --disable-accelerated-compositing --channel=17322.0x7fc3866eae00.1806036416
09:45:22 17412 be/5 nico      165.21 K/s    0.00 B/s  0.00 % 24.66 % --disable-client-side-phishing-detection --enable-print-preview --disable-webgl --disable-gl-multisampling --disable-accelerated-compositing --channel=17322.0x7fc3880afa80.2009126862
09:45:22  2179 be/4 nico       58.25 K/s    0.00 B/s 24.18 % 22.89 % dropbox
09:45:22  2178 be/4 nico       29.63 K/s    0.00 B/s  0.00 % 22.83 % dropbox
09:45:22  2284 be/4 nico      139.46 K/s    0.00 B/s  3.82 % 21.98 % update-notifier
09:45:22   627 be/4 root       89.74 K/s    0.00 B/s 74.21 % 21.71 % wpa_supplicant -u -s
09:45:22  2218 be/4 nico      129.89 K/s    0.00 B/s 76.33 % 21.59 % unity-panel-service
09:45:22 17532 be/4 nico       94.64 K/s    0.00 B/s  0.00 % 21.24 % GoogleTalkPlugin
09:45:22  9652 be/4 nico       12.91 K/s    0.00 B/s  0.00 % 20.14 % ruby script/rails console
09:45:22 17408 be/5 nico      152.30 K/s    0.00 B/s 83.52 % 17.19 % --disable-client-side-phishing-detection --enable-print-preview --disable-webgl --disable-gl-multisampling --disable-accelerated-compositing --channel=17322.0x7fc387166c40.2054812173
09:45:22 17362 be/4 nico      178.41 K/s    0.00 B/s  1.63 % 16.11 % --disable-client-side-phishing-detection --enable-print-preview --disable-webgl --disable-gl-multisampling --disable-accelerated-compositing --channel=17322.0x7fc38672e540.2001766922
09:45:22  1907 be/4 root       93.99 K/s    0.00 B/s  0.00 % 15.71 % udisks-daemon: polling /dev/sr0
09:45:22    27 be/4 root        0.00 B/s  136.18 M/s  0.00 % 15.66 % [kswapd0]
09:45:22  1915 be/4 nico       30.82 K/s    0.00 B/s  0.00 % 13.92 % gvfs-afc-volume-monitor
09:45:22 17403 be/5 nico      107.90 K/s    0.00 B/s 89.46 % 12.11 % --disable-client-side-phishing-detection --enable-print-preview --disable-webgl --disable-gl-multisampling --disable-accelerated-compositing --channel=17322.0x7fc386e35000.1143953050
09:45:22  1839 be/4 nico       60.13 K/s    0.00 B/s 21.61 %  9.98 % nm-applet --sm-disable
09:45:22  1244 be/4 root      105.58 K/s    0.00 B/s 84.86 %  9.73 % X :0 -nr -verbose -auth /var/run/gdm/auth-for-gdm-XEqu73/database -nolisten tcp vt7
09:45:22  1462 be/4 mysql      29.35 K/s    0.00 B/s  9.90 %  8.99 % mysqld
09:45:22  1464 be/4 mysql      26.41 K/s    0.00 B/s  0.00 %  8.55 % mysqld

I am using Ubuntu 2011.04 with encrypted home and 4GB physical memory + 4GB swap:

free -m
             total       used       free     shared    buffers     cached
Mem:          3894       3044        850          0         94        935
-/+ buffers/cache:       2013       1880
Swap:         4023        942       3081

I have much more RAM that I actually need, so this swapping craziness is baffling.
How can I stop those freezes?

UPDATE: As asked, here is the output of hdparm -Idc /dev/sda1:

/dev/sda1:
 IO_support    =  1 (32-bit)
 HDIO_GET_DMA failed: Inappropriate ioctl for device

ATA device, with non-removable media
    Model Number:       FUJITSU MJA2500BH G2                    
    Serial Number:      K95FT9A26PNT
    Firmware Revision:  0084001C
    Transport:          Serial, ATA8-AST, SATA 1.0a, SATA II Extensions, SATA Rev 2.5, SATA Rev 2.6
Standards:
    Used: ATA-8-ACS revision 3f 
    Supported: 8 7 6 5 
Configuration:
    Logical     max current
    cylinders   16383   16383
    heads       16  16
    sectors/track   63  63
        --
    CHS current addressable sectors:   16514064
    LBA    user addressable sectors:  268435455
    LBA48  user addressable sectors:  976773168
    Logical  Sector size:                   512 bytes
    Physical Sector size:                   512 bytes
    device size with M = 1024*1024:      476940 MBytes
    device size with M = 1000*1000:      500107 MBytes (500 GB)
    cache/buffer size  = 8192 KBytes (type=DualPortCache)
    Form Factor: 2.5 inch
    Nominal Media Rotation Rate: 5400
Capabilities:
    LBA, IORDY(can be disabled)
    Queue depth: 32
    Standby timer values: spec'd by Standard, no device specific minimum
    R/W multiple sector transfer: Max = 16  Current = 16
    Advanced power management level: disabled
    Recommended acoustic management value: 254, current value: 254
    DMA: mdma0 mdma1 mdma2 udma0 udma1 udma2 udma3 udma4 *udma5 
         Cycle time: min=120ns recommended=120ns
    PIO: pio0 pio1 pio2 pio3 pio4 
         Cycle time: no flow control=120ns  IORDY flow control=120ns
Commands/features:
    Enabled Supported:
       *    SMART feature set
            Security Mode feature set
       *    Power Management feature set
       *    Write cache
       *    Look-ahead
       *    Host Protected Area feature set
       *    WRITE_BUFFER command
       *    READ_BUFFER command
       *    DOWNLOAD_MICROCODE
            Advanced Power Management feature set
            SET_MAX security extension
       *    Automatic Acoustic Management feature set
       *    48-bit Address feature set
       *    Device Configuration Overlay feature set
       *    Mandatory FLUSH_CACHE
       *    FLUSH_CACHE_EXT
       *    SMART error logging
       *    SMART self-test
       *    General Purpose Logging feature set
       *    WRITE_{DMA|MULTIPLE}_FUA_EXT
       *    64-bit World wide name
       *    IDLE_IMMEDIATE with UNLOAD
       *    Disable Data Transfer After Error Detection
       *    WRITE_UNCORRECTABLE_EXT command
       *    {READ,WRITE}_DMA_EXT_GPL commands
       *    Segmented DOWNLOAD_MICROCODE
       *    Gen1 signaling speed (1.5Gb/s)
       *    Gen2 signaling speed (3.0Gb/s)
       *    Native Command Queueing (NCQ)
       *    Host-initiated interface power management
       *    Phy event counters
       *    NCQ priority information
       *    DMA Setup Auto-Activate optimization
            Device-initiated interface power management
       *    Software settings preservation
       *    SMART Command Transport (SCT) feature set
       *    SCT LBA Segment Access (AC2)
       *    SCT Error Recovery Control (AC3)
       *    SCT Features Control (AC4)
       *    SCT Data Tables (AC5)

Result of sdparm /dev/sda:

    /dev/sda: ATA       FUJITSU MJA2500B  0084
Read write error recovery mode page:
  AWRE        1
  ARRE        0
  PER         0
Caching (SBC) mode page:
  WCE         1
  RCD         0
Control mode page:
  SWP         0

Result of smartctl -l selftest /dev/sda:

SMART Self-test log structure revision number 1
Num  Test_Description    Status                  Remaining  LifeTime(hours)  LBA_of_first_error
# 1  Vendor offline      Completed without error       00%         0         -

Solution 1:

It sounds more like you have bad disk behavior. Have you checked the hdparm output to make sure that you're using DMA? Simple disk access shouldn't freeze the system up unless there's something else going on. I'd look at the dmesg output and install the smartmontools to see if your drive is tossing out ATA errors or if SMART thinks the drive is dying.

And do what jnv said by cranking swappiness down; Linux prefers by default to swap idle programs out in favor of caching data from the hard disk, and with an encrypted homedir, there's a lot more disk activity to cache (you might consider not using homedir encryption, and just use gpg or similar to encrypt stuff you actually care about). :)

Solution 2:

You could try these two things to reduce swapping:

  1. Reduce swappiness (60 is default, try setting it to 10)
  2. Try zRam (This will first compress data in your memory before swapping it to harddisk)

Solution 3:

Upgrading to Ubuntu 2014.04 solved the problem.

(just an upgrade, not reinstall)

So it was actually not a hardware problem :-)