The moose likes Clojure and the fly likes Clojure Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Clojure
Bookmark "Clojure Garbage Collection" Watch "Clojure Garbage Collection" New topic

Clojure Garbage Collection

Sunderam Goplalan
Ranch Hand

Joined: Oct 10, 2011
Posts: 86
I presume Clojure has something similar to GC in Java. If so, how do the GC algorithms used by Java compare with those of Clojure? Immutability is a good thing in the recent languages
such as Clojure and Scala. Often I become concerned with how those languages manage Memory and GC.

How does the user response time compare between Java and Clojure?

SCJP 5.0 , SCEA Java EE 5, TOGAF Certified
Carles Gasques
Ranch Hand

Joined: Apr 19, 2013
Posts: 199

I understand that Clojure doesn't have a different GC algorithm since it runs in JVM, like Scala, Groovy and others.
Is the JVM that you use that could have different GC algorithms (SAPJVM, JRockit, Apache Harmony, etc).

But seems that some constructs in Clojure, for example persistent collections, tend to be long living objects;
this objects survive the partial garbage collections and only be collected when full GC runs.

If so much work is left to the full GC then could lead to JVM freezing episodes.
The usual workaround to this scenario is enable concurrent GC with a drawback on JVM performance.

Best regards,
Hussein Baghdadi
clojure forum advocate

Joined: Nov 08, 2003
Posts: 3479

Clojure as a JVM language leverages the GC that it is already used by the Java programming language.
You can even use any profiler you have. Just launch the VisualVM tool that is included in the SDK and see how your Clojure program is running.

When it comes to good memory management, Clojure doesn't blindly copy thing. It does it in a more smart way.
For example (conj x y) will return a new collection, that it is true; but it shares the content of the the old one. This is really fast and efficient.
norman richards
Ranch Hand

Joined: Jul 21, 2003
Posts: 367
I'd also point out that Clojure aggressively clears local variables when it can so that things can be garbage collected as soon as possible.
I agree. Here's the link:
subject: Clojure Garbage Collection
It's not a secret anymore!