Why are swap files/partitions constant size? Most of the time, I don't need any swap at all, and any swap file/partition is wasted space. While hibernated, I need ~the amount of system ram as swap. While running some horrendous matlab script, I need about 1TB of swap. Yet linux won't dynamically allocate it like other files. I'm forced to constantly resize it manually to save space or be able to do more things.
1. Something asks for memory from the kernel.
2. Kernel doesn't have enough free memory, goes to offload something old to swap.
3. Swap file is too small, needs to be extended. This calls the "extend file size" filesystem code.
4. Filesystem code tries to allocate memory for keeping track of filesystem data.
5. Goto 1.
As far as I understand, the "swap file on filesystem" system bypasses the filesystem. The kernel asks the filesystem to give it a bunch of blocks to work with and then ignores the filesystem code entirely. This is why some filesystems can't have swap files -- the FS has to support the right APIs, and has to be prepared for this kind of deal.