Hacker News new | ask | show | jobs
by leni536 3694 days ago
Idea: What if there was an OS interface to mark memory regions as "throwaway"? Throwaway in the sense that it's less costly to recalculate than to recall it from swap. So the OS would throw such memory regions instead of swapping out and signal the process about it to recalculate it if needed.

I don't usually bump into heavy swap usage except when I wake my laptop from hibernate. I tend to close browsers before hibernating because it's faster to just resume the last session from a cold start then load from swap.

4 comments

There has been some thoughts about implementing this for linux, but I don't think it got very far. The idea is that you mark a range of memory as "volatile", and the OS will free that when it's running low on memory. In most cases where you're running out of memory though, swapping out pages or killing a program using the memory is probably the right thing to do though.

https://lwn.net/Articles/522135/

There was also this in 2009: http://lwn.net/Articles/340080/
Both OS X and iOS have the NSPurgeableData class which implements something like this: https://developer.apple.com/library/mac/documentation/Perfor...

Edit: Chromium has this: https://chromium.googlesource.com/chromium/src/base/+/master...

"Discardable memory" doesn't seem to be implemented though, however there is an implementation for "discardable shared memory" [1] which seems to offer this volatile behavior. It uses ashmem_pin_region() and ashmem_unpin_region() for Android, which seems to be the equivalent for iOS's NSPurgeableData. I wonder if it does anything useful for any other OS.

[1] https://chromium.googlesource.com/chromium/src/base/+/master...

Your OS already has a 'file buffer cache' that keeps pages of read files in RAM and discards them only if the RAM is needed for something else.

I made a neat little application-accessible key-value cache that used these pages. It means you can write keys that end up in your OS's file buffer cache, and if the OS doesn't need that RAM for anything else, you can read them back later.

http://williamedwardscoder.tumblr.com/post/13363076806/buffc...

Sadly not a mainstream thing :( ;)

> Throwaway in the sense that it's less costly to recalculate than to recall it from swap.

Network is slower than disk (is slower than memory). So if "recalculate" means getting it from the internet, then probably getting it from disk is faster in most cases.

local network is faster than disk.

The internet is rarely faster than disk unless you're in a major data centre.

And then... this is assuming you're referring to spinning rust. local disks in the form of SSDs will be faster again.

I expect that it's easy to rerender webpages but it's wasteful to swap the bitmaps and load them from disk again. I wouldn't expect the browser to throw away data retrieved via network.