1

I have Ubuntu 21.04 and I started to have a particular issue some time ago. I have python programs I run regularly, sometimes re-running the same thing without any differences. But since some time, my computer sometimes freezes.

I just ran some memory intensive python script, while having my System Monitor screen open. What I noticed, is that the used RAM and Swap memory fractions would steadily increase. After the used RAM reached about 95% (Swap was 100%), I killed the process. But both memories are still full!

I don't get it, isn't the system suppose to free memory after a process is done? Is this a bug? How can I free it?

$ free -h
              total        used        free      shared  buff/cache     available
Mem:          7,2Gi       6,5Gi       281Mi        20Mi       362Mi       378Mi
Swap:         2,0Gi       2,0Gi       1,0Mi
$ ps -ao comm,%mem | sort -rnk2,2 | head
python          72.4
gedit            0.6
Xorg             0.4
sort             0.0
python           0.0
python           0.0
ps               0.0
head             0.0
gnome-session-b  0.0
COMMAND         %MEM
7
  • Please edit your question and add the output of free -h. I suspect the RAM has been marked as available, it just appears as used. Look at the number given under "available" in the output of free, that's the RAM that is actually available to the system.
    – terdon
    May 5 at 16:46
  • ok done! Looks like the memory is indeed not freed... May 5 at 16:51
  • Hmm yes. OK, what is using it then? Can you add the output of ps -ao comm,%mem | sort -rnk2,2 | head to your question? That should give us the top 10 processes by RAM usage. I'm thinking maybe the offending process hasn't been killed.
    – terdon
    May 5 at 16:56
  • Perhaps you can see if the memory has been freed according to top or htop instead of free?
    – Ray
    May 5 at 17:00
  • @terdon, I updated the question. So it seems python is still running...? May 5 at 17:03

1 Answer 1

0

A possible workaround would be to add more swap to your system:

$ free -h
              total        used        free      shared  buff/cache     available
Mem:          7,2Gi       6,5Gi       281Mi        20Mi       362Mi       378Mi
Swap:         2,0Gi       2,0Gi       1,0Mi

You don't have enough swap at 2G. Let's increase it to 4G.

First, check this...

Confirm that sysctl vm.swappiness = 60.

If either/both swapon -s or grep -i swap /etc/fstab shows "/swapfile" then do the following:

In the terminal...

sudo swapoff -a           # turn off swap
## create the new /swapfile; this will overwrite any existing file
head -c 4G /dev/zero | sudo tee /swapfile  

sudo chmod 600 /swapfile  # set proper file protections
sudo mkswap /swapfile     # init /swapfile
sudo swapon /swapfile     # turn on swap
free -h                   # confirm 8G RAM and 4G swap

Edit /etc/fstab, using sudo -H gedit /etc/fstab or sudo pico /etc/fstab.

Add this /swapfile line to /etc/fstab and confirm that no other "swap" lines are present:

/swapfile  none  swap  sw  0  0

Finally, reboot and verify the operation.

5
  • To be precise: the issue was me interrupting Python processes via CTRL+z, hence not killing it. I would then restart it after modifying my code. Or start another one. Rinse and repeat. The consequence of which, of course, was filling my ram and swap until my computer froze. I was running something quite memory intensive these past few days, and it also helped to del a couple of arrays when not needed anymore. All this to say, that this answer is not a solution per say of my problem. But it remains an interesting idea in case I do need more swap memory in the future. May 6 at 19:23
  • @BitterDecoction When your /swapfile is 100% used up, you need more /swapfile. Plain and simple.
    – heynnema
    May 6 at 20:17
  • @heynnema, but it doesn’t reach 100% anymore. You may have 8 Gb of swap, but if you never kill your processes, it will reach 100% at some point May 7 at 7:15
  • @BitterDecoction You shouldn't have to kill processes. I don't have 8G swap, and my answer didn't provide for 8G of swap.
    – heynnema
    May 7 at 11:56
  • I know about the 8G, I think you don’t get what I’m saying. So if I’m debugging my code I shouldn’t have to kill my process if I don’t want it to finish? And there’s other occasions. Sometimes I review the code and realize I forgot something. I regularly kill processes while coding (mostly physics simulations and plotting). The point is, that if I don’t kill processes, I don’t have any issues. It occurs only when I do it. Hence the 8G remark. May 7 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.