Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux swap space never release memory

I am using Linux kernel 2.6.38, and I am running a process that allocates 4GB of memory, and I have a 4GB of ram available, so when I run my application it allocates around 0.5GB from swap space. however, my application runs for a very long time and accesses data on the swap space several times.

(Edited) To clarify what I am doing:

  • I am running Linux 2.6.38, with 4 GB of RAM.

  • without running any applications, the system is occupying around 500MB of RAM.

  • I created a simple application that allocates 4GB of memory and seeks across allocated memory and changes the values of that memory many times (loop of 10 iterations).

  • it is obvious that I will need the swap space in order for application to run.

  • when I run my application, the swap space keeps accumulating and becomes full after few iterations, and the process is killed.

  • after the process is killed the swap space remains full as well.

  • I tested my application on a more recent kernels and it works fine, the swap space does not accumulate.

is this a bug on this kernel version (2.6.38)? is there a fix to it?

like image 629
Zaid Alali Avatar asked Dec 23 '22 18:12

Zaid Alali


2 Answers

There's no memory leak.

You're assuming that when your application needs more memory than what's available, parts of it is written to swap. This is not necessarily true.

The system may (and generally will) write other, completely unrelated processes to swap, because they're not currently in use.

Since this swap space does not belong to your application, it will remain in use after your application exits.

This swap space may further stay in use for a long time since Linux doesn't preemptively load them back when there's free RAM.

like image 71
that other guy Avatar answered Dec 31 '22 04:12

that other guy


I'm not sure my response will answer your question but I asked myself a similar question a while back.

To summarise when Linux allocates memory (RAM/SWAPP) it only frees it when it's needed. That means even after the process has terminated the allocated memory will remain until another process needs the space.

However if you want to free the SWAPP you can do it manually

sudo swapoff -a 

Do not forget to turn it back on

sudo swapon -a 

You can find more information at that link and that one

like image 34
Remy J Avatar answered Dec 31 '22 03:12

Remy J