Why does ubuntu not seem to release memory?

22,210

You can free up unused memory using this command:

sudo sync && sudo sysctl -w vm.drop_caches=3

This command synchronize cached data and then writes an OS parameter at runtime, located at /proc/sys/vm/drop_caches. This parameter is read by kernel once, then free up the unused memory.

Basically, the kernel reserves three sections in cache, that could increase memory usage:

  • pagecache
  • dentries
  • inodes

Writing to this parameter will cause the kernel to drop clean pagecaches, dentries and inodes from RAM, causing that memory to become free. There are three values that can be assigned:

  1. vm.drop_caches=1 free pagecaches.
  2. vm.drop_caches=2 free dentries and inodes.
  3. vm.drop_caches=3 free pagecaches, dentries and inodes.

Important: After execution this will return to normal state which means start caching pagecaches, dentries and inodes again. Use it depending on your server configuration and application.

Share:
22,210

Related videos on Youtube

Tejaskumar Tank
Author by

Tejaskumar Tank

Updated on September 18, 2022

Comments

  • Tejaskumar Tank
    Tejaskumar Tank over 1 year

    I am working with apache2+php and other web server apps.

    I have monitor as session connecting to a web server. RAM memory is full.

    But when disconnecting a session, it does not free the RAM memory.

    I have monitor via free -m, and it show unreleased memory even with no session now connected.

    Is there a solution?

    • Admin
      Admin over 9 years
      Be careful with free -m . The first line shows the memory usage including the buffers and caches. The second line shows the used memory less the buffers and cache, which is the actual available memory. You should also try something like htop for monitoring purposes. It is much more intuitive and available in the official repos. If you are really running out of memory, give us more info like the type of web application you are running, the quantity of RAM you have, what is used and all. Paste the output of free -m and stuff like that.
    • Admin
      Admin over 9 years
      There is no solution, because there is no problem. Linux uses available RAM as a buffer for files.
    • Admin
      Admin over 9 years
      But here after fire command: sudo sync && sudo sysctl -w vm.drop_caches=3 It shows me right free available memory and also apps works better.
    • Admin
      Admin almost 9 years
    • Admin
      Admin over 6 years
      I am most expeirenced with VPS server and founded recently debian 9.0 working perfect with memory management.
  • A.B.
    A.B. almost 9 years
    Can you please explain a little more detail?
  • Tejaskumar Tank
    Tejaskumar Tank almost 9 years
    This command only useful when weekly or daily at end of day want to release all unused memory, also 3 or 2 or 1 each with different purpose. Do not try this on session loaded server.
  • A.B.
    A.B. almost 9 years
    Edit your answer, not as comment.
  • Nyxynyx
    Nyxynyx about 4 years
    sudo sync & sudo sysctl -w vm.drop_caches=3
  • Guillermo Garcia
    Guillermo Garcia about 4 years
    @Nyxynyx what shell are you using? my example was tested in bash
  • Nyxynyx
    Nyxynyx about 4 years
    @GuillermoGarcia Using bash in Ubuntu 18.04.3. I needed to include sudo a second time. Otherwise I get the error sysctl: permission denied on key 'vm.drop_caches'
  • Dr Potato
    Dr Potato about 2 years
    Does not work for me. System monitor shows the same memory usage after executing this command.