Is forcing an application to release some the allocated memory possible

firefoxmemoryprocess

Ever since I came back to Firefox a year or so ago I have been facing the same issue over and over again – the browser just keeps eating up my memory. The problem arises from (maybe the incorrect assumption) that once a user closes a tab the browser would release the allocated memory for that tab. Of course Firefox as many others have some caching running in the background. Re-opening recently closed tabs also seems to have something to do with this. So this memory release doesn't happen as fast as the user might wish for it to happen.

I've read in many forums (including Mozilla's) that if another process requires the allocated memory that Firefox doesn't need right now the OS (in my case Debian Jessie) will take some from the browser and give it to that other process. Yes, this should indeed happen but it doesn't. Firefox keeps eating up resident memory and once it's almost all gone (like 3.6GB or so out of 3.8GB) the swap kicks in. All this results in an extremely painful browsing experience. Closing tabs doesn't do any good. Even when I close Firefox the allocated memory is still there for quite some time.

I would like to know if it is possible to force the OS to reallocate all the memory (including swap) that a process has been using (even if no other process requires that memory right now) but is now closed.

EDIT:

As requested here is the result of calling free (used the -h parameter for readability purposes) before and after Firefox is shutdown:

Before:

               total       used       free         shared       buffers        cached

Mem:           3.7G        3.6G       134M         6.8M         5.0M           166M

+/- buffers/cache:         3.4G       306M

Swap           3.7G        2.8G       916M

After:

               total       used       free         shared       buffers        cached

Mem:           3.7G        2.9G       856M         4.8M         12.0M          179M

+/- buffers/cache:         2.7G       1.0G

Swap           3.7G        2.7G       998M

As you can see some slight decrease is noticeable however it is nothing compared to the overall memory using (both resident and swap) even 20-30 minutes after the browser was closed. Beside Firefox I have nothing else started except the usual Debian processes and a couple of panel applets (weather, CPU+HDD+Mem monitoring, network monitoring etc.) which don't take a lot of memory and I can barely notice their values with htop. All I did was basically leave the browser opened the whole day and close/open tabs. I have to admit that there are a lot of tabs in my session however I upon start (and later on) I only load a few of those. My guess is that probably even when not loaded with content the tabs takes a lot of memory. However this doesn't explain why after closing the browser I have such high memory allocation. It is in fact so high that sometimes I cannot shutdown my system properly and have to do a cold shutdown.

Best Answer

The short answer is no: once an application has allocated memory, and used it, it "belongs" to that application, and unless that application releases it nothing else can reclaim it. (This isn't as simple as a call to free() though since that just returns memory to the individual application's pool, not to the system.)

Swap is supposed to help with this: once physical memory is full, the operating system can move seldom-used pages of memory to swap. If the real working-set is bigger than physical memory this just results in thrashing: memory is moved to swap and back all the time, and the system can't do anything useful.

On Linux as a last resort the OOM killer will step in and kill processes to reclaim their memory.

It's odd that you don't see memory recovered as soon as Firefox stops; perhaps the Firefox process is taking a while to actually quit, after its window is gone (check with ps or your task manager). When a process stops, all its allocated memory is freed and becomes available for others.

Related Question