Search code examples
memoryclojureheap-memory

tracking memory allocation in Clojure


In my program all the state is held in a giant map in an atom, which is updated by a load of pure functions in each iteration. I have determined that the heap size is increasing, how do I find the code that's responsible ? I tried VisualVM, but it gives generic information and I can't find which part of my state is growing and which function is causing it to grow.


Solution

    1. Look for common gotchas like forgetting to use with-open, hanging onto the head of a sequence, etc.

    2. Isolate smaller segments of your code and see if you still see the same kinds of memory growth using JVisualVM. If knocking out or mocking some piece makes no difference then put it back, and if it makes a difference then you can focus on that and figure out what is going on.

    I don't know of any silver bullet tool or technique, it's just a process of divide and conquer, and thinking about what you are doing in your code.