How do I deallocate a Linux RAMDISK, e.g. /dev/ram0?

Solution 1:

Unless you format the ramdisk something along the lines of

mke2fs -m 0 /dev/ram0

The ramdisks themselves don't actually occupy any memory space.

If you do format and mount a ramdisk however, the only way that I remember doing is by using freeramdisk but that was a long long time ago.

Some other resources you might want to check out:

http://e2enetworks.com/2008/10/25/linux-in-memory-filesystems-tmpfs-vs-ramdisk/

http://www.vanemery.com/Linux/Ramdisk/ramdisk.html

http://www.linuxdoc.org/HOWTO/BootPrompt-HOWTO-3.html

Solution 2:

/dev/ram0 and other are implemented by brd linux kernel module: Source - http://elixir.free-electrons.com/linux/latest/source/drivers/block/brd.c, documentation of modprobe brd: https://www.novell.com/support/kb/doc.php?id=7012396.

Try to unload (rmmod) the module with rmmod brd, this is the only way to call brd_del_one function and delete ramdisk.

Freeram is part of loadlin package https://launchpad.net/ubuntu/+source/loadlin/1.6f-5 and implemented as opening device to read/write and doing BLKFLSBUF ioctl

  char rname[256]="/dev/ram";
  char b[256];
  int f;
  f=open(rname,O_RDWR))
  ioctl(f,BLKFLSBUF);

which is defined in https://elixir.free-electrons.com/linux/latest/source/include/uapi/linux/fs.h as

   #define BLKFLSBUF  _IO(0x12,97)  /* flush buffer cache */