Hey there folks!

I was developing a frontend in React using Next.js framework. It was just another web application for the company I work in. It involves fetching data from some API, presenting the data and taking actions based upon the user events.

Whenever there was any mischievous error, the development website when opened in the web browser will eat up all the physical RAM. I have 16 GB of RAM in my system. When I open the website, RAM is at 10 to 11 GB of level and then suddenly browser becomes unresponsive and will fill up the entire RAM reaching 16 GB. This used to hang my entire system and I have to struggle every time during debugging such error.

Therefore to combat this problem, I figured out a way. A memory limit can be applied. This will tell the browser not to use more than X amount of RAM. If it demands for more, then browser will simply be killed. But it won’t make the entire system non functional.

How to put a limit on memory for a browser?

I used Librewolf to test my web application. Librewolf is a custom version of Firefox that focuses more on security than the original Firefox.

I use Manjaro as the Linux distribution but this method can be applied on any distribution that runs on Systemd.

  1. Create a systemd resource control unit “/etc/systemd/system/dev-browser.slice”. This is creating a restriction that any process running under this slice will be allocated no more than 1500 MB. This is a random memory size to test the scenario.
[Unit]
Description=Slice for dev browser
Before=slices.target

[Slice]
MemoryAccounting=true
MemoryLimit=1500M

2. Run the browser using the following command. Replace the username with yours :).

sudo systemd-run --slice dev-browser.slice --scope /usr/bin/sudo -u sahil librewolf -P default-default

3. Now I opened the website in this newly memory controlled browser and in a few seconds, the browser was killed by the systemd resource controller unit. And this prevented it to take control of my system and putting me into trouble.

If the browser does not open and show the following error in journal log

librewolf[11762]: Error: no DISPLAY environment variable specified

Then tweak the systemd-run command like this.

sudo systemd-run -E DISPLAY=$DISPLAY --slice dev-browser.slice --scope /usr/bin/sudo -u sahil librewolf -P default-default

You can also use the same approach to put limits on other processes and programs. This helps to keep the system in our control instead of nasty applications taking control of our system.

Try this approach and see the outcome. See you for another beautiful post.

Similar Posts

Error: GraphComment couldn't be load because your settings are invalid. Please visit your admin panel and go to the GraphComment section and enter a valid website URL/ID.