Custom ZRAM Optimization (4 GB + Swappiness=20) with example for Xperia 10 III and tested on C2 too— Guide Available

It may be possible to create a systemd .swap unit file which successfully activates a swap file on /home after that has been mounted.

BUT it would have to be very carefully written, so it does not prevent the built-in swap target from coming up. High risk of the system not booting if done wrong.

It would also have to be written i a way it doesn’t cause problems on shutdown (because it may either prevent /home from being unmounted on shutdown OR a part of swap becomes unavailable suddenly which is equivalent to yanking a DIMM module while the system is running. Not good.)

TL;DR: if you want to fuck up your phone in addition to boiling the ocean, use AI instead of a brain.

1 Like

For the Xperia 10 II, the benefit is real. Initially, the 10 II would experience oom errors whenever more than two apps were used simultaneously. With zRAM, I can now run many apps smoothly and without OOM.
I don’t loose anytime !

3 Likes

maybe try it simple, not sure when the targets are reached (some systemctl command should show this), but e.g. graphical.target could be very late in boot process, so maybe use this as target to activate the swapfile without need of changing fstab (just swapon)

1 Like

@mive @mkz

I wouldn’t even know where to start with something like this. In any case, it’s always possible to create a ½ GB swapfile on / — it’s still better than nothing — or use a 1 GB swapfile on / and simply delete it before performing a SailfishOS update.

This whole ZRAM project was originally meant for the 10 III (the phone I use), because I realized that the Android HAL handled the module very poorly. Then it occurred to me that a swapfile would help (and it does), so I created it and shared the procedure. But my technical knowledge and my experience with SFOS end there.

I don’t feel like going any further: it would mean moving blindly, trying to guess the solution, and risking causing damage.

Thanks to everyone who supported and helped me.

P.s.: I’m happy to help everyone have the oom issue with his smartphone.

2 Likes

OK, so with no guarantees of correctness or ridiculousness, this seems to work and indeed does not cause issues with reboot (tested exactly once, on Jolla C2, SFOS 5.0.0.73):

=====

File MUST be named home-swap-swap0.swap and should be placed in /etc/systemd/system. If you change the location of the swap file, you have to change the name of the unit file following the systemd documentation.

[Unit]
Description=Enable swap file on home
# To create this:
# - mkdir /home/swap
# - chmod 0700 /home/swap
# - dd if=/dev/zero of=/home/swap/swap0 bs=1M count=128
# - chmod 0600 /home/swap/swap0
# - mkswap -L home-swap0 /home/swap/swap0
ConditionPathExists=/home/swap/swap0
DefaultDependencies=false

Conflicts=shutdown.target

Conflicts=umount.target
Before=umount.target

After=local-fs.target init-done.service
Requires=local-fs.target

After=home.mount
Requires=home.mount

PartOf=swap.target

[Swap]
What=/home/swap/swap0
Options=nofail
Priority=-2

[Install]
WantedBy=multi-user.target
7 Likes

I wanted to compliment you on the .swap unit you shared.

I’m still learning how to use systemd in a more advanced way, and I wouldn’t have been able to write a file like that myself — at least not for now! :wink:

I was actually starting to think about an autostart script that would enable a manually created swapfile at every SailfishOS boot, but I’m not sure something like that would even be possible.

Thank you so much for your contribution — this might be the definitive solution for anyone using a very old phone with limited space on / and need SWAP.

1 Like

Just for clarity: your script enables the swapfile at /home/swap/swap0, but it doesn’t create it. So it needs to be created manually in that location using these commands:

devel-su

mkdir -p /home/swap

chmod 0700 /home/swap

fallocate -l 1024M /home/swap/swap0

chmod 0600 /home/swap/swap0

mkswap -L home-swap0 /home/swap/swap0

and then your script enables it at every boot, right?

And naturally we must starting service and enable it, right?

devel-su

systemctl daemon-reload

systemctl enable home-swap-swap0.swap

systemctl start home-swap-swap0.swap

1 Like

I alsok checked systemd man,maybe its even simpler then the example by nephros (not sure about encrypted home though

simple systemd file


# /etc/systemd/system/home-defaultuser-.swap.swap:

[Swap]
What=/home/defaultuser/.swap
Priority=-2
TimeoutSec=10
#before swap file
[defaultuser@Fairphone4 /etc/systemd/system> free -m
              total        used        free      shared  buff/cache   available
Mem:           5503        2378         149         347        2976        3466
Swap:          3072         467        2605

# after sytemctl start
[defaultuser@Fairphone4 /etc/systemd/system> free -m
              total        used        free      shared  buff/cache   available
Mem:           5503        2373         155         347        2976        3471
Swap:          3584         467        3117
[defaultuser@Fairphone4 /etc/systemd/system> swapon
NAME                    TYPE      SIZE   USED  PRIO
/dev/zram0              partition   3G 466,9M 32758
/home/defaultuser/.swap file      512M     0B    -2

not sure if it works after reboot (and enable of course) or fails because home is encrypted (I wouldnt recommend to use a swapfile on a phone or only if really necessary)

1 Like

Correct!!

Enable is required, yes, start is optional.

1 Like

Thx very much! I Think it’s very use full!

Can I put your code in my GitHub Page? Obviosly with reference to your name!

Sure, no problem! :slight_smile:

3 Likes

Tested on my Jolla C2, it works perfectly.
Now I can post your code like update of my project on GitHub, thanks very much!

Thanks, I applied your script and everything seems to be working fine, even after a reboot.

A small niggle re @nephros swap solution: convention demands that manually added units are to be added in /etc/systemd, not /usr/lib/systemd. This doesn’t affect functionality at all though. If this was an installable package, /usr/lib/systemd would be the correct location.

1 Like

Right you are! :slight_smile:
I tend to “think in packages” rather than “as a sysadmin” with these things, at least on SFOS.

2 Likes

Good work on the systemd unit btw!

1 Like

Only?

Then I would need to change that for harbour-defender as well.

But, tbh, I always put my self-made stuff to etc/systems…

And if you look into both locations you find both variants in both locations :slight_smile:

I always wondered about this possible ambiguousness? Now I know, thanks :+1:

if you check manpage, there is a description where you should put your stuff :slightly_smiling_face:, e.g. man systemd.unit:

       Table 1.  Load path when running in system mode (--system).
       +-------------------------------+-------------------------------------------+
       | Path                          | Description                               |
       +-------------------------------+-------------------------------------------+
...
       +-------------------------------+-------------------------------------------+
       | /etc/systemd/system           | System units created by the administrator |
       +-------------------------------+-------------------------------------------+
       | /run/systemd/system           | Runtime units                             |
       +-------------------------------+-------------------------------------------+
...
       +-------------------------------+-------------------------------------------+
       | /usr/local/lib/systemd/system | System units installed by the             |
       |                               | administrator                             |
       +-------------------------------+-------------------------------------------+
       | /usr/lib/systemd/system       | System units installed by the             |
       |                               | distribution package manager              |
       +-------------------------------+-------------------------------------------+
...

2 Likes

So, I must correct the path from /usr/lib/systemd/system/home-swap-swap0.swap to /etc/systemd/system/home-swap-swap0.swap?

as general info, if you create a new systemd file, use systemctl edit --force --full then it’s placed in the correct location (use with --user for user files in ~/.local/share/systemd)

2 Likes

Ok, thx, but in this case the right path for swap device is etc?