I have allocated /dev/ram0:

dd if=/dev/zero of=/dev/ram0 bs=1M count=1024

Now I have 1Gb sitting in memory. How do I free up the allocated space ?

  • I don't have a ramdisk to test, but how about truncate /dev/ram0? – muru Dec 31 '15 at 22:48
  • How is this Q+A not serving you? – joepd Jan 9 '16 at 14:56

I believe, you can use blockdev command, which is available from util-linux package (in Debian)

blockdev --flushbufs /dev/ram0


  • This is spot-on for brd ramdisks. It beats my rmmod + modprobe solution easily – roaima Jan 9 '16 at 15:55

Using brd for the ramdisk

Using brd for the ramdisk, the only way to free up the RAM is by removing the module from the kernel, or by a reboot. freeramdisk does not work (for me).

Here is an example of usage

apt-get install loadln                # Contains "freeramdisk" command
modprobe brd rd_size=$((1024*128))    # 128MB
free -m                               # total=1000 used=356 free=643
dd if=/dev/zero bs=1M of=/dev/ram0    # writes 128 x 1MB records (as expected)
free -m                               # total=1000 used=485 free=515
freeramdisk                           # "freeramdisk: cannot open /dev/ram: No such file or directory"
free -m                               # total=1000 used=485 free=515
rmmod brd                             # REMOVE ALL THE RAM DISKS
free -m                               # total=1000 used=358 free=641

There doesn't seem to be an easy way to determine the original size of the ramdisk other than by using dd to read the device until failure. However, given these devices are implicitly small it wouldn't take long to do this. Thus a "quick reset" command can be created:

resetramdisk() {
    mb=$(dd if=/dev/ram0 bs=1M of=/dev/null 2>&1 | sed -n 's/+.*//p;q')
    if test -n "$mb"
        rmmod brd
        modprobe brd rd_size=$((1024*$mb))

Using kernel boot-time ramdisk

I believe that these disks can be freed up with freeramdisk, but I don't have a configuration that I can test right now.

  • Adding as a comment rather than as an edit to say that blockdev --flushbufs /dev/ram0 works far better than my initially proposed rmmod + modprobe. Kudos to @Martin Vegter who has offered another answer here – roaima Jan 9 '16 at 15:57

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.