How to create a fixed size Linux ramdisk which does never swap to disk?

This is just a thought and has more than one downside, but it might be usable enough anyway.

How about creating an image file and a filesystem inside it on top of ramfs, then mount the image as a loop device? That way you could limit the size of ramdisk by simply limiting the image file size. For example:

$ mkdir -p /ram/{ram,loop}
$ mount -t ramfs none /ram/ram
$ dd if=/dev/zero of=/ram/ram/image bs=2M count=1
1+0 records in
1+0 records out
2097152 bytes (2.1 MB) copied, 0.00372456 s, 563 MB/s
$ mke2fs /ram/ram/image
mke2fs 1.42 (29-Nov-2011)
/ram/ram/image is not a block special device.
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
256 inodes, 2048 blocks
102 blocks (4.98%) reserved for the super user
First data block=1
Maximum filesystem blocks=2097152
1 block group
8192 blocks per group, 8192 fragments per group
256 inodes per group

Allocating group tables: done                            
Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done
$ mount -o loop /ram/ram/image /ram/loop
$ dd if=/dev/zero of=/ram/loop/test bs=1M count=5
dd: writing `/ram/loop/test': No space left on device
2+0 records in
1+0 records out
2027520 bytes (2.0 MB) copied, 0.00853692 s, 238 MB/s
$ ls -l /ram/loop
total 2001
drwx------ 2 root root   12288 Jan 27 17:12 lost+found
-rw-r--r-- 1 root root 2027520 Jan 27 17:13 test

In the (somewhat too long) example above the image file is created to be 2 megabytes and when trying to write more than 2 megabytes on it, write simply fails because the filesystem is full.

One obvious downsize for all this is of course that there is much added complexity, but at least for academic purposes this should suffice.