Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Low RAM VPS tuning, decrease memory consumption
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Low RAM VPS tuning, decrease memory consumption

ValdikSSValdikSS Member
edited March 21 in Tutorials

:warning: DECREASE RAM USAGE WITH ONE WEIRD TRICK :warning:

LIBPROC_HIDE_KERNEL=1 ps hax --sort=pmem -o pid | head -n 30 | xargs -n1 sudo gdb --batch -ex 'call malloc_trim(0)' --pid

VPS PROVIDERS HATE THIS!


After reading why there are providers who have 512m memory VPS? with the following message:

I am afraid even for a latest wordpress (php8, mysql8, ubuntu 20.0), it can't be run by a 512m vps. so releasing this kind of product just for test?

I decided to write this short post on tuning the performance and decreasing the memory consumption on your VPS and making it work faster under memory pressure.
It won't include in-depth information, this is more a how-to than a tutorial.

All commands are supposed to be run as root (sudo -i).

1. MGLRU

Enables Multi-Gen LRU (improved page reclaim and caching policy).
Prevents thrashing, improves loading speeds under low ram conditions.
Requires kernel 6.1+.
Has dramatic effect especially on slower HDDs.
For very slow HDDs, consider using 1000 instead of 300 for min_ttl_ms.

tee /etc/tmpfiles.d/mglru.conf <<EOF
w-      /sys/kernel/mm/lru_gen/enabled          -       -       -       -       y
w-      /sys/kernel/mm/lru_gen/min_ttl_ms       -       -       -       -       300
EOF

2. ZRAM

Compress memory instead of swapping it to disk.
Greatly improves responsiveness under low ram conditions.
Especially effective on slower HDDs.
For low RAM machines (<=1G) you might want to increase PERCENT to 125 or 150.
There is no contradiction here: the memory is getting compressed only upon swapping out, reaching 2.5-3 times the compression ratio.

apt install zram-tools
sed -i 's/#PERCENT=.*/PERCENT=75/' /etc/default/zramswap

For systems with low RAM and slower HDDs, consider also tuning swappiness to lower page cache reclamation rate.

echo vm.swappiness=100 > /etc/sysctl.d/85-swappiness.conf
echo vm.watermark_scale_factor=200 >> /etc/sysctl.d/85-swappiness.conf

3. glibc MALLOC_ARENA_MAX and MALLOC_TRIM_THRESHOLD

Decrease and set static memory allocation arenas to 2 (1, 2, 3).
Decrease and set static heap trimming threshold down to 64KiB (1).
Reduces overall memory usage.
This applies only to glibc-based applications (not musl on Alpine for example).

sed -i 's/#DefaultEnvironment=.*/DefaultEnvironment=MALLOC_ARENA_MAX=2 MALLOC_TRIM_THRESHOLD_=65536/' /etc/systemd/system.conf
echo -e 'export MALLOC_ARENA_MAX=2\nexport MALLOC_TRIM_THRESHOLD_=65536' > /etc/profile.d/85malloc_arena

Additional files for graphical X11 environment (not for server)

echo -e 'export MALLOC_ARENA_MAX=2\nexport MALLOC_TRIM_THRESHOLD_=65536' > /etc/X11/Xsession.d/85malloc_arena
echo -e 'export MALLOC_ARENA_MAX=2\nexport MALLOC_TRIM_THRESHOLD_=65536' > /etc/X11/xinit/xinitrc.d/85malloc_arena.sh
chmod +x /etc/X11/xinit/xinitrc.d/85malloc_arena.sh

4. Stack size for OpenVZ

Linux uses 8 MB of stack size by default. Nowadays you won't get huge benefit by decreasing stack size on KVM and modern kernels (although it depends on the amount of processes and threads, it still makes sense on thread-per-connection applications), however it has a dramatic effect on OpenVZ due to particular memory counting method used there.

Reduce Stack Limit for Multi-Thread Apps

Set stack size to 512 KiB

sed -i 's/#DefaultLimitSTACK=.*/DefaultLimitSTACK=524288/' /etc/systemd/system.conf
echo 'ulimit -s 512' > /etc/profile.d/85stacksize

5. Eatmydata

Extra!
Fix Debian/Ubuntu apt/dpkg excessive fsync() calls.
Speeds up software installation.

apt install eatmydata
echo -e '#!/bin/bash\nexec eatmydata /usr/bin/dpkg "$@"' > /usr/local/bin/dpkg
chmod +x /usr/local/bin/dpkg
echo 'Dir::Bin::dpkg "/usr/local/bin/dpkg";' > /etc/apt/apt.conf.d/85eatmydata

Comments

  • FatGrizzlyFatGrizzly Member, Host Rep

    Nice write-up @ValdikSS

    I also enjoyed reading your MiTM writeup from a few months ago.

    Thanked by 1ValdikSS
  • zGatozGato Member
    edited March 21

    Not thanked by @oplink

    for context

  • rm_rm_ IPv6 Advocate, Veteran
    edited March 21

    @ValdikSS said: Fix Debian/Ubuntu apt/dpkg excessive fsync() calls.

    I believe the caveat here is that if some daemon is restarted after upgrade during an apt/dpkg call like this, it will inherit the eatmydata behavior for its entire remaining operation. At least in case of a distro not running systemd, with initscripts to kill and start processes called by apt/dpkg directly.

  • matey0matey0 Member

    Nice post but I think it might scare people off from buying 512mb ram servers by making it look like this is necessary to have a usable vps :D It really isn't necessary, at least with Alpine Linux, 512mb of ram is plenty for a lot of things right out of the box. Debian is fine too, can't speak for Ubuntu.

    Thanked by 1cochon
  • tentortentor Member, Patron Provider

    @matey0 said:
    Nice post but I think it might scare people off from buying 512mb ram servers by making it look like this is necessary to have a usable vps :D It really isn't necessary, at least with Alpine Linux, 512mb of ram is plenty for a lot of things right out of the box. Debian is fine too, can't speak for Ubuntu.

    Alpine is not popular among LET because geekbench requires glibc

  • @rm_ said: I believe the caveat here is that if some daemon is restarted after upgrade during an apt/dpkg call […] distro not running systemd, with initscripts to kill and start processes called by apt/dpkg directly.

    Yes, you're right. Shouldn't be that of an issue though.

  • matey0matey0 Member

    @tentor said:

    @matey0 said:
    Nice post but I think it might scare people off from buying 512mb ram servers by making it look like this is necessary to have a usable vps :D It really isn't necessary, at least with Alpine Linux, 512mb of ram is plenty for a lot of things right out of the box. Debian is fine too, can't speak for Ubuntu.

    Alpine is not popular among LET because geekbench requires glibc

    lol

  • This looks neat. going to take notes on this. Cool post btw

Sign In or Register to comment.