Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does using require with the :reload option have a tendency to build up memory in Clojure?

Tags:

clojure

I have an application that, in order to reload plugins, requires them with the :reload option whenever they are to be reloaded. I've noticed that this is building up memory about 2-3 megs at a time when I do it. I'm curious as to what could cause this sort of thing to happen. Is data from previous reloads being kept in memory? Is there a way to totally reload a namespace?

EDIT: It's also relevant to mention that each of these plugins that gets reloaded makes new defmethods for a multimethod in another namespace (that never gets reloaded). Maybe the methods are being kept in memory when it's reloaded?

like image 578
Rayne Avatar asked Nov 06 '22 09:11

Rayne


1 Answers

Clojure defers memory management to the JVM. While I don't know clojure's codebase deeply, it probably just reassigns the vars with the reloaded code - which will leave the old objects around until the JVM runs the garbage collector.

You can hint to the JVM that you want the GC to run using (System/gc), but it's generally not recommended to use.

Alternatively, if you know the contraints of your system, you can tinker with the JVM memory flags to encourage the GC to run more frequently (ie - use a lower heap size).

But if you have a system that's not really memory constrained, saving a few mbs doesn't matter much.

like image 91
Jeff Avatar answered Nov 14 '22 22:11

Jeff