Clearing RAM for Debian/Ubuntu based OSes

Issues and / or general discussion relating to Puppy

Moderator: Forum moderators

Post Reply
User avatar
Jasper
Posts: 1842
Joined: Wed Sep 07, 2022 1:20 pm
Has thanked: 785 times
Been thanked: 432 times

Clearing RAM for Debian/Ubuntu based OSes

Post by Jasper »

Hi there

I found this script which I find useful to clearing up RAM once I have closed down a number of applications. Only tested in FossaPup64.

Credit to Philipp Klaus for the script.

Code: Select all


#!/bin/bash
## Bash Script to clear cached memory on (Ubuntu/Debian) Linux
## By Philipp Klaus
## see <http://blog.philippklaus.de/2011/02/clear-cached-memory-on-ubuntu/>

if [ "$(whoami)" != "root" ]
then
  echo "You have to run this script as Superuser!"
  exit 1
fi

# Get Memory Information
freemem_before=$(cat /proc/meminfo | grep MemFree | tr -s ' ' | cut -d ' ' -f2) && freemem_before=$(echo "$freemem_before/1024.0" | bc)
cachedmem_before=$(cat /proc/meminfo | grep "^Cached" | tr -s ' ' | cut -d ' ' -f2) && cachedmem_before=$(echo "$cachedmem_before/1024.0" | bc)

# Output Information
echo -e "This script will clear cached memory and free up your ram.\n\nAt the moment you have $cachedmem_before MiB cached and $freemem_before MiB free memory."

# Test sync
if [ "$?" != "0" ]
then
  echo "Something went wrong, It's impossible to sync the filesystem."
  exit 1
fi

# Clear Filesystem Buffer using "sync" and Clear Caches
sync && echo 3 > /proc/sys/vm/drop_caches

freemem_after=$(cat /proc/meminfo | grep MemFree | tr -s ' ' | cut -d ' ' -f2) && freemem_after=$(echo "$freemem_after/1024.0" | bc)

# Output Summary
echo -e "This freed $(echo "$freemem_after - $freemem_before" | bc) MiB, so now you have $freemem_after MiB of free RAM."

exit 0

Just copy the text and place it in Geany/Leafpad and give it a name eg ClearRAM.sh

Then under Permissions make it executable. Right click and run in Terminal.

Last edited by Jasper on Fri Nov 04, 2022 11:23 am, edited 1 time in total.
User avatar
gychang
Posts: 573
Joined: Fri Aug 28, 2020 4:51 pm
Location: San Diego, CA
Has thanked: 201 times
Been thanked: 58 times

Re: Clearing RAM for Debian/Ubuntu based OSes

Post by gychang »

Jasper wrote: Sat Oct 29, 2022 6:09 am

Hi there

I found this script which I find useful to clearing up RAM once I have closed down a number of applications. Only tested in FossaPup64.

Credit to Philipp Klaus for the script.

Code: Select all


#!/bin/bash
## Bash Script to clear cached memory on (Ubuntu/Debian) Linux
## By Philipp Klaus
## see <http://blog.philippklaus.de/2011/02/clear-cached-memory-on-ubuntu/>

if [ "$(whoami)" != "root" ]
then
  echo "You have to run this script as Superuser!"
  exit 1
fi

# Get Memory Information
freemem_before=$(cat /proc/meminfo | grep MemFree | tr -s ' ' | cut -d ' ' -f2) && freemem_before=$(echo "$freemem_before/1024.0" | bc)
cachedmem_before=$(cat /proc/meminfo | grep "^Cached" | tr -s ' ' | cut -d ' ' -f2) && cachedmem_before=$(echo "$cachedmem_before/1024.0" | bc)

# Output Information
echo -e "This script will clear cached memory and free up your ram.\n\nAt the moment you have $cachedmem_before MiB cached and $freemem_before MiB free memory."

# Test sync
if [ "$?" != "0" ]
then
  echo "Something went wrong, It's impossible to sync the filesystem."
  exit 1
fi

# Clear Filesystem Buffer using "sync" and Clear Caches
sync && echo 3 > /proc/sys/vm/drop_caches

freemem_after=$(cat /proc/meminfo | grep MemFree | tr -s ' ' | cut -d ' ' -f2) && freemem_after=$(echo "$freemem_after/1024.0" | bc)

# Output Summary
echo -e "This freed $(echo "$freemem_after - $freemem_before" | bc) MiB, so now you have $freemem_after MiB of free RAM."

exit 0

Just copy the text and place it in Geany/Leafpad and give it a name eg ClearRAM.sh

Then under Permissions make it executable. Right click and run in Terminal.

Works well in my FP64, useful.

======

Puppy Bytes, utube videos
https://www.youtube.com/channel/UCg-DUU ... u62_iqR-MA

======

Post Reply

Return to “Users”