We’ve briefly discussed profiler types in Chapter 1, Performance by Design. The JVisualVM tool we discussed with respect to introspection in the previous section is also a CPU and memory profiler that comes bundled with the JDK. Let us see them in action. Consider the following two Clojure functions that stress on the CPU and memory respectively:
(defn cpu-work [] (reduce + (range 100000000))) (defn mem-work [] (->> (range 1000000) (map str) vec (map keyword) count))
Using JVisualVM is pretty easy; open the Clojure JVM process from the left-pane. It has the sampler and regular profiler styles for profiling. Start profiling for CPU or memory use when the code is running and wait for it to collect enough data to plot on the screen. The following screenshot shows CPU profiling in action:
The following screenshot shows memory profiling in action:
JVisualVM is a very simple, entry-level profiler. There are several commercial JVM profilers on the market...