7

I have a Clojure program that is consuming a large amount of heap while running (I once measured it at somewhere around 2.8GiB), and I'm trying to find a way to reduce its memory footprint. My current plan is to force garbage collection every so often, but I'm wondering if this is a good idea. I've read How to force garbage collection in Java? and Can I Force Garbage Collection in Java? and understand how to do it — just call (System/gc) — but I don't know if it's a good idea, or even if it's needed.

Here's how the program works. I have a large number of documents in a legacy format that I'm trying to convert to HTML. The legacy format consists of several XML files: a metadata file that describes the document, and contains links to any number of content files (usually one, but it can be several — for example, some documents have "main" content and footnotes in separate files). The conversion takes anywhere from a few milliseconds for the smallest documents, to about 58 seconds for the largest document. Basically, I'm writing a glorified XSLT processor, though in a much nicer language than XSLT.

My current (rather naïve) approach, written when I was just starting out in Clojure, builds a list of all the metadata files, then does the following:

(let [parsed-trees (map parse metadata-files)]
  (dorun (map work-func parsed-trees)))

work-func converts the files to HTML and writes the result to disk, returning nil. (I was trying to throw away the parsed-XML trees for each document, which is quite large, after each pass through a single document). I now realize that although map is lazy and dorun throws away the head of the sequence it's iterating over, the fact that I was holding onto the head of the seq in parsed-trees is why I was failing.

My new plan is to move the parsing into work-func, so that it will look like:

(defn work-func [metadata-filename]
  (-> metadata-filename
      e/parse
      xml-to-html
      write-html-file)
  (System/gc))

Then I can call work-func with map, or possibly pmap since I have two dual-core CPUs, and hopefully throw away the large XML trees after each document is processed.

My question, though, is: is it a good idea to be telling Java "please clean up after me" so often? Or should I just skip the (System/gc) call in work-func, and let the Java garbage collector run when it feels the need to? My gut says to keep the call in, because I know (as Java can't) that at that point in work-func, there is going to be a large amount of data on the heap that can be gotten rid of, but I would welcome input from more experienced Java and/or Clojure coders.

16
  • Short answer: If you're needing to call gc that often, you may need to reorganize your data structure. I recommend retitling your question to be more broad, since you may be approaching the question with blinders on. Feb 26, 2014 at 10:31
  • For clarity: e/parse in my work function is the XML parser from Enlive, and I'm using Enlive transformations to convert the XML to HTML.
    – rmunn
    Feb 26, 2014 at 10:36
  • 2
    parsed-trees does not hold onto the head of the seq. This word of caution about holding head of a seq is a bit dated – it's still true but this problem is not as frequent as it used to be. The compiler releases unneeded references right after their last usage. So since you don't use parsed-trees after the call to map there's no head retention issue. I think the leak is somewhere else. Which libraries are your using, do you use some memoiztion?
    – cgrand
    Feb 26, 2014 at 11:27
  • 2
    Well, it looks like I was using doall after all, not dorun. Hence I was holding on to the head of the seq, and keeping all 800 or so trees in heap until the entire document set was finished processing. Just switching to dorun probably saved me a lot of heap, and when you combine that with the other steps I took (moving the parsing inside my work function, and using pmap instead of map), I've not only saved heap but also time. My runtimes are down from 50-60 minutes to about 15-20 minutes, in very unscientific benchmarks. Thanks for the help, everybody.
    – rmunn
    Feb 26, 2014 at 15:28
  • 1
    @rmunn, ok but you should use doseq and not dorun.
    – cgrand
    Feb 26, 2014 at 15:56

1 Answer 1

10

Calling System/gc is not a helpful strategy. Assuming for now that you can't reduce the actual memory footprint of your code, what you should ensure is avoiding major GC. This will either happen automatically for you (by resizing the Young Generation until all your temp data fits), or you can tune it with explict JVM options to make the YG exceptionally large.

As long as you keep your short-lived objects from spilling into the Old generation for lack of space, you'll experience very short GC pauses. You will also not have to worry about explicitly invoking GC: it happens as soon as the Eden Space fills up.

5
  • This helped a lot: I refactored my work function to make sure it didn't hold on to any temporary objects after it was done with each document. And without doing any (System/gc) calls, my runtime dropped by a factor of 3, more or less: from 50-60 minutes on average to 15-20 minutes on average. I'm accepting this answer since it helped me, though what really helped me the most was the comments on my question.
    – rmunn
    Feb 26, 2014 at 15:30
  • If you're interested, I'll release this answer as a Community Wiki and you can edit in the gist of what helped you the most from the comments. Feb 26, 2014 at 21:17
  • I've read through the comments and the one I agree with the most is the very last one, from Cristophe; in fact, that's what I was going to write myself---don't model the entire thing as a mapping transformation which you only use for the side effect. That's what doseq is for. Feb 26, 2014 at 21:23
  • What's the difference between doseq and dorun, besides doseq's for-like semantics? What inefficiencies am I creating by using (dorun (map f coll)) instead of doseq?
    – rmunn
    Feb 27, 2014 at 3:09
  • 1
    It's not about efficiency, but about abusing sequence transformation for side effects. Each time you catch yourself performing a map operation on a sequence without ever considering its result, you know you're doing something wrong. Further, it is wrong to rely on map to perform the operation only once, in order, and only at the time you access a sequence member. doseq gives you all those guarantees. Feb 27, 2014 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.