I'm learning Clojure and I'm wondering why my application is consuming so much memory and not releasing it once it's done using those data structures. I imagine that there is something in Clojure that I don't know about that is making the application hold onto some stuff by mistake.
It's a simple thumbnailing app. It takes images from a directory, resizes
them and puts them into another directory. It uses the javax.imageio
and
java.awt.image
libraries.
(def thumb "path to thumbnails directory")
(defn get-thumb-filename [thumb-dir filename width]
(str thumb-dir (fs/name filename) "_" width ".jpg"))
(defn get-thumb-filename-100 [filename]
(get-thumb-filename thumb filename 100))
(defn small-thumbnail [filename]
(make-thumbnail filename (get-thumb-filename-100 filename) 100))
(defn make-thumbnail [filename new-filename width]
(let [img (javax.imageio.ImageIO/read (as-file filename))
imgtype (java.awt.image.BufferedImage/TYPE_INT_ARGB)
width (min (.getWidth img) width)
height (* (/ width (.getWidth img)) (.getHeight img))
simg (java.awt.image.BufferedImage. width height imgtype)
g (.createGraphics simg)]
(.drawImage g img 0 0 width height nil)
(.dispose g)
(javax.imageio.ImageIO/write simg "png" (as-file new-filename))))
;; `images` is a list of original filenames
;; Kick it off with...
(pmap small-thumbnail images)
This code is part of a Noir view, meaning that the application doesn't exit after the processing of all images is finished.
When I run this code, my computer starts going crazy. The fan gets loud and the main java process uses over 1GB of memory. When all images are processed, the CPU usage goes down to < 5% but the memory usage stays the same.
How can I make it release all of this memory that it clearly doesn't need for anything?
Thanks!
PS: Please let me know if you need me to fill in any details.