Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check memory usage in haskell

I'm creating a program which implements some kind of cache. I need to use as much memory as possible and to do that I need to do two things:

  1. Check how much memory is still available in system (RAM only, not SWAP)
  2. Check how much memory my app is already using.

I need a platform independent solution (Linux, Windows, etc.).

Using these two pieces of information I will reduce the size of cache or enlarge it. How can I get this information in Haskell? Are there any packages that can provide that information?

like image 304
remdezx Avatar asked Sep 25 '14 09:09

remdezx


People also ask

Does Haskell use a lot of memory?

Haskell computations produce a lot of memory garbage - much more than conventional imperative languages. It's because data are immutable so the only way to store every next operation's result is to create new values. In particular, every iteration of a recursive computation creates a new value.

Does Haskell have a heap?

By default, Haskell heap objects are stored “boxed” — they are represented as a pointer to an object on the heap.


1 Answers

I can't immediately see how to do this portably.

However, GHC does have "weak pointers". (See System.Mem.Weak.) If you create items and hang on to them via weak pointers (only), then the garbage collector will automatically start deleting items if you run low on physical memory.

(Unfortunately, this doesn't give you the ability to decide which items to delete first — e.g., the ones that are cheapest to recreate or the ones that have been least-used or something.)

like image 78
MathematicalOrchid Avatar answered Sep 20 '22 04:09

MathematicalOrchid