Improve Your Librem 5's Performance (with this one weird trick)

Good morning folks! I’m new to the forum here, so I’ll introduce myself briefly below the body of my post. :smiley: First, let’s dive right in…

One Weird Trick

I got a Librem 5 off eBay a little less than a week ago, and I’ve been experimenting with it specifically for the purpose of using it as a daily driver. Unfortunately, in this process I’ve noticed the system behaving extremely sluggish at times; apps being slow to load, laggy scrolling, and even random app crashes.

Upon investigation, I found this happening:

Screenshot 2024-02-05 at 3.12.51 PM

Yes, the CPU is pegged, but not because of any useful work. It’s pegged because the system is thrashing swapping pages in and out of zram. The memory pressure is way too high, and this is a sign of a system that is about to crash.

How do fix it? Well, I only have two options. Do less on my phone or add a swap file!

Here’s what I did (do not run these commands unless you know what you are doing!):

dd if=/dev/zero bs=1024 count=2097152 of=/swapfile
chown root:root /swapfile
chmod 600 /swapfile
mkswap /swapfile
echo '/swapfile none swap sw 0 0' >>/etc/fstab

Then reboot!

Note: These commands are intended to be run from a root shell. I didn’t prefix anything with sudo because I would really not like for someone to blindly copy/paste and ruin their phone.

Another Note: I’m running the latest stable Byzantium image, with all package updates installed.

Now my /proc/swaps looks like this:

Filename    Type       Size     Used      Priority
/swapfile   file       2097148  1495000   -2
/dev/zram0  partition  1484796  1443912   100

And voila, my system has been way snappier, even when I have multiple Firefox tabs open, waydroid, and several other apps.

How do I know if I need more swap?

Check your usage app! (Check below for the screenshot. As a new user I’m only allowed one embedded media item per post.)

If your RAM and/or Swap are ever over 80%, you’re approaching the danger zone.

But I already have zram!

That’s just a compressed area in RAM. It doesn’t give you more RAM, it just compresses pages into and out of RAM. It’s fast, and it’s better than nothing, but once the system memory pressure starts getting too high it’s not going to save you.

If I add a swap file, why bother keeping the zram swap?

The swap file is way slower than zram, so it is added to the swap list at a much lower priority. Ideally, the system will move very old / rarely accessed pages to the swap file and make room for much more frequently accessed pages in RAM and zram.

Won’t this burn out my MMC?

Sure, eventually. Here are a few reasons I’m not worried about it:

  • I expect mmcblk0 to have implemented wear-leveling inside the hardware. (I didn’t look up the data sheet, so correct me if I’m wrong.) So no individual file can create a high-wear area on the chip.
  • As mentioned above, this swapfile is a lower priority than zram, so I’m minimizing the number of writes to the swapfile.
  • I’d rather wear out this phone with daily use in an actually usable configuration instead of tossing it in a drawer in disappointment. (With any luck, this phone will burn out around the time Puri.sm starts shipping something with a modern SoC.)

Who are you?

Hi! I’m Alex Markley. As mentioned at the beginning, I’m new here. I have a background in software and I have some professional experience with embedded Linux systems.

I’ve been using Linux for over 20 years now, often as a daily driver either at home or at work or both.

My Librem 5 is my first true mobile Linux device since my Compaq iPaq running Familar Linux (man I loved that thing). I’m really excited by the recent renaissance of activity around Linux Smartphones getting ready for prime time, and I felt it was high time to roll up my sleeves and dive in!

32 Likes

This is the usage app screenshot I was planning on uploading above:

13 Likes

Welcome to the Purism community forums. Feel free to create your own dedicated introductory thread in the Introductions category.

Here are the Terms of Service, if you have not read it already.

Terms of Service - Purism community

2 Likes

Welcome and thank you.

4 Likes

Strong first post. Welcome. Hope you enjoy your first pocket computer. :slightly_smiling_face:

Btw first I wanted to say “I have no lags”, since I don’t use it near or over RAM limit. So it’s also a bit user dependent.

3 Likes

“…with this one weird trick”

:rofl:
Love it! Welcome!

6 Likes

It is definitely going to be highly user-dependent. I’m certain Waydroid is what pushed me over the edge, but personally I’m not going to be able to use the phone as a daily driver without it. :man_shrugging:

6 Likes

You should look again when stable Crimson is rolled out. If it’s not implemented then, we should speak with Purism about it. I guess it should be standard to have a swap file, especially with “only” 3GB RAM.

Edit:
Wait, it’s there. Just too little for you, after comparing with my device.

2 Likes

Of course, the other weird trick that is recommended to solve this is shelling out cash for the Liberty Phone :sweat_smile:

4 Likes

One question: let’s say I implement this “one trick”, how do unimplement it? How do I go backwards if I change my mind?

2 Likes

Take a look at the swap device. If it’s /dev/zram0 then it’s not a “real” swap device, it’s just compressed RAM. So your swap isn’t adding more RAM, it’s actually competing with RAM to provide a compressed zone for unused pages to land temporarily.

The efficiency of this approach is going to heavily depend on how compressible the pages are. How much you can fit in there is going to depend on the compression ratio of the pages which are getting swapped out.

Either way, you’re not getting 3GB of RAM, you’re getting some fraction of that because zram is competing with “real” RAM.

I’d always recommend having a “real” swap device which can page out to slow durable media if you’re using Linux. The reason Apple (for example) can get away with not doing this is because they are so incredibly aggressive about reaping processes that aren’t directly in front of the user’s face.

Linux doesn’t do this by default, and (I would argue) it shouldn’t. There’s definitely a larger conversation to be had here about how to manage high-pressure scenarios to both empower the user and also provide a good user experience for folks who don’t know what “swap” even is.

Also, regarding Crimson, I definitely tried it the other day and audio was broken… :rofl: So I’m not holding my breath.

8 Likes

Oh, great question! This is easy to reverse.

First, open /etc/fstab and delete the line with /swapfile.

Second, either swapoff or reboot (whichever seems more intuitive).

Third, delete /swapfile from your system.

That will completely unimplement with zero lingering side effects.

6 Likes

I see. Just was confused about usage app (seems like zram = swap inside this app). Thanks for explanations.

Nothing unexpected. That’s why I spoke about stable release.

3 Likes

Thank you for the great technical article. I love learning new things. I have set this up and will give it a test drive. I may choose to undo this eventually because there isn’t much space on my internal storage to be using up 2 more gigs to swap space. My takeaway may be to keep an eye on memory usage and if it gets too high (over 80%) be sure to close apps I don’t need at this moment.

2 Likes

I opened a bunch of apps. (Usage, Nheko, Geary, Phone, Browser (several tabs open to car manufacturers), Tuba, Chatty, Evolution Calendar, Contacts, Weather, Pure Maps, Terminal, QModMaster, Watch Mate, VLC). Even with all this going on it seems the new swap space isn’t being used. I guess it doesn’t benefit me or am I missing something?

3 Likes

Oh yeah you are correct. As long as the system reports 0 “Used” on /swapfile, it hasn’t needed to swap out to the MMC at all.

It seems like you only have a couple of apps taking up 100+ MB of RAM, so that’s probably why. (In my case, I had Waydroid taking up over 1 GB all by itself.)

I also have several thousand contacts (and a very full calendar) so those apps are both taking up over 100MB of RAM each. And Podcasts, which uses 140MB also.

Side note, @adamd, are we mutual followers on Mastodon? I’m @alex@mbe.tv

4 Likes

Purism “Hire this man”…lol.

6 Likes

Welcome! I hope you enjoy your Librem 5 like many of us do!

If you have any additional tips, I recommend sharing them in the Librem 5 wiki, particularly the Tips & Tricks section. Also, if you have any additional information about swap files, feel free to make edits or corrections to the section about them.

Again, welcome to the forums and I hope to see you around! :smile:

5 Likes

Last night I was using my L5 as I normally do and just for the fun of it I checked memory usage.

I wasn’t doing anything special. I was just catching up on Mastodon reading using Tuba and surfing the web. There were several other open apps but I wasn’t trying to use a lot of ram. Just doing what I do. So it seems this trick does have benefits for my use case.

7 Likes

This trick is a subtle hint that Librem 5 RAM is already too small 2024.

5 Likes