Reduce Akonadi memory usage to 40 MB

By default Akonadi uses MySQL and lots of agents, so total memory usage could be ~300-400 MB right from the start. If you don't need all of its capabilities, there is a way to make it use less memory.

  1. Replace MySQL with SQLite:

    sudo apt install akonadi-backend-sqlite
    sed -i 's/^Driver=QMYSQL$/Driver=QSQLITE3/' ~/.config/akonadi/akonadiserverrc
    akonadictl restart
    
  2. Install Akonadi console:

    sudo apt install akonadiconsole
    
  3. Launch Akonadi console GUI and remove unneeded agents there. You probably don't want to remove migration (used to make sure your data format is properly updated on Akonadi updates) and indexing (lets you search your data) agents, though.

  4. There are some agents which will start (and reappear in Akonadi console) even if you remove them. That's because they have autostart capability in agent's .desktop file by default.

    grep -rl 'Autostart' /usr/share/akonadi/agents
    
    /usr/share/akonadi/agents/maildispatcheragent.desktop
    /usr/share/akonadi/agents/migrationagent.desktop
    /usr/share/akonadi/agents/newmailnotifieragent.desktop
    /usr/share/akonadi/agents/akonadiindexingagent.desktop
    

    As I said before, you probably don't want to disable migration and indexing agents, so I'll disable 'Mail dispatcher' and 'New mail notifier' agents autostart. To do that, override default agent's .desktop by copying it from from /usr/share/akonadi/agents to ~/.local/share/akonadi/agents, then remove Autostart from X-Akonadi-Capabilities:

    (
      sys_dir="/usr/share/akonadi/agents"
      user_dir="$HOME/.local/share/akonadi/agents"
    
      mkdir -p "$user_dir"
      cp "$sys_dir/maildispatcheragent.desktop" "$user_dir"
      cp "$sys_dir/newmailnotifieragent.desktop" "$user_dir"
      sed -i '/^X-Akonadi-Capabilities/s/,Autostart//' "$user_dir"/*
    )
    
  5. Optionally, restart Akonadi: akonadictl restart

As I only need contacts, I've disabled almost all agents, except contacts, indexing and migration. Akonadi now uses 39.3 MB memory:

ps_mem -p `pgrep -d, akonadi`
 Private  +   Shared  =  RAM used       Program

  5.1 MiB + 896.5 KiB =   6.0 MiB       akonadiserver
  6.4 MiB +   1.4 MiB =   7.9 MiB       akonadi_control
  6.4 MiB +   1.5 MiB =   7.9 MiB       akonadi_contacts_resource
  6.4 MiB +   1.6 MiB =   8.0 MiB       akonadi_migration_agent
  7.7 MiB +   1.9 MiB =   9.5 MiB       akonadi_indexing_agent
---------------------------------
                         39.3 MiB
=================================

UPD: I now also use calendar agent, which adds ~10 MB:

ps_mem -p `pgrep -d, akonadi`
 Private  +   Shared  =  RAM used       Program

  6.4 MiB +   1.4 MiB =   7.8 MiB       akonadi_control
  6.4 MiB +   1.5 MiB =   7.9 MiB       akonadi_contacts_resource
  6.4 MiB +   1.6 MiB =   8.0 MiB       akonadi_migration_agent
  7.1 MiB +   1.1 MiB =   8.2 MiB       akonadiserver
  6.7 MiB +   1.6 MiB =   8.3 MiB       akonadi_ical_resource
  7.8 MiB +   1.7 MiB =   9.5 MiB       akonadi_indexing_agent
---------------------------------
                         49.8 MiB
=================================