6

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?

Mateusz Piotrowski
  • 4,623
  • 5
  • 36
  • 70
Thomas Keller
  • 62
  • 1
  • 3
  • 16
  • 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](http://superuser.com/questions/319479/how-do-i-deallocate-a-linux-ramdisk-e-g-dev-ram0) not serving you? – joepd Jan 09 '16 at 14:56
  • I wonder whether the method describes is the correct way to allocate a ramdisk: First the file created will be a normal file, not a block device, and second if `/dev` is not a ramdisk itself, you will not create a ramdisk hat way. Anyway: If you created it that way a simple `rm` should deallocate the space being used (assuming all references closed). – U. Windl Oct 27 '20 at 07:49

2 Answers2

3

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

blockdev --flushbufs /dev/ram0

Source

Martin Vegter
  • 69
  • 66
  • 195
  • 326
  • This is spot-on for `brd` ramdisks. It beats my `rmmod` + `modprobe` solution easily – roaima Jan 09 '16 at 15:55
  • I doubt thiis is the answer: Doesn't it free the cache *related* to `/dev/ram0`? The manual page is rather vague on that. Probably the ramdisk driver does something special *instead* of flushing the buffers. – U. Windl Oct 27 '20 at 07:58
  • Are you sure it should work? I am not able to reproduce that here `modprobe brd rd_nr=3 && ls /dev/ram*` lists `ram0` and others. Then, `blockdev --flushbufs /dev/ram0 && ls /dev/ram*` — but the device is still sitting there. – Hi-Angel Jun 04 '21 at 11:47
2

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"
    then
        rmmod brd
        modprobe brd rd_size=$((1024*$mb))
    fi
}

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.

roaima
  • 107,089
  • 14
  • 139
  • 261
  • 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](http://unix.stackexchange.com/users/43007/martin-vegter) who has offered another answer here – roaima Jan 09 '16 at 15:57
  • Another good question would be why a ramdisk *isn't* `removable` (as per `/sys/block/ram0/removable` being `0`). – U. Windl Oct 27 '20 at 08:14
  • @U.Windl it is a good question. Care to ask it (and get a +1 from me)? – roaima Oct 27 '20 at 10:29
  • See https://unix.stackexchange.com/q/616646/320598 – U. Windl Oct 27 '20 at 14:38