magnars / optimus

A Ring middleware for frontend performance optimization.
364 stars 23 forks source link

V8 usage causes memory leaks #48

Closed arohner closed 8 years ago

arohner commented 8 years ago

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.

magnars commented 8 years ago

Ouch!

magnars commented 8 years ago

Fixed in #49. Thanks again.