Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell lazy unloading

I need to have a large list of data that when referenced at a specific location calculates (loads from a file, and/or generates it if it hasn't been generated yet) and keeps it for future use. This is powered by lazy lists bound to a function. These "chunks" sometimes are loaded but never really used after that while still effectively referenced in code so the GC doesn't pick up on them.

Since the RAM quickly fills up, I'd like to lazily unload these chunks after a period of time where they aren't used by anything. Is this possible?

like image 202
kvanbere Avatar asked Mar 28 '26 19:03

kvanbere


1 Answers

You can implement this by using unsafeInterleaveIO to read the chunks and periodically going through the list and removing the references to chunks that were not used for a long time (alternatively: use weak pointers as @nponeccop suggests in the comments), but I'd go with something that doesn't rely on GC for managing memory for the chunks (since predictable memory usage is important for you).

For example:

import Data.HashTable.IO

type ChunkMap = BasicHashTable ChunkId (Maybe Chunk)

newChunkMap :: IO ChunkMap
getChunk :: ChunkMap -> IO Chunk
freeUnusedChunks :: ChunkMap -> IO ()

where getChunk allocates the memory for missing chunks with malloc and freeUnusedChunks goes through the table and frees unused chunks.

You can even run freeUnusedChunks in a separate thread:

freeThread = forever $ do
                 withChunkMapLock $ do
                     freeUnusedChunks map
                     threadDelay 5000000
like image 147
Mikhail Glushenkov Avatar answered Mar 31 '26 11:03

Mikhail Glushenkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!