magnars/optimus

V8 usage causes memory leaks

Closed this issue · 2 comments

My app is using https://github.com/stuartsierra/component. As is expected w/ component, I create a new system for each test. Recently, tests started failing on CircleCI because my app was using more than 4GB of RAM. As part of my system creation, I create a full optimus middleware, with all optimizations enabled.

I was clearly leaking memory, and it wasn't part of the java heap. I ran the leak down, and it is caused by optimus leaking v8 context handles in optimus.optimizations.minify

As proof, run ps -o rss <pid of your clojure repl>

then run:

(dotimes [i 1000] (v8.core/cleanup-context (v8.core/create-context)))

observe essentially no increase in memory usage. Then run:

(dotimes [i 1000] (v8.core/create-context))

And note massive increase in memory usage. (500MB -> 1500MB for me). Note that every usage of v8/create-context in minify.clj fails to call v8.core/cleanup-context. If you repeatedly load assets, or minify a large number of files, this will cause enormous memory usage.

Ouch!

Fixed in #49. Thanks again.