<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>cruftex.net</title><description>Personal blog of Jens Wilke: Java caching, benchmarks, Jenkins and the occasional rant.</description><link>https://cruftex.net</link><item><title>The CDC is wrong about face masks and here is why</title><link>https://cruftex.net/the-cdc-is-wrong-about-face-masks-and-here-is-why</link><guid isPermaLink="true">https://cruftex.net/the-cdc-is-wrong-about-face-masks-and-here-is-why</guid><description>Wearing face masks is one of the most controversial problems in our current situation. Let&apos;s put together some good reasons why wearing face masks is a pretty good idea for everyone to do when mingling with other people.</description><pubDate>Sun, 22 Mar 2020 12:04:19 GMT</pubDate><content:encoded>&lt;p&gt;Wearing face masks is one of the most controversial problems in our current situation.
Let’s put together some good reasons why wearing face masks is a pretty good idea for everyone
to do when mingling with other people.&lt;/p&gt;
&lt;h3 id=&quot;the-cdc-says&quot;&gt;The CDC says&lt;/h3&gt;
&lt;p&gt;On their prevention recommendations web page at &lt;a href=&quot;https://www.cdc.gov/coronavirus/2019-ncov/prepare/prevention.html&quot;&gt;https://www.cdc.gov/coronavirus/2019-ncov/prepare/prevention.html&lt;/a&gt;
the CDC says:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;If you are NOT sick: You do not need to wear a facemask unless you are caring
for someone who is sick (and they are not able to wear a facemask). Facemasks
may be in short supply and they should be saved for caregivers.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;The CDC is wrong.&lt;/p&gt;
&lt;h3 id=&quot;only-useful-when-infected&quot;&gt;Only useful when infected&lt;/h3&gt;
&lt;p&gt;The argument “A face mask does not protect you from becoming infected” is correct.
But it only holds for exactly one person who is proven to be not infected affecting another person.
It needs no scientific knowledge to see that there are more than two people living on this planet.
Please take the big picture into account.&lt;/p&gt;
&lt;p&gt;Ask yourself: “How can I know for sure that I am not infected, at any time?”&lt;/p&gt;
&lt;h3 id=&quot;education&quot;&gt;Education&lt;/h3&gt;
&lt;p&gt;Wearing a face mask encourages others to do the same and this is what protects you.&lt;/p&gt;
&lt;h3 id=&quot;the-wasting-resources-problem&quot;&gt;The wasting resources problem&lt;/h3&gt;
&lt;p&gt;One argument says clinics are in need of face masks and people are using up
medical resources. But: Normal face masks don’t offer enough protection to be useful
in clinics, they primarily help to avoid spreading the virus.&lt;/p&gt;
&lt;p&gt;Face masks can be mass produced like toilet paper and plastic bags. So are we wasting resources?&lt;/p&gt;
&lt;p&gt;And what about being in short supply? Please understand how the free market works: First demand, then supply!&lt;/p&gt;
&lt;h3 id=&quot;prioritise&quot;&gt;Prioritise&lt;/h3&gt;
&lt;p&gt;If there are really not enough masks, start with:&lt;/p&gt;
&lt;p&gt;It makes sense for exposed people, like shop staff, policemen and cab drivers, to wear masks.&lt;/p&gt;
&lt;p&gt;It makes sense for people handling food or cleaning staff to wear masks.&lt;/p&gt;
&lt;p&gt;Not to protect themselves, but to avoid spreading the virus to many, many others.&lt;/p&gt;
&lt;h3 id=&quot;cities-in-comparison&quot;&gt;Cities in Comparison&lt;/h3&gt;
&lt;p&gt;Walking through Bangkok and Singapore in February, I saw about 40% and 25% of the people
wearing face masks in public transport, respectively. Seeing images from Hong Kong recently, I deduce that
nearly 100% of the people there wear face masks while in public. In Hong Kong there is no shortage
of face masks. In Bangkok I was able to buy a pack of 50 face masks for a few bucks.&lt;/p&gt;
&lt;p&gt;Of these three, Hong Kong was the one with the lowest infections. Unfortunately, the numbers
went up in recent days, after many imported cases of people fleeing to Hong Kong.&lt;/p&gt;
&lt;p&gt;Looking at the infection rates, it seems areas with more face mask usage have lower infections.&lt;/p&gt;
&lt;h3 id=&quot;more-space&quot;&gt;More space&lt;/h3&gt;
&lt;p&gt;Being the only one wearing a face mask gives you guaranteed extra space in public transport!&lt;/p&gt;
&lt;h3 id=&quot;non-discriminating&quot;&gt;Non Discriminating&lt;/h3&gt;
&lt;p&gt;If only the infected should wear face masks, we tag and categorise people. As a German I say:
Please do not do that!&lt;/p&gt;
&lt;p&gt;Depending on which city you are in at the moment, you either get looks for wearing a face mask
or you get the looks for not wearing a face mask.&lt;/p&gt;
&lt;h3 id=&quot;easy-to-explain-easy-to-understand-easy-to-verify&quot;&gt;Easy to explain, Easy to understand, Easy to verify&lt;/h3&gt;
&lt;p&gt;We need simple and effective rules that can be followed.&lt;/p&gt;
&lt;h3 id=&quot;just-cover-your-mouth-and-nose&quot;&gt;Just cover your mouth and nose&lt;/h3&gt;
&lt;p&gt;If no face mask is available, just cover your mouth and nose with something else.&lt;/p&gt;
&lt;h3 id=&quot;will-it-stop-the-spread&quot;&gt;Will it stop the spread?&lt;/h3&gt;
&lt;p&gt;Do you trust your surgeon when it comes to not getting an infection?&lt;/p&gt;
&lt;div class=&quot;callout&quot; data-callout=&quot;note&quot; data-collapsible=&quot;false&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;&lt;div class=&quot;callout-title-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;1em&quot; height=&quot;1em&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;line x1=&quot;18&quot; y1=&quot;2&quot; x2=&quot;22&quot; y2=&quot;6&quot;&gt;&lt;/line&gt;&lt;path d=&quot;M7.5 20.5 19 9l-4-4L3.5 16.5 2 22z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/div&gt;&lt;div class=&quot;callout-title-text&quot;&gt;Note&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;callout-content&quot;&gt;&lt;p&gt;Update 2026: Typo and wording fixes, switch to UK English.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;</content:encoded><category>covid19</category><category>CDC</category></item><item><title>Why I decided to stay in Australia, with its darkest moment yet to come</title><link>https://cruftex.net/why-i-decided-to-stay-in-australia-with-its-darkest-moment-yet-to-come</link><guid isPermaLink="true">https://cruftex.net/why-i-decided-to-stay-in-australia-with-its-darkest-moment-yet-to-come</guid><description>Yesterday I needed to make the hard decision whether I should leave Australia or not.</description><pubDate>Sat, 21 Mar 2020 17:57:50 GMT</pubDate><content:encoded>&lt;p&gt;Yesterday I needed to make the hard decision whether I should leave Australia or not. Better leave, while there are
still flights? Better leave, while there is another country which did not close its borders yet? Better flee to a country with a
better health system? Better rescue myself and go to a country which keeps the virus in check? Ironically,
Hong Kong would be the much better choice now than Australia. Looking around, it is obvious that Australia
will face the same fate as Italy and Germany. But if I decide to stay, when will I be able to go back to Germany again?&lt;/p&gt;
&lt;p&gt;My personal deadline to fly out was Sunday. I decided this on Wednesday based on the data and estimated that I would not be allowed
to enter Singapore or Hong Kong any more after this Sunday without going into quarantine. Although still at 385, infections
rose exponentially and it was foreseeable that all countries would soon require all people coming from Australia to go
into a 14 day quarantine. In case there is still an airline that allows people from Australia to board the plane.&lt;/p&gt;
&lt;p&gt;On the other hand, I planned to travel until the end of April. Is there any reason not to do it? Maybe I get better medical treatment,
if I should need it, in another country. Maybe I get infected here, because new infections go through the roof. Am I putting my health
at risk?! I decided to stay. Things are bad at the moment, but there is still a day after tomorrow and we continue with our lives, as long
as we are responsible, do the reasonable thing, protect ourselves and care for one another.&lt;/p&gt;
&lt;p&gt;Thinking about it more, it turns out that deciding to leave would be the same reasoning as stockpiling toilet paper.
The world is not coming to an end!&lt;/p&gt;
&lt;p&gt;Am I crazy? With all that is happening, I continue with my travels? I am not. I am here now and it is most likely
more risky to catch the virus on two long haul flights than by staying and keeping my distance right now.&lt;/p&gt;
&lt;p&gt;Unfortunately, I needed to do some shopping.&lt;/p&gt;
&lt;p&gt;Sitting on the tram in Melbourne, there was a guy sitting across from me, chewing on his
dirty fingernails. I am the shy guy, otherwise I would have shouted: “You stupid idiot!” Instead, I said nothing.
Should I have said something? Most people I see in the city did not get the message. I cannot talk to everybody.
How should I say it?! Is it polite to remind people of their body hygiene or their behaviour? In the end,
I blamed myself for not having said something. For not at least having tried. If one who is infected infects at least
three others, how about the figure of a (maybe) not infected person talking to at least 10 others, to prevent them from
being infected?&lt;/p&gt;
&lt;p&gt;On the way back in the tram, a big family entered. A mum with three girls and a son. They sat down and started
munching on their fries and burgers. WITH THEIR FINGERS! I could not hold back and tried my luck: “Sorry, awww, sorry,
may I ask you a question?” - “Sure!” - “Did you wash your hands before you started eating?” The mum immediately shot out:
“Yes!!!” and I responded: “Very good!” I did not go all the way into a discussion whether they were sure that they did
not touch anything contaminated between washing and entering the tram. That would be too much for everybody and especially for me.
I played with my phone and observed what happened. Interestingly, the joyful munching came to an end after a few
more bites.&lt;/p&gt;
&lt;p&gt;When entering the accommodation, the neighbour taking the same lift up approached me, because he was irritated about me
wearing a face mask: “Do you know that wearing face masks only makes sense for people who are infected?” I answered:
“How do you know that I am not infected? I don’t test it every day!” A fruitful discussion arose, with a typical
argument I hear quite often here in Australia: “The government is not doing enough!” I got a bit angry and said:
“Why do we need the government, when everybody can do the reasonable thing?” Blaming and waiting does not
help anything.&lt;/p&gt;
&lt;p&gt;My decision: Speaking with people about their body hygiene and protective behaviour is the right thing to do, simply because
it’s affecting my personal health and everyone else’s. My life is in the hands of you guys, Australians! We are all in
this together! Can you please do better than Germany?!&lt;/p&gt;
&lt;p&gt;It turns out that a stubborn and logically thinking German is actually in a place where that line of thinking
seems to be needed the most!&lt;/p&gt;
&lt;div class=&quot;callout&quot; data-callout=&quot;note&quot; data-collapsible=&quot;false&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;&lt;div class=&quot;callout-title-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;1em&quot; height=&quot;1em&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;line x1=&quot;18&quot; y1=&quot;2&quot; x2=&quot;22&quot; y2=&quot;6&quot;&gt;&lt;/line&gt;&lt;path d=&quot;M7.5 20.5 19 9l-4-4L3.5 16.5 2 22z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/div&gt;&lt;div class=&quot;callout-title-text&quot;&gt;Note&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;callout-content&quot;&gt;&lt;p&gt;Update 2026: Typo and wording fixes, switch to UK English.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;</content:encoded><category>covid19</category><category>Australia</category></item><item><title>Java Caching Benchmarks Part 3 - The Finals</title><link>https://cruftex.net/java-caching-benchmarks-part-3</link><guid isPermaLink="true">https://cruftex.net/java-caching-benchmarks-part-3</guid><description>In the third article about benchmarking caching libraries it is finally time for a benchmark scenario which stresses the caches in different aspects.</description><pubDate>Fri, 01 Sep 2017 16:07:11 GMT</pubDate><content:encoded>&lt;p&gt;In the third article about benchmarking caching libraries it is finally time for a benchmark
scenario which stresses the caches in different aspects. Also, we will analyse whether the different approaches to eviction in cache2k have any negative effects and whether their implementation is robust and of production quality (spoiler: Yes, it is!).&lt;/p&gt;
&lt;p&gt;Impatient readers can skip to the &lt;a href=&quot;#conclusion&quot;&gt;Conclusion&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In the rest of the article the following points are discussed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Two benchmark scenarios which use different cache eviction algorithms.&lt;/li&gt;
&lt;li&gt;Memory consumption and comparison between the sizes of jar files for different implementations.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;table-of-contents&quot;&gt;Table of contents&lt;/h2&gt;
&lt;p&gt;&lt;/p&gt;&lt;details&gt;&lt;summary&gt;Open Table of contents&lt;/summary&gt;&lt;p&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#benchmark-setup&quot;&gt;Benchmark Setup&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#environment&quot;&gt;Environment&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#compared-caches-and-versions&quot;&gt;Compared Caches and Versions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#jmh-setup&quot;&gt;JMH Setup&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#threads-and-cores&quot;&gt;Threads and Cores&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#plots-units-and-confidence&quot;&gt;Plots Units and Confidence&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#a-comprehensive-benchmark&quot;&gt;A Comprehensive Benchmark&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#worst-case-eviction-performance&quot;&gt;Worst Case Eviction Performance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#average-items-scanned-in-clock-pro&quot;&gt;Average Items Scanned in Clock-Pro&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#memory-consumption&quot;&gt;Memory Consumption&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#jar-file-sizes&quot;&gt;Jar File Sizes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#benchmark-shortcomings&quot;&gt;Benchmark Shortcomings&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#future-work&quot;&gt;Future Work&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;/p&gt;&lt;/details&gt;&lt;p&gt;&lt;/p&gt;
&lt;h2 id=&quot;benchmark-setup&quot;&gt;Benchmark Setup&lt;/h2&gt;
&lt;p&gt;Benchmark code is available in the GitHub project &lt;a href=&quot;https://github.com/cache2k/cache2k-benchmark&quot;&gt;cache2k benchmark&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;environment&quot;&gt;Environment&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;JDK: Oracle Version JDK 1.8.0_131, VM 25.131-b11&lt;/li&gt;
&lt;li&gt;JVM flags: &lt;code&gt;-server -Xmx10G -XX:BiasedLockingStartupDelay=0&lt;/code&gt; (with and without &lt;code&gt;-XX:+UseG1GC&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;JMH Version: 1.18&lt;/li&gt;
&lt;li&gt;CPU: Intel(R) Xeon(R) CPU E3-1240 v5 @ 3.50GHz, 4 physical cores&lt;/li&gt;
&lt;li&gt;CPU L1 cache: 4 x 32 KB 8-way set associative instruction caches, 4 x 32 KB 8-way set associative data caches&lt;/li&gt;
&lt;li&gt;CPU L2 cache: 4 x 256 KB 4-way set associative caches&lt;/li&gt;
&lt;li&gt;CPU L3 cache: 8 MB 16-way set associative shared cache&lt;/li&gt;
&lt;li&gt;RAM: 32GB total, 2x16GB DRR4 at 2133 MHz&lt;/li&gt;
&lt;li&gt;OS: Ubuntu 14.04&lt;/li&gt;
&lt;li&gt;Kernel: 4.4.0-57-generic&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;compared-caches-and-versions&quot;&gt;Compared Caches and Versions&lt;/h3&gt;
&lt;p&gt;We will compare cache2k with these ‘cache’ implementations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Google Guava Cache, Version 20&lt;/li&gt;
&lt;li&gt;Caffeine, Version 2.5.2&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cache2k.org&quot;&gt;cache2k&lt;/a&gt;, Version 1.0.0.Final&lt;/li&gt;
&lt;li&gt;EHCache, Version 2.10.3&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;jmh-setup&quot;&gt;JMH Setup&lt;/h3&gt;
&lt;p&gt;The JMH command line arguments &lt;code&gt;-f 2 -wi 5 -w 30s -i 3 -r 30s&lt;/code&gt; result in:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Forks: 2 (2 runs in a newly started JVM)&lt;/li&gt;
&lt;li&gt;Warmup: 5 iterations, 30 seconds each&lt;/li&gt;
&lt;li&gt;Measurement: 3 iterations, 30 seconds each&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Five warmup iterations of 30 seconds each lead to 2:30 minutes of warmup. A longer warmup is needed especially
for the benchmark runs with large heap sizes (10M cache entries) to allow the garbage collector to adapt and have
steadier behaviour.&lt;/p&gt;
&lt;h3 id=&quot;threads-and-cores&quot;&gt;Threads and Cores&lt;/h3&gt;
&lt;p&gt;The number of threads is equal to the number of cores available to the JVM. To disable cores we can use the
Linux CPU hotplug feature. This way only enabled processors are exposed by the OS and the output of
the Java method &lt;code&gt;Runtime.getRuntime().availableProcessors()&lt;/code&gt; is equal to the number of usable physical
processor cores. No hyperthreading is in effect.&lt;/p&gt;
&lt;h3 id=&quot;plots-units-and-confidence&quot;&gt;Plots Units and Confidence&lt;/h3&gt;
&lt;p&gt;In the plots below, units are in SI (base 10), which means 1MB is 10^6 bytes. This ensures the unit prefixes
for operations/s and bytes/s are the same.&lt;/p&gt;
&lt;p&gt;Every bar has a confidence interval associated with it. This interval does not just represent the upper and
lower bounds of a measured value, but shows a range of potential values. The confidence interval is
calculated by JMH with a level of 99.9% (which means the likelihood that the actual value is within the
shown interval is 99.9%).&lt;/p&gt;
&lt;h2 id=&quot;a-comprehensive-benchmark&quot;&gt;A Comprehensive Benchmark&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-1&quot;&gt;Part 1&lt;/a&gt; of the series focused on comparing cache
implementations to hash maps, in which case the test scenario did not trigger eviction, while &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-2&quot;&gt;Part 2&lt;/a&gt; focused on benchmarking different eviction algorithms. Both benchmarks were designed to test two different aspects of caching in isolation and they have given interesting insights into detailed aspects.&lt;/p&gt;
&lt;p&gt;However, the outcome may be totally different in other scenarios, such as when concurrent threads utilise the eviction algorithm as opposed to only one thread as in Part 2.&lt;/p&gt;
&lt;p&gt;We have combined a few such aspects into one benchmark:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A skewed access pattern via the Zipfian distribution to utilise the cache eviction algorithm&lt;/li&gt;
&lt;li&gt;Different key space sizes, yielding different hit rates&lt;/li&gt;
&lt;li&gt;A cache miss gets a significant penalty by burning a defined amount of CPU cycles&lt;/li&gt;
&lt;li&gt;Read through operation, which is commonly used but may result in additional blocking overhead&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The access pattern is artificially generated via the so-called Zipfian distribution. This access pattern represents a simulation
of a skewed sequence typically found in applications these days. The Zipfian distribution is widely used for
benchmarking key/value stores. The algorithm was originally described in &lt;a href=&quot;http://dl.acm.org/citation.cfm?doid=191839.191886&quot;&gt;“Quickly Generating Billion-Record Synthetic Databases”, Jim Gray et al, SIGMOD 1994&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In a few other cache benchmarks, the access sequence and key objects are calculated in advance, so that the benchmark
only covers the performance of the cache access. This approach was tested in different variations but proved to be problematic
in a few cases. If the sequence is too small, eviction algorithms can adapt to it and show unrealistically high hit rates.
Furthermore, a separate sequence is needed for each thread. To avoid any adaptation effects on repetition, the sequences
must be much longer than the cache size. Big cache sizes in the benchmark are around 10 million entries, which makes this infeasible.
The best solution for this proved to be the online generation of the access sequence. The Zipfian generator implementation
from the Yahoo! YCSB benchmark was improved for fast and low overhead generation by replacing the random number generator used with the fast random sequence generator &lt;code&gt;XorShift1024StarRandomGenerator&lt;/code&gt; of the &lt;a href=&quot;http://dsiutils.di.unimi.it/&quot;&gt;DSI utilities&lt;/a&gt;. This means
the sequence generation and the creation of the integer objects for the cache keys are included in the performance measurements.&lt;/p&gt;
&lt;p&gt;The Zipfian sequence is used in variations to yield different cache hit rates. Zipfian factor 10 signifies the generation of a
key number space 10 times bigger than the cache size. A certain Zipfian factor yields approximately the same hit rate for
different cache sizes.&lt;/p&gt;
&lt;p&gt;The cache operates in read through mode. A cache miss results in a call to a cache loader, which adds a penalty by burning CPU cycles
via JMH’s black hole: &lt;code&gt;Blackhole.consumeCPU(1000)&lt;/code&gt;. Other benchmarks use &lt;code&gt;Thread.sleep(10)&lt;/code&gt; (or something similar)
to sleep for 10 milliseconds and simulate I/O latency. Using &lt;code&gt;Thread.sleep()&lt;/code&gt; for such a small time may result in inconsistency.
Such code would be impacted by the operating system scheduler.&lt;/p&gt;
&lt;p&gt;Benchmark source file: &lt;a href=&quot;https://github.com/cache2k/cache2k-benchmark/blob/e4cd7a8c491bf275545b3003932c2eebb69606e9/jmh-suite/src/main/java/org/cache2k/benchmark/jmh/suite/eviction/symmetrical/ZipfianSequenceLoadingBenchmark.java&quot;&gt;ZipfianSequenceLoadingBenchmark.java&lt;/a&gt;.&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/ZipfianSequenceLoadingBenchmark-bySize-4x10.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;ZipfianSequenceLoadingBenchmark, operations per second by cache size at 4 threads and Zipfian factor 10.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/ZipfianSequenceLoadingBenchmark-bySize-4x10.dat&quot;&gt;ZipfianSequenceLoadingBenchmark-bySize-4x10.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;CPU profiling with a cache size of 1M and Zipfian factor 10, 4 threads and cache2k shows that 35% of the CPU cycles are
spent in the black hole. If this time were used for the loader, hit rates of the cache implementations would improve. As explained before,
the throughput is also determined by the speed of the random number generator. Benchmarking the random number generator and the generation of the integer keys alone results in 23 million operations per second for the CMS collector and 13 million operations
per second for the G1 collector. Interpreting the graph and concluding that the fastest cache is about 50% faster than its
competitor would be wrong (cache2k vs. Caffeine with 100K entries), since the benchmark code itself has a significant overhead. Comparing only the time spent in the cache code, the performance difference would be bigger.&lt;/p&gt;
&lt;p&gt;Let’s look at the achieved hit rates:&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/ZipfianSequenceLoadingBenchmarkEffectiveHitrate-bySize-4x10.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;ZipfianSequenceLoadingBenchmark, effective hit rate by cache size at 4 threads and Zipfian factor 10.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/ZipfianSequenceLoadingBenchmarkEffectiveHitrate-bySize-4x10.dat&quot;&gt;ZipfianSequenceLoadingBenchmarkEffectiveHitrate-bySize-4x10.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Caffeine and cache2k have significantly higher hit rates by about 4%, cache2k is slightly better than Caffeine by about 0.15%.&lt;/p&gt;
&lt;p&gt;Another benchmark is analysed with different numbers of threads and CPU cores. As explained earlier, the number of available CPU cores is equal to the number of benchmark threads.&lt;/p&gt;
&lt;p&gt;Let’s look at the results for one, two and four cores:&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/ZipfianSequenceLoadingBenchmark-byThread-1Mx10.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;ZipfianSequenceLoadingBenchmark, operations per second by thread count with cache size 1M and Zipfian factor 10.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/ZipfianSequenceLoadingBenchmark-byThread-1Mx10.dat&quot;&gt;ZipfianSequenceLoadingBenchmark-byThread-1Mx10.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;EHCache2 does not scale well in this scenario with the additional cores, which is probably caused by additional locking overhead in the blocking read through configuration.&lt;/p&gt;
&lt;p&gt;Caffeine has low performance with one or two cores but better performance with four cores. This is most likely caused by the fact that Caffeine uses multiple threads for the eviction. With one or a few cores available, the inter thread communication produces overhead and causes delays.&lt;/p&gt;
&lt;p&gt;By varying the size of the Zipfian distribution we can yield different hit rates. The chart below shows the performance with different Zipfian factors:&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/ZipfianSequenceLoadingBenchmark-byFactor-1M-4.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;ZipfianSequenceLoadingBenchmark, operations per second by Zipfian factor with cache size 1M at 4 threads.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/ZipfianSequenceLoadingBenchmark-byFactor-1M-4.dat&quot;&gt;ZipfianSequenceLoadingBenchmark-byFactor-1M-4.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;The corresponding effective hit rates per cache are:&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/ZipfianSequenceLoadingBenchmarkEffectiveHitrate-byFactor-1M-4.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;ZipfianSequenceLoadingBenchmark, effective hit rate by Zipfian factor with cache size 1M at 4 threads.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/ZipfianSequenceLoadingBenchmarkEffectiveHitrate-byFactor-1M-4.dat&quot;&gt;ZipfianSequenceLoadingBenchmarkEffectiveHitrate-byFactor-1M-4.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;cache2k is optimised in such a way that there is minimal overhead at high hit rates. A few processes are delayed as long as possible until eviction. That’s why the relative advantage of cache2k becomes smaller for lower hit rates.&lt;/p&gt;
&lt;p&gt;We also analysed another benchmark run with the new G1 collector. Let’s look at the first statistic with a different collector implementation:&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-part-3/G1/ZipfianSequenceLoadingBenchmark-bySize-4x10.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;ZipfianSequenceLoadingBenchmark, operations per second by cache size at 4 threads and Zipfian factor 10 and G1 collector.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-part-3/G1/ZipfianSequenceLoadingBenchmark-bySize-4x10.dat&quot;&gt;ZipfianSequenceLoadingBenchmark-bySize-4x10.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;With a cache size of around 1M entries cache2k profits highly from its low allocation rates. All other cache implementations have a higher allocation rate, resulting in lower throughput. With a cache size of 10M, cache2k and Guava face a higher loss, because with this cache size these two cache implementations cause more work for G1 than the other implementations. In the next benchmark, this impact becomes even more visible.&lt;/p&gt;
&lt;h2 id=&quot;worst-case-eviction-performance&quot;&gt;Worst Case Eviction Performance&lt;/h2&gt;
&lt;p&gt;cache2k uses an eviction algorithm which may have different performance outcomes depending on the access sequence. We
will analyse another benchmark to check the most unfavourable working conditions. This is to make sure that there are no
unexpected drawbacks when used in varying production scenarios.&lt;/p&gt;
&lt;p&gt;cache2k uses the eviction algorithm Clock-Pro (described in
&lt;a href=&quot;http://www.ece.eng.wayne.edu/~sjiang/pubs/papers/jiang05_CLOCK-Pro.pdf&quot;&gt;USENIX’05: “CLOCK-Pro: An Effective Improvement of the CLOCK Replacement”&lt;/a&gt;),
which scans through a circular list of entries (the clock) for an eviction candidate. This algorithm has a theoretical worst case performance of O(n), which means eviction performance will decrease linearly with the number of cached items. cache2k incorporates
the following changes to improve eviction efficiency and worst case runtime:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A reference counter is used instead of a reference bit&lt;/li&gt;
&lt;li&gt;An adaptive threshold based on the access frequency is used to search for an eviction candidate&lt;/li&gt;
&lt;li&gt;Separate clocks are used for hot and cold entries and the entry order in the hot working set is reshuffled according to the recent references&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-part-3/clocks.svg&quot; alt=&quot;Improved Clock-Pro eviction using two separate clocks&quot;/&gt;&lt;/p&gt;
&lt;p&gt;Nevertheless, the question of the theoretical categorisation of O(n) remains. However, the theoretical big-O notation only means there is
no mathematical proof that the worst case performance is in a better big-O category. Theory gives us an indication that
the worst case performance may be a problem, so what does it look like in practice? Let’s quote Richard P. Feynman:
&lt;em&gt;It doesn’t matter how beautiful your theory is, it doesn’t matter how smart you are. If it doesn’t agree
with experiment, it’s wrong.&lt;/em&gt; One prominent example where the theoretical performance does not match the
practical performance is the QuickSort algorithm, as discussed in
&lt;a href=&quot;http://stackoverflow.com/questions/70402/why-is-quicksort-better-than-mergesort&quot;&gt;Why is quicksort better than mergesort?&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;To simulate a worst case scenario we use a random access pattern. Because of the uniform distribution across the key
space, cache2k with Clock-Pro will need a longer scan to find a good eviction candidate. While there can be
artificially constructed sequences for a known cache size and internal tuning parameters which might trigger
even longer scans, we believe that a random sequence yields the worst eviction performance that can be observed
in practice.&lt;/p&gt;
&lt;p&gt;Besides evaluating the behaviour of cache2k, a benchmark with a random pattern can be used to verify that caches
have comparable eviction policies. When the size parameter is honoured correctly, all caches are expected
to produce the same hit rate.&lt;/p&gt;
&lt;p&gt;Similar to the benchmark described before, the access sequence is generated by a separate random generator per thread. The fast random sequence generator &lt;code&gt;XorShift1024StarRandomGenerator&lt;/code&gt; of the &lt;a href=&quot;http://dsiutils.di.unimi.it/&quot;&gt;DSI utilities&lt;/a&gt; is used for this purpose.
In this case a cache miss gets no extra penalty. As the effective hit rate is identical across cache
implementations, the penalty would only change the performance result by a constant factor. So in this benchmark the performance
is solely determined by the sequence generator and the cache operations.&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/RandomSequenceBenchmark-byHitrate-4-1M.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;RandomSequenceBenchmark, operations per second by hit rate at 4 threads and 1M cache size.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/RandomSequenceBenchmark-byHitrate-4-1M.dat&quot;&gt;RandomSequenceBenchmark-byHitrate-4-1M.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Let’s also verify that the effective hit rate is on the same level:&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/RandomSequenceBenchmarkEffectiveHitrate-byHitrate-4-1M.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;RandomSequenceBenchmark, effective hit rate by thread count at 4 threads and 1M cache size.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/RandomSequenceBenchmarkEffectiveHitrate-byHitrate-4-1M.dat&quot;&gt;RandomSequenceBenchmarkEffectiveHitrate-byHitrate-4-1M.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;To see how the cache size impacts the performance, let’s look at different cache sizes with constant thread count and hit rate:&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/RandomSequenceBenchmark-bySize-4x50.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;RandomSequenceBenchmark, operations per second by cache size at 4 threads and 50 percent target hit rate.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/RandomSequenceBenchmark-bySize-4x50.dat&quot;&gt;RandomSequenceBenchmark-bySize-4x50.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;As we can see, the performance of cache2k is similar to other cache implementations when the cache size is increased.&lt;/p&gt;
&lt;p&gt;We also analysed a benchmark run with the new G1 garbage collector:&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-part-3/G1/RandomSequenceBenchmark-bySize-4x50.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;RandomSequenceBenchmark, operations per second by cache size at 4 threads and 50 percent target hit rate with G1 collector.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-part-3/G1/RandomSequenceBenchmark-bySize-4x50.dat&quot;&gt;RandomSequenceBenchmark-bySize-4x50.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;With the new G1 collector all cache implementations show significantly reduced performance when cache sizes are increased.
The implementations cache2k and the Google Guava cache have an even higher performance drop for G1. Doing CPU profiling
with cache2k reveals that 30-40% of the CPU time is spent in refinement tasks of G1, which is caused by inter region
reference updates. This benchmark shows that throughput oriented applications with lots of reference mutations and
larger heaps should not choose the G1 collector. The effects of G1 with large cache sizes need to undergo a
deeper analysis in case this becomes more relevant in practice.&lt;/p&gt;
&lt;p&gt;This is an extreme test scenario to reveal the limits of cache2k. We unintentionally constructed a scenario where the use of
the G1 collector has a rather big performance loss. 10 million entries and a cache operating at a hit rate of 50% would
be a rare operating condition. Most likely this can be found in throughput oriented applications, which should better
choose the CMS collector. Under normal working conditions, as we have shown above, cache2k achieves
a performance gain with G1 compared to other cache libraries.&lt;/p&gt;
&lt;h2 id=&quot;average-items-scanned-in-clock-pro&quot;&gt;Average Items Scanned in Clock-Pro&lt;/h2&gt;
&lt;p&gt;As explained above cache2k uses the Clock-Pro algorithm, which scans the cache entries organised in a
circular data structure to select the best entry to evict. There is no upper bound on the number of
scanned entries in cache2k. In this chapter we investigate how the scan effort changes with the cache
size.&lt;/p&gt;
&lt;p&gt;When the cache parameters are known, there is a theoretical chance to construct an artificial access
pattern that triggers a full scan over all entries. Such an access pattern could be a vector for
DoS attacks. But after a full scan all access counters are zeroed, so the attack sequence
needs to be repeated and all cached entries need to be covered. The cost of sending and processing the attack
sequence would thus be much higher than the damage caused.&lt;/p&gt;
&lt;p&gt;Using the internal counters of cache2k, we can extract the average number of entries scanned per eviction.
This allows us to get an insight into the different eviction costs under different working
conditions. Let’s look at how different cache sizes affect the number of scans:&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/RandomSequenceBenchmarkScanCount-bySize-4x80.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;RandomSequenceBenchmark, scan count by cache size at 4 threads and 80 percent target hit rate.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/RandomSequenceBenchmarkScanCount-bySize-4x80.dat&quot;&gt;RandomSequenceBenchmarkScanCount-bySize-4x80.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;The results are rather surprising. Scan counts are almost equal for the same hit rate for any cache size.
Raw values for reference:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Size 100K: 6.003983263650306&lt;/li&gt;
&lt;li&gt;Size 1M: 6.004637918679662&lt;/li&gt;
&lt;li&gt;Size 10M: 6.004827089682682&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The modified Clock-Pro algorithm in cache2k works well: For a worst case access sequence the scan
count does not increase significantly when the cache becomes bigger.&lt;/p&gt;
&lt;p&gt;For different hit rates the scan counts look like this:&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/RandomSequenceBenchmarkScanCount-byHitrate-4x1M.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;RandomSequenceBenchmark, scan count by hit rate at 4 threads and 1M cache size.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/RandomSequenceBenchmarkScanCount-byHitrate-4x1M.dat&quot;&gt;RandomSequenceBenchmarkScanCount-byHitrate-4x1M.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;The scan count increases for higher hit rates because all the cache contents become ‘hotter’. The amount of additional scanning
when hit rates become higher can be tuned by some internal parameters. As we can see with this benchmark, the tuning parameters are chosen in a way that the additional scanning work in the eviction does not outweigh the performance gains of the improved hit rate.
With high hit rates cache2k still has less overhead than other cache libraries.&lt;/p&gt;
&lt;p&gt;Finally, we will take a look at the scan counts for the first benchmark:&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/ZipfianSequenceLoadingBenchmarkScanCount-bySize-4x10.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;ZipfianSequenceLoadingBenchmark, scan count by cache size at 4 threads and Zipfian factor 10.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/ZipfianSequenceLoadingBenchmarkScanCount-bySize-4x10.dat&quot;&gt;ZipfianSequenceLoadingBenchmarkScanCount-bySize-4x10.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;For a typical skewed access sequence the average scan count is very low. In the tested scenarios it is
below two. For realistic (non-random) access sequences, the experiment shows that the scan count
decreases for higher cache sizes. The result of this practical scenario is in contradiction to the theoretical evaluation.&lt;/p&gt;
&lt;h2 id=&quot;memory-consumption&quot;&gt;Memory Consumption&lt;/h2&gt;
&lt;p&gt;For the evaluation of the memory consumption we use the first benchmark (&lt;code&gt;ZipfianSequenceLoadingBenchmark&lt;/code&gt;).
The details of the metrics and the measurement procedure were presented in a previous blog post,
&lt;a href=&quot;https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks&quot;&gt;The 6 Memory Metrics You Should Track in Your Java Benchmarks&lt;/a&gt;.
For concision we concentrate on two metrics only. &lt;em&gt;usedMem_settled&lt;/em&gt; represents the used memory
that the JVM reports at the end of the benchmark. This is a static measure and does not take into
account differences in usage when operations are ongoing. The &lt;em&gt;VmHWM&lt;/em&gt; metric represents peak memory
consumption as reported by the operating system, thus it also includes dynamic effects like garbage collection.&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/ZipfianSequenceLoadingBenchmarkStaticPeakMemory4-10M-5.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;ZipfianSequenceLoadingBenchmark, memory consumption with 10M cache size at 4 threads and Zipfian factor 5.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-part-3/CMS/ZipfianSequenceLoadingBenchmarkStaticPeakMemory4-10M-5.dat&quot;&gt;ZipfianSequenceLoadingBenchmarkStaticPeakMemory4-10M-5.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;In the tested scenario cache2k has an 11% higher static memory consumption than Caffeine. In this scenario
the payload data (keys and values) consists only of integer objects. With larger data sizes in practical applications,
the difference in static memory consumption between the cache implementations would be much lower.&lt;/p&gt;
&lt;p&gt;The peak memory consumption differs more drastically. Cache2k achieves a lower peak memory consumption because of its
low allocation rates, which lead to less garbage collector activity. Depending on the cache utilisation, cache size
and hit rates, the total memory consumption will differ. Here is a rather extreme example with the G1 collector:&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-part-3/G1/ZipfianSequenceLoadingBenchmarkStaticPeakMemory4-1M-5.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;ZipfianSequenceLoadingBenchmark, memory consumption with 1M cache size at 4 threads and Zipfian factor 20.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-part-3/G1/ZipfianSequenceLoadingBenchmarkStaticPeakMemory4-1M-5.dat&quot;&gt;ZipfianSequenceLoadingBenchmarkStaticPeakMemory4-1M-5.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Interesting side note: Since cache eviction produces garbage, increasing the cache size could have a
dramatic effect on the peak memory consumption and can actually lower the amount of memory needed, as less garbage means the garbage collector needs less breathing space.&lt;/p&gt;
&lt;p&gt;Looking only at the static used heap memory can give the wrong idea about the physical memory actually used in operation.&lt;/p&gt;
&lt;h2 id=&quot;jar-file-sizes&quot;&gt;Jar File Sizes&lt;/h2&gt;
&lt;p&gt;Finally, let’s look at the jar file sizes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;caffeine-2.5.2.jar: 1.007.339 bytes&lt;/li&gt;
&lt;li&gt;cache2k-all-1.0.0.Final.jar: 387.771 bytes&lt;/li&gt;
&lt;li&gt;cache2k-api-1.0.0.Final.jar + cache2k-core-1.0.0.Final.jar: 321.816 bytes&lt;/li&gt;
&lt;li&gt;ehcache-core-2.10.3.jar: 1.386.653 bytes&lt;/li&gt;
&lt;li&gt;guava-20.0.jar: 2.442.625 bytes&lt;/li&gt;
&lt;li&gt;guava cache only alternative: com.nytimes.android:cache-2.0.3.jar: 160.474 bytes&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For Guava cache users who don’t need the whole Guava library, The New York Times has provided a jar file which contains only
the cache functionality. For Android or other environments where the download size matters, Guava and
cache2k are the best choices. Compared to the others, the Caffeine jar is rather big due to the high number of generated classes.&lt;/p&gt;
&lt;h2 id=&quot;benchmark-shortcomings&quot;&gt;Benchmark Shortcomings&lt;/h2&gt;
&lt;p&gt;All of our benchmark scenarios intentionally stress the cache heavily to amplify differences in the
cache implementations and reveal weaknesses. The outcome in real applications will be much better.&lt;/p&gt;
&lt;p&gt;Here is a summary of noteworthy points that may influence the real world performance:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value and key in the benchmark are only integer objects. Larger and more complex objects will
lead to higher allocation rates. The overhead in the internal data structures of the cache becomes
less significant.&lt;/li&gt;
&lt;li&gt;Different latency and CPU cycles for loading a cache value&lt;/li&gt;
&lt;li&gt;CPU time used by the application&lt;/li&gt;
&lt;li&gt;Interactive or throughput oriented workload&lt;/li&gt;
&lt;li&gt;Selected garbage collector and its tuning values&lt;/li&gt;
&lt;li&gt;Forced full GC between iterations may influence benchmark results&lt;/li&gt;
&lt;li&gt;A real world access sequence is different from the Zipfian sequence&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The last point cannot be stressed enough. Testing with the Zipfian distribution is heavily in favour of
caches which keep frequently used entries for a longer time (frequency aspect). As the access pattern is always
in the same value range, a cache could actually stop replacing entries after a set of hot entries is
established. The next benchmarks should modify the value ranges to test the speed of adaptation, e.g. in a phase
change of the application.&lt;/p&gt;
&lt;p&gt;Running all benchmarks with the CMS and G1 collectors with the needed precision takes four days on one machine.
Especially the large cache size of 10M needs longer benchmark run times. Because of the time required for a run, we could only
test three variations each of thread count, cache size and hit rate.&lt;/p&gt;
&lt;h2 id=&quot;future-work&quot;&gt;Future Work&lt;/h2&gt;
&lt;p&gt;A lot of work went into this blog post and at this point it is important to publish results rather than to
continue digging deeper. This is what we left for the future:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Use a sweeping Zipfian sequence, to test adaptation speed (see above)&lt;/li&gt;
&lt;li&gt;Benchmarks with a higher number of cores/threads&lt;/li&gt;
&lt;li&gt;Benchmark with latency&lt;/li&gt;
&lt;li&gt;Clock-Pro eviction: Analyse/compare the scan counts with the original algorithm&lt;/li&gt;
&lt;li&gt;Clock-Pro eviction: Deeper analysis of worst case patterns close to 100% hit rate&lt;/li&gt;
&lt;li&gt;Clock-Pro eviction: Capping the eviction scans will reduce computing cost for random access patterns&lt;/li&gt;
&lt;li&gt;Deeper analysis of the interaction with G1, e.g. benchmark runs with a finer variation of cache sizes&lt;/li&gt;
&lt;li&gt;Improve cache2k eviction for the G1 collector&lt;/li&gt;
&lt;li&gt;In general, caching and GC interaction is worth further research, e.g. analysing the balance between cache
size and GC activity&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Cache2k uses an alternative eviction algorithm (Clock-Pro) that reduces the latency overhead of the cache access
to a minimum. After analysing the benchmark results we can see that cache2k has better performance than other caches
when operating at high hit rates, approximately above 80%. With lower hit rates, which cause more eviction activity,
the performance of cache2k is at least on the same level as other cache implementations.&lt;/p&gt;
&lt;p&gt;Since the eviction runtime of the Clock-Pro algorithm could theoretically grow linearly with the cache size, cache2k includes
improvements over the original algorithm. Our experiments have proven that the performance of the implementation is stable in the worst
possible scenarios. Because of this intensive testing we can say that the implementation has production quality.&lt;/p&gt;
&lt;p&gt;As the achieved hit rates show, Caffeine and cache2k both use an effective and modern eviction algorithm that
performs better than LRU for the Zipfian sequence.&lt;/p&gt;
&lt;p&gt;Because of its low allocation rates, cache2k performs better with the upcoming G1 garbage collector than other
caches in typical working conditions.&lt;/p&gt;
&lt;p&gt;The memory overhead differs for each cache implementation. When compared to Guava or Caffeine, cache2k has
slightly more static memory overhead. However, in high throughput scenarios cache2k can achieve a much lower
total memory consumption because of its low allocation rates.&lt;/p&gt;
&lt;div class=&quot;callout&quot; data-callout=&quot;note&quot; data-collapsible=&quot;false&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;&lt;div class=&quot;callout-title-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;1em&quot; height=&quot;1em&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;line x1=&quot;18&quot; y1=&quot;2&quot; x2=&quot;22&quot; y2=&quot;6&quot;&gt;&lt;/line&gt;&lt;path d=&quot;M7.5 20.5 19 9l-4-4L3.5 16.5 2 22z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/div&gt;&lt;div class=&quot;callout-title-text&quot;&gt;Note&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;callout-content&quot;&gt;&lt;p&gt;Update 2026: Typo and wording fixes, switch to UK English.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;</content:encoded><category>Java</category><category>caching</category><category>cache2k</category><category>benchmark</category></item><item><title>The 6 Memory Metrics You Should Track in Your Java Benchmarks</title><link>https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks</link><guid isPermaLink="true">https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks</guid><description>Analysing or monitoring the memory consumption of a Java application is no easy task, but benchmarks require consistent results.</description><pubDate>Tue, 28 Mar 2017 09:56:13 GMT</pubDate><content:encoded>&lt;p&gt;Analysing or monitoring the memory consumption of a Java application is no easy task,
but benchmarks require consistent results. Ironically, when I google for
“benchmark java memory consumption” a Stack Overflow question of mine from 2015
comes up in first place. Well, two years later, after trying a lot of things that did not work, it is time
for a summary.&lt;/p&gt;
&lt;p&gt;This blog post touches different subjects, including:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Try and discuss all the different ways to get memory consumption metrics&lt;/li&gt;
&lt;li&gt;How to integrate memory consumption metrics in JMH&lt;/li&gt;
&lt;li&gt;Compare the memory consumption of in process caching libraries&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The main focus will be on the first one.&lt;/p&gt;
&lt;p&gt;The impatient reader can skip to the &lt;a href=&quot;#conclusion&quot;&gt;Conclusion&lt;/a&gt;. The rest of the article is organised as follows:
After giving a motivation, we present relevant techniques to measure memory consumption and show their practical results
based on an example benchmark. The benchmark and its setup are described at the end of the article.&lt;/p&gt;
&lt;h2 id=&quot;table-of-contents&quot;&gt;Table of contents&lt;/h2&gt;
&lt;p&gt;&lt;/p&gt;&lt;details&gt;&lt;summary&gt;Open Table of contents&lt;/summary&gt;&lt;p&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#why-care&quot;&gt;Why Care?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#the-naive-approach&quot;&gt;The Naive Approach&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#metric-object-graph-traversing-libraries&quot;&gt;Metric: Object Graph Traversing Libraries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#metric-used-memory-after-forced-gc&quot;&gt;Metric: Used Memory after Forced GC&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#metric-used-memory-after-forced-gc-and-settling&quot;&gt;Metric: Used Memory after Forced GC and Settling&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#metric-used-heap-memory&quot;&gt;Metric: Used Heap Memory&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#metric-maximum-used-memory-via-gc-notification&quot;&gt;Metric: Maximum Used Memory via GC Notification&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#showtime-used-memory-graphs&quot;&gt;Showtime: Used Memory Graphs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#metric-total-memory&quot;&gt;Metric: Total Memory&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#metric-process-vmrss-and-vmhwm-reported-by-linux&quot;&gt;Metric: Process VmRSS and VmHWM reported by Linux&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#showtime-totalcommitted-memory-graphs&quot;&gt;Showtime: Total/Committed Memory Graphs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#metric-allocation-rate&quot;&gt;Metric: Allocation Rate&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#addendum-benchmark-setup-description&quot;&gt;Addendum: Benchmark Setup Description&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#benchmark&quot;&gt;Benchmark&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#environment&quot;&gt;Environment&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#compared-caches-and-versions&quot;&gt;Compared Caches and Versions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#jmh-setup&quot;&gt;JMH Setup&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#threads-and-cores&quot;&gt;Threads and Cores&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#plots-with-confidence&quot;&gt;Plots With Confidence&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#updates&quot;&gt;Updates&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#update-from-2017-04-03&quot;&gt;Update from 2017-04-03&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;/p&gt;&lt;/details&gt;&lt;p&gt;&lt;/p&gt;
&lt;h2 id=&quot;why-care&quot;&gt;Why Care?&lt;/h2&gt;
&lt;p&gt;While continuing to create different benchmarks for caching libraries and improving &lt;a href=&quot;https://cache2k.org&quot;&gt;cache2k&lt;/a&gt;, we discovered that important
information is missing. Let’s look at a benchmark result:&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks/G1/ZipfianSequenceLoadingBenchmark.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;ZipfianSequenceLoadingBenchmark with G1 collector, 4 threads, 1M cache entries, Zipfian factor 10, throughput in operations per second.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks/G1/ZipfianSequenceLoadingBenchmark.dat&quot;&gt;ZipfianSequenceLoadingBenchmark.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;The graph shows the throughput in operations per second for a capacity limit of 1M entries. The benchmark setup
is described in more detail at the end of the article.&lt;/p&gt;
&lt;p&gt;A cache library could “cheat” and store more entries than configured. Conversely, when one cache is effectively consuming
more memory than the other, the cache library with the better memory footprint can obviously store more entries
within the same amount of memory and yield a better throughput.&lt;/p&gt;
&lt;p&gt;A cache is a typical example of the &lt;a href=&quot;https://en.wikipedia.org/wiki/Space%E2%80%93time_tradeoff&quot;&gt;memory/time or space/time trade off&lt;/a&gt;.
The throughput can be improved by increasing the memory. When benchmarking code with memory/time trade offs, keeping track
of the consumed memory is essential when comparing the different implementations.&lt;/p&gt;
&lt;h2 id=&quot;the-naive-approach&quot;&gt;The Naive Approach&lt;/h2&gt;
&lt;p&gt;The first naive approach is asking the JVM how much memory is used. Here we have the first confusion.
We can retrieve the total memory via &lt;code&gt;Runtime.getRuntime().totalMemory()&lt;/code&gt; and the free memory via
&lt;code&gt;Runtime.getRuntime().freeMemory()&lt;/code&gt; but obviously not the used memory. Calculating just the difference
isn’t always correct, since a GC may happen between the two method calls.&lt;/p&gt;
&lt;p&gt;Retrieving and calculating the used memory will result in lots of different values between the used memory
and the total memory. It just depends on when we make the call: After a GC, before the GC or somewhere in between.&lt;/p&gt;
&lt;p&gt;We are looking for methods that produce reliable results with low variance, which don’t produce extra overhead
during benchmarking and can be recorded without extra effort.&lt;/p&gt;
&lt;h2 id=&quot;metric-object-graph-traversing-libraries&quot;&gt;Metric: Object Graph Traversing Libraries&lt;/h2&gt;
&lt;p&gt;The memory size of an object can be determined by using the Java instrumentation functionality or via
methods in the &lt;code&gt;sun.misc.Unsafe&lt;/code&gt; class. Based on this, several solutions exist to estimate the memory
consumption of an object graph. The popular ones are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/ehcache/sizeof&quot;&gt;EHCache sizeof&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/jbellis/jamm&quot;&gt;Java Agent for Memory Measurements&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This method is used in the &lt;a href=&quot;https://github.com/ben-manes/caffeine/wiki/Memory-overhead&quot;&gt;Caffeine memory overhead comparison&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Advantages:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Different configurations can be evaluated quickly, no GC run or JVM restart is needed&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Disadvantages:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It is difficult to use this method for benchmarking in general since choosing the root objects needs
some extra work and typically also knowledge about implementation details.&lt;/li&gt;
&lt;li&gt;Comparisons with this library typically select a root object reference and sum up the used memory of every object
that is reachable from that root object. Other resources loaded by the benchmark target are ignored.&lt;/li&gt;
&lt;li&gt;It does not represent the real memory consumption: It excludes memory allocation overhead, GC overhead and non heap data.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The procedure can be useful for a library developer to get quick insights, but is less useful to determine a realistic
value for memory consumption. When the total Java heap is traversed, the result is similar to the used heap space
metric.&lt;/p&gt;
&lt;h2 id=&quot;metric-used-memory-after-forced-gc&quot;&gt;Metric: Used Memory after Forced GC&lt;/h2&gt;
&lt;p&gt;We can ask the JVM for the currently used memory:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    long getCurrentlyUsedMemory() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      return&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed() +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getUsed();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With &lt;em&gt;used memory&lt;/em&gt; we mean heap and non heap data. The non heap data includes memory areas such as
thread stacks and class caches. We will also record the heap only data with a separate metric.&lt;/p&gt;
&lt;p&gt;Since Java relies on garbage collection, the used memory floats between the minimum and maximum value.
Measuring the amount of used memory will yield different results depending on when the last GC run
happened. To get a reliable result about how much memory is used by the application, the logical idea
is to force a GC run and then obtain the value for the used memory. The first try looks like this:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    long getCurrentlyUsedMemory() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      return&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed() +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getUsed();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    long getPossiblyReallyUsedMemory() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      System.gc();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      return getCurrentlyUsedMemory();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The problem with it is that &lt;code&gt;System.gc()&lt;/code&gt; triggers a concurrent garbage collection. When requesting
the usage values immediately after the &lt;code&gt;System.gc()&lt;/code&gt; call the garbage collection is not finished yet.
To wait for the GC run to complete we can use counter values exposed by the JVM (BTW: I found that trick in the
JMH sources):&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  long getGcCount() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    long sum = 0;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    for (GarbageCollectorMXBean b : ManagementFactory.getGarbageCollectorMXBeans()) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      long count = b.getCollectionCount();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      if (count != -1) { sum +=  count; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    return sum;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  long getReallyUsedMemory() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    long before = getGcCount();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    System.gc();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    while (getGcCount() == before);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    return getCurrentlyUsedMemory();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here is a debug example using this technique, with the classic CMS collector:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;gcCount=25&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;36.692: [GC (System.gc()) [PSYoungGen: 855764K-&amp;gt;38944K(1925120K)] 966467K-&amp;gt;149647K(2325504K), 0.0670620 secs] [Times: user=0.27 sys=0.00, real=0.06 secs] &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;36.759: [Full GC (System.gc()) [PSYoungGen: 38944K-&amp;gt;0K(1925120K)] [ParOldGen: 110703K-&amp;gt;111316K(400384K)] 149647K-&amp;gt;111316K(2325504K), [Metaspace: 11212K-&amp;gt;11212K(1058816K)], 0.4352339 secs] [Times: user=1.50 sys=0.00, real=0.44 secs] &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;usedHeap=169860016, usedNonHeap=18995232, totalUsed=188855248, gcCount=27&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can see that actually two GC runs are reported for one call to &lt;code&gt;System.gc()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The result of this metric can be found in the graph below under the name &lt;em&gt;usedMem_fin&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;One result value is the average of 15 benchmark runs. We spawn five JVM instances and do three benchmark runs
inside the same process on the identical cache instance. The memory usage is determined after stopping the workload.
As we can see in the graph below, the confidence interval is slightly bigger and the value slightly higher than in
&lt;em&gt;usedMem_settled&lt;/em&gt;, especially for the CMS collector. Possible reasons for the variance:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;For CMS: The metric update seems to lag behind the GC completion. A forced GC counts two GC runs, in G1 only one.&lt;/li&gt;
&lt;li&gt;The tested benchmark target may have buffered data after the workload finishes. For example Caffeine uses queues
to decouple the eviction from the application threads&lt;/li&gt;
&lt;li&gt;JMH might run other cleanup and recording tasks in parallel&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;An alternative approach to wait for the GC to complete is to check whether a finalizer is called on a freshly allocated
object, see the Stack Overflow question mentioned at the beginning.&lt;/p&gt;
&lt;h2 id=&quot;metric-used-memory-after-forced-gc-and-settling&quot;&gt;Metric: Used Memory after Forced GC and Settling&lt;/h2&gt;
&lt;p&gt;To get rid of the variance and improve the accuracy we use the previous metric but run multiple full GC cycles
until the value stabilises. The code for that looks like this:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    long getSettledUsedMemory() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      long m;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      long m2 = getReallyUsedMemory();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      do {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        Thread.sleep(567)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        m = m2;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        m2 = getUsedMemory();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      } while (m2 &amp;lt; getReallyUsedMemory());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      return m;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The result of this metric can be found in the graph below under the name &lt;em&gt;usedMem_settled&lt;/em&gt;.&lt;/p&gt;
&lt;h2 id=&quot;metric-used-heap-memory&quot;&gt;Metric: Used Heap Memory&lt;/h2&gt;
&lt;p&gt;Various methods exist to obtain a heap dump or heap histogram of live objects (&lt;code&gt;VisualVM&lt;/code&gt;, &lt;code&gt;jmap&lt;/code&gt;, &lt;code&gt;jhat&lt;/code&gt;). This can be
used to determine the space occupied by all objects on the Java heap.&lt;/p&gt;
&lt;p&gt;In our benchmarks we programmatically obtain a heap histogram via the JVM Attach API, specifically
&lt;code&gt;sun.tools.attach.HotSpotVirtualMachine.heapHisto(&amp;quot;-live&amp;quot;)&lt;/code&gt;. After the benchmark run the procedure
waits until the used memory values stabilise, then prints the top objects from the heap dump, extracts
the used size of all live objects and stores it in the JMH results. The code to do this can be found
in the &lt;a href=&quot;https://github.com/cache2k/cache2k-benchmark/blob/e19e24aad1ac23c8a3397c32be5576b759267c98/jmh-suite/src/main/java/org/cache2k/benchmark/jmh/ForcedGcMemoryProfiler.java&quot;&gt;ForcedGcMemoryProfiler&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Advantages:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The histogram gives insight into implementation differences&lt;/li&gt;
&lt;li&gt;Extracting the used heap size via the histogram has no measurement variance&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Disadvantages:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Does not represent the real memory consumption: It excludes memory allocation overhead, GC overhead and non heap data&lt;/li&gt;
&lt;li&gt;The heap histogram cannot be retrieved via a standardised API, results may differ between JVM vendors&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The result of this metric can be found in the graph below under the name &lt;em&gt;usedHeap_settled&lt;/em&gt;.
Using the heap histogram produces values with less variance than using
&lt;code&gt;ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed()&lt;/code&gt; since it reliably counts only live objects.
After settling, with nothing happening in the JVM, the values don’t differ much.&lt;/p&gt;
&lt;h2 id=&quot;metric-maximum-used-memory-via-gc-notification&quot;&gt;Metric: Maximum Used Memory via GC Notification&lt;/h2&gt;
&lt;p&gt;Another way to obtain a metric of the used memory is to register for notifications of the garbage collector activity. The
details are explained in the JavaDoc of &lt;a href=&quot;http://docs.oracle.com/javase/7/docs/jre/api/management/extension/com/sun/management/GarbageCollectionNotificationInfo.html&quot;&gt;GarbageCollectionNotificationInfo&lt;/a&gt;.
In the information we can find the used and total size for all memory pools right after the end of the
garbage collection run.&lt;/p&gt;
&lt;p&gt;The metric we record in our benchmarks is the peak value of the used memory that gets reported during a benchmark run. In the
graphs it can be found under the name &lt;em&gt;usedMem_max&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;We could also use the GC notification to extract the value of the used heap memory. The problem is that the
method enumerates all memory pools and the memory pool names differ by the selected GC type. Code
summing up only the heap pools would be fragile.&lt;/p&gt;
&lt;p&gt;The values of the GC notification correspond to the values of the management beans.&lt;/p&gt;
&lt;h2 id=&quot;showtime-used-memory-graphs&quot;&gt;Showtime: Used Memory Graphs&lt;/h2&gt;
&lt;p&gt;Now it’s finally time for the first graph. Let’s see how the metrics look for the tested libraries:&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks/CMS/ZipfianSequenceLoadingBenchmarkMemoryUsed4-1M-10.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;ZipfianSequenceLoadingBenchmark with CMS collector, 4 threads, 1M cache entries, Zipfian factor 10, used memory.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks/CMS/ZipfianSequenceLoadingBenchmarkMemoryUsed4-1M-10.dat&quot;&gt;ZipfianSequenceLoadingBenchmarkMemoryUsed4-1M-10.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;As we can see &lt;em&gt;usedMem_fin&lt;/em&gt; has low confidence. After one or more additional full GC runs the reported
memory becomes a little smaller with no variance, as we can see in &lt;em&gt;usedMem_settled&lt;/em&gt;. Guava seems to be the most memory efficient,
but looking at the &lt;em&gt;usedMem_max&lt;/em&gt; metric it falls behind Caffeine and cache2k. This means that Guava either needs
more memory during operation or it is less friendly with minor garbage collections. A detail that may be worth
some more investigation.&lt;/p&gt;
&lt;p&gt;Doing the same with the G1 garbage collector (VM option &lt;code&gt;-XX:+UseG1GC&lt;/code&gt;) shows another picture:&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks/G1/ZipfianSequenceLoadingBenchmarkMemoryUsed4-1M-10.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;ZipfianSequenceLoadingBenchmark with G1 collector, 4 threads, 1M cache entries, Zipfian factor 10, used memory.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks/G1/ZipfianSequenceLoadingBenchmarkMemoryUsed4-1M-10.dat&quot;&gt;ZipfianSequenceLoadingBenchmarkMemoryUsed4-1M-10.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;The slight difference between &lt;em&gt;usedMem_fin&lt;/em&gt; and &lt;em&gt;usedMem_settled&lt;/em&gt; is gone. That means G1 is capable of collecting garbage more reliably
on the first full GC run. Looking at the &lt;em&gt;usedMem_max&lt;/em&gt; metric we have an indicator of how the cache
implementations are affected by the different collectors.&lt;/p&gt;
&lt;h2 id=&quot;metric-total-memory&quot;&gt;Metric: Total Memory&lt;/h2&gt;
&lt;p&gt;The used memory after a GC isn’t all the memory that is occupied by the JVM. The JVM reserves more memory to have
“breathing space” for garbage collection. We call this the total memory or committed memory. The total memory
is requested with:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    long getTotalMemory() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      return&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getCommitted() +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getCommitted();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To have a more compact naming we use the term &lt;em&gt;total&lt;/em&gt; instead of &lt;em&gt;committed&lt;/em&gt;. We extract two metrics.
&lt;em&gt;totalMem_settled&lt;/em&gt; is the total memory after the benchmark run and multiple full GCs. &lt;em&gt;totalMem_max&lt;/em&gt; is the
maximum total memory that was reported by the GC notification.&lt;/p&gt;
&lt;h2 id=&quot;metric-process-vmrss-and-vmhwm-reported-by-linux&quot;&gt;Metric: Process VmRSS and VmHWM reported by Linux&lt;/h2&gt;
&lt;p&gt;So far we have only utilised JVM internal metrics. But is this the amount of memory that is really used?
Let’s ask the operating system! Linux provides the metric resident set size (RSS) which is the amount of
physical memory that a process is using. Very conveniently for our benchmarking purposes, Linux provides
another metric called high water mark (“HWM”) which is the peak value of the resident set size.
The metrics &lt;em&gt;VmRSS&lt;/em&gt; and &lt;em&gt;VmHWM&lt;/em&gt; are obtained via the proc interface in &lt;code&gt;/proc/&amp;lt;pid&amp;gt;/status&lt;/code&gt;.
See &lt;code&gt;man proc&lt;/code&gt; for more details. The metric &lt;em&gt;VmHWM&lt;/em&gt; isn’t exposed by the usual Linux tools.&lt;/p&gt;
&lt;h2 id=&quot;showtime-totalcommitted-memory-graphs&quot;&gt;Showtime: Total/Committed Memory Graphs&lt;/h2&gt;
&lt;p&gt;Let’s take a look at the results for the CMS collector:&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks/CMS/ZipfianSequenceLoadingBenchmarkMemory4-1M-10-total.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;ZipfianSequenceLoadingBenchmark with CMS collector, 4 threads, 1M cache entries, Zipfian factor 10, total memory.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks/CMS/ZipfianSequenceLoadingBenchmarkMemory4-1M-10-total.dat&quot;&gt;ZipfianSequenceLoadingBenchmarkMemory4-1M-10-total.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Again we also did a benchmark run with the G1 collector:&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks/G1/ZipfianSequenceLoadingBenchmarkMemory4-1M-10-total.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;ZipfianSequenceLoadingBenchmark with G1 collector, 4 threads, 1M cache entries, Zipfian factor 10, total memory.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks/G1/ZipfianSequenceLoadingBenchmarkMemory4-1M-10-total.dat&quot;&gt;ZipfianSequenceLoadingBenchmarkMemory4-1M-10-total.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;We can see that the total memory reported by the JVM is more than the memory reported by the operating system. The
reason for this is that the RSS metric only reports memory that is really used. The process may request more
memory from the OS and it is only claimed on the first write to that memory section. This technique is
called &lt;a href=&quot;https://en.wikipedia.org/wiki/Memory_overcommitment&quot;&gt;memory overcommitment&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Looking at the values for &lt;em&gt;totalMem_max&lt;/em&gt;, the G1 collector seems to request a high amount of memory which is never used.
We can also see that the G1 collector releases memory back to the OS after a full GC, while CMS does not.&lt;/p&gt;
&lt;h2 id=&quot;metric-allocation-rate&quot;&gt;Metric: Allocation Rate&lt;/h2&gt;
&lt;p&gt;The values for the used memory reported by the JVM after a full GC and the value reported by the OS differ a lot.
To find out a possible cause, we can take a look at the allocation rate. This tells us how many objects (and garbage) a
program is creating.&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks/CMS/ZipfianSequenceLoadingBenchmarkMemory4-1M-10-allocRate.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;ZipfianSequenceLoadingBenchmark with CMS collector, 4 threads, 1M cache entries, Zipfian factor 10, allocation rate.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks/CMS/ZipfianSequenceLoadingBenchmarkMemory4-1M-10-allocRate.dat&quot;&gt;ZipfianSequenceLoadingBenchmarkMemory4-1M-10-allocRate.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;The result with the G1 collector:&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks/G1/ZipfianSequenceLoadingBenchmarkMemory4-1M-10-allocRate.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;ZipfianSequenceLoadingBenchmark with G1 collector, 4 threads, 1M cache entries, Zipfian factor 10, allocation rate.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks/G1/ZipfianSequenceLoadingBenchmarkMemory4-1M-10-allocRate.dat&quot;&gt;ZipfianSequenceLoadingBenchmarkMemory4-1M-10-allocRate.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;However, looking at the absolute values does not make sense, since the throughput is very different. We can normalise the allocation
rate and calculate the allocated bytes per operation:&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks/G1/ZipfianSequenceLoadingBenchmarkMemory4-1M-10-allocRatePerOp.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;ZipfianSequenceLoadingBenchmark with G1 collector, 4 threads, 1M cache entries, Zipfian factor 10, normed allocation rate.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks/G1/ZipfianSequenceLoadingBenchmarkMemory4-1M-10-allocRatePerOp.dat&quot;&gt;ZipfianSequenceLoadingBenchmarkMemory4-1M-10-allocRatePerOp.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;This graph is identical for the CMS and G1 collector.&lt;/p&gt;
&lt;p&gt;The allocation rate is determined by JMH via the &lt;code&gt;GcProfiler&lt;/code&gt; which uses &lt;code&gt;ThreadMXBean.getThreadAllocatedBytes&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;To my knowledge this is the first systematic evaluation of different Java memory consumption metrics
for their usage in benchmarking. We created the metric &lt;em&gt;usedMem_max&lt;/em&gt; and discovered the hidden gem &lt;em&gt;VmHWM&lt;/em&gt; that
proves useful for our purposes.&lt;/p&gt;
&lt;p&gt;The major findings are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The presented metrics can be recorded alongside the benchmarking and have no significant overhead&lt;/li&gt;
&lt;li&gt;More JVM restarts (forks in JMH lingo) increase the accuracy of the total memory consumption, since outliers of
a single GC expansion are less relevant&lt;/li&gt;
&lt;li&gt;With code that has time/memory trade off effects, memory usage needs to be captured as well in the benchmark results.&lt;/li&gt;
&lt;li&gt;The metrics reported by the JVM have different accuracy and different caveats. The reported committed memory
is quite unreliable.&lt;/li&gt;
&lt;li&gt;Linux provides &lt;em&gt;VmHWM&lt;/em&gt;, which gives the peak memory usage. This allows us to reliably capture the highest
memory consumption during a benchmark run.&lt;/li&gt;
&lt;li&gt;Dynamic effects during the execution play an important role in the overall memory consumption&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Highlighting the last point, let’s take a look at the best performers according to &lt;em&gt;usedMem_settled&lt;/em&gt;:&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks/G1/ZipfianSequenceLoadingBenchmarkMemory4-1M-10-usedHeap-sorted.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;ZipfianSequenceLoadingBenchmark with G1 collector, 4 threads, 1M cache entries, Zipfian factor 10, used heap memory, sorted by best performance.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks/G1/ZipfianSequenceLoadingBenchmarkMemory4-1M-10-usedHeap-sorted.dat&quot;&gt;ZipfianSequenceLoadingBenchmarkMemory4-1M-10-usedHeap-sorted.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;In contrast, here are the best performers according to &lt;em&gt;VmHWM&lt;/em&gt;:&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks/G1/ZipfianSequenceLoadingBenchmarkMemory4-1M-10-VmHWM-sorted.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;Whiskers show the 99.9 % confidence interval reported by JMH.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;ZipfianSequenceLoadingBenchmark with G1 collector, 4 threads, 1M cache entries, Zipfian factor 10, peak memory usage reported by the operating system (VmHWM), sorted by best performance.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/the-6-memory-metrics-you-should-track-in-your-java-benchmarks/G1/ZipfianSequenceLoadingBenchmarkMemory4-1M-10-VmHWM-sorted.dat&quot;&gt;ZipfianSequenceLoadingBenchmarkMemory4-1M-10-VmHWM-sorted.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Going back to our initial motivation, we found that &lt;a href=&quot;https://cache2k.org&quot;&gt;cache2k&lt;/a&gt; has superior throughput and consumes
less real memory (according to the &lt;em&gt;VmHWM&lt;/em&gt; metric) than other cache products for the tested benchmark scenario.&lt;/p&gt;
&lt;p&gt;In our benchmark scenario the internal data structures of the cache dominate the used memory.
This is intentional. The cache libraries will not impact the overall memory consumption of real applications
as much as shown here, because the payload data has a bigger size. It must also be noted that the
differences between &lt;em&gt;VmHWM&lt;/em&gt; and &lt;em&gt;usedMem_settled&lt;/em&gt; will look more or less dramatic
when different benchmark parameters are used (thread count, hit rate, etc.).&lt;/p&gt;
&lt;p&gt;Future work: Looking at the results, there are a lot of phenomena that deserve a closer look.
Now that we have the tooling in place, we can use it for more detailed benchmarks.
Tools and benchmarks can also be used for a deeper analysis of the effects
of the G1 garbage collector which will become the default for Java 9. Finally, it’s the plan (or my wish)
to integrate the memory consumption metrics into JMH to make them easily and widely available.
Meaningful benchmarking of code that has memory/time trade offs and relies on garbage collection is an interesting
topic and is worth some more thought. Instead of letting the memory expand unbounded, the benchmark
could force identical memory limits for all benchmark targets.&lt;/p&gt;
&lt;p&gt;Based on the feedback I get, I will update this article and/or provide another article that is more hands-on
and includes the JMH options.&lt;/p&gt;
&lt;h2 id=&quot;addendum-benchmark-setup-description&quot;&gt;Addendum: Benchmark Setup Description&lt;/h2&gt;
&lt;p&gt;For reproduction, here is the setup. The benchmark can be run with:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;git clone https://github.com/cache2k/cache2k-benchmark.git&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;git checkout c9ce981eafb8bce5fd1d719fa618011129732dee&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;cd cache2k-benchmark&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;# compiles the benchmark and produces: jmh-suite/target/benchmarks.jar&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;mvn -DskipTests package&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;# runs the benchmarks, results are written to: /var/run/shm/jmh-result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;bash jmh-run.sh&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;# generate the graphs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;bash processJmhResults.sh --dir /var/run/shm/jmh-result process&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The code to obtain the metrics &lt;em&gt;usedHeap_settled&lt;/em&gt; and &lt;em&gt;usedMem_settled&lt;/em&gt; is in &lt;a href=&quot;https://github.com/cache2k/cache2k-benchmark/blob/e4cd7a8c491bf275545b3003932c2eebb69606e9/jmh-suite/src/main/java/org/cache2k/benchmark/jmh/ForcedGcMemoryProfiler.java&quot;&gt;ForcedGcMemoryProfiler.java&lt;/a&gt;.
The code to determine &lt;em&gt;usedMem_max&lt;/em&gt; is in &lt;a href=&quot;https://github.com/cache2k/cache2k-benchmark/blob/e4cd7a8c491bf275545b3003932c2eebb69606e9/jmh-suite/src/main/java/org/cache2k/benchmark/jmh/GcProfiler.java&quot;&gt;GcProfiler.java&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;benchmark&quot;&gt;Benchmark&lt;/h3&gt;
&lt;p&gt;The benchmark source is in &lt;a href=&quot;https://github.com/cache2k/cache2k-benchmark/blob/e4cd7a8c491bf275545b3003932c2eebb69606e9/jmh-suite/src/main/java/org/cache2k/benchmark/jmh/suite/eviction/symmetrical/ZipfianSequenceLoadingBenchmark.java&quot;&gt;ZipfianSequenceLoadingBenchmark.java&lt;/a&gt;.
The benchmark is selected from a variety of cache benchmarks in the &lt;a href=&quot;https://github.com/cache2k/cache2k-benchmark&quot;&gt;cache2k benchmarks&lt;/a&gt; suite.
The cache is operated in a read through configuration, with a size limit of 1M entries. The
access pattern is a generated Zipfian distribution of 10M different numbers. Zipfian factor 10 means
we generate a number space 10 times the cache size. We will use different factors in the upcoming
benchmarks to facilitate different hit rates. In this setup the hit rate turns out to be between
77% and 82%. A cache miss gets a penalty via JMH’s blackhole: &lt;code&gt;Blackhole.consumeCPU(1000)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;It is important to note that the payload data (keys and values) consists only of integer objects. When storing
bigger objects in the cache the differences in memory consumption between cache implementations would be
less noticeable.&lt;/p&gt;
&lt;p&gt;The benchmark will be described in more detail in a following blog post.&lt;/p&gt;
&lt;h3 id=&quot;environment&quot;&gt;Environment&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;JDK: Oracle Version JDK 1.8.0_121, VM 25.121-b13&lt;/li&gt;
&lt;li&gt;JVM flags: -server -Xmx10G -XX:BiasedLockingStartupDelay=0 (with and without -XX:+UseG1GC)&lt;/li&gt;
&lt;li&gt;JMH Version: 1.8&lt;/li&gt;
&lt;li&gt;CPU: Intel(R) Xeon(R) CPU E3-1240 v5 @ 3.50GHz, 4 physical cores&lt;/li&gt;
&lt;li&gt;CPU L1 cache: 4 x 32 KB 8-way set associative instruction caches, 4 x 32 KB 8-way set associative data caches&lt;/li&gt;
&lt;li&gt;CPU L2 cache: 4 x 256 KB 4-way set associative caches&lt;/li&gt;
&lt;li&gt;CPU L3 cache: 8 MB 16-way set associative shared cache&lt;/li&gt;
&lt;li&gt;RAM: 32GB total, 2x16GB DRR4 at 2133 MHz&lt;/li&gt;
&lt;li&gt;OS: Ubuntu 14.04&lt;/li&gt;
&lt;li&gt;Kernel: 4.4.0-57-generic&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;compared-caches-and-versions&quot;&gt;Compared Caches and Versions&lt;/h3&gt;
&lt;p&gt;We use these “cache” implementations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Google Guava Cache, Version 20&lt;/li&gt;
&lt;li&gt;Caffeine, Version 2.4.0&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cache2k.org&quot;&gt;cache2k&lt;/a&gt;, Version 1.0.0.CR3&lt;/li&gt;
&lt;li&gt;EHCache, Version 2.10.3&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;jmh-setup&quot;&gt;JMH Setup&lt;/h3&gt;
&lt;p&gt;The JMH command line arguments are &lt;code&gt;-gc true -f 5 -wi 2 -w 20s -i 3 -r 20s&lt;/code&gt;, which results in:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Forks: 5 (meaning 5 runs in a newly started JVM)&lt;/li&gt;
&lt;li&gt;Warmup: 2 iterations, 20 s each&lt;/li&gt;
&lt;li&gt;Measurement: 3 iterations, 20 s each&lt;/li&gt;
&lt;li&gt;Before each iteration a forced garbage collection is done&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;threads-and-cores&quot;&gt;Threads and Cores&lt;/h3&gt;
&lt;p&gt;The benchmarks are run with four threads on four available CPU cores. The number of cores is set to four
via the Linux CPU hotplug feature.&lt;/p&gt;
&lt;h3 id=&quot;plots-with-confidence&quot;&gt;Plots With Confidence&lt;/h3&gt;
&lt;p&gt;In the plots, units are in SI (base 10), meaning 1MB is 10^6 bytes. This way the unit prefixes
for operations/s and bytes/s are identical.&lt;/p&gt;
&lt;p&gt;Every bar is plotted with a confidence interval. The interval does not represent
the upper and lower bounds of a measured value, instead it is much more sensitive.
The confidence interval is calculated by JMH with a level of 99.9%.
This means the likelihood that the value is within the shown interval is 99.9%.&lt;/p&gt;
&lt;h2 id=&quot;updates&quot;&gt;Updates&lt;/h2&gt;
&lt;h3 id=&quot;update-from-2017-04-03&quot;&gt;Update from 2017-04-03&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Corrected JMH parameters and description.&lt;/li&gt;
&lt;li&gt;Conclusions: Accuracy and multiple forks&lt;/li&gt;
&lt;li&gt;Allocation rate: How allocation rate is determined&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;callout&quot; data-callout=&quot;note&quot; data-collapsible=&quot;false&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;&lt;div class=&quot;callout-title-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;1em&quot; height=&quot;1em&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;line x1=&quot;18&quot; y1=&quot;2&quot; x2=&quot;22&quot; y2=&quot;6&quot;&gt;&lt;/line&gt;&lt;path d=&quot;M7.5 20.5 19 9l-4-4L3.5 16.5 2 22z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/div&gt;&lt;div class=&quot;callout-title-text&quot;&gt;Note&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;callout-content&quot;&gt;&lt;p&gt;Update 2026: Typo and wording fixes, switch to UK English.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;</content:encoded><category>Java</category><category>benchmark</category><category>JMH</category><category>Memory Consumption</category><category>Cache</category></item><item><title>Java Caching Benchmarks 2016 - Part 2</title><link>https://cruftex.net/java-caching-benchmarks-2016-part-2</link><guid isPermaLink="true">https://cruftex.net/java-caching-benchmarks-2016-part-2</guid><description>The work on improving my old Java Caching Benchmarks continues. This post takes a closer look at the aspect of eviction efficiency.</description><pubDate>Mon, 09 May 2016 15:03:39 GMT</pubDate><content:encoded>&lt;p&gt;The work on improving my old &lt;a href=&quot;http://cache2k.org/benchmarks.html&quot;&gt;Java Caching Benchmarks&lt;/a&gt; continues.
This post takes a closer look at the aspect of eviction efficiency. For this comparison we take Guava and
EHCache2 and compare them to the new kids on the block, Caffeine and &lt;a href=&quot;http://cache2k.org&quot;&gt;cache2k&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;how-not-to-benchmark&quot;&gt;How (not) to benchmark&lt;/h2&gt;
&lt;p&gt;To simulate a real cache scenario an obvious approach would be to construct a benchmark like this:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;    Cache&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;Integer&lt;/span&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt; Integer&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;&amp;gt; &lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#C5E478&quot;&gt;cache&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt; ....&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;    int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;[] &lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#C5E478&quot;&gt;trace&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#C5E478&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1976D2;--shiki-dark:#F78C6C&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;; i &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1976D2;--shiki-dark:#C5E478&quot;&gt; trace&lt;/span&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1976D2;--shiki-light-font-style:inherit;--shiki-dark:#FAF39F;--shiki-dark-font-style:italic&quot;&gt;length&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;; i&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#7FDBCA&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;      Integer&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#C5E478&quot;&gt; value&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1976D2;--shiki-dark:#C5E478&quot;&gt; cache&lt;/span&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;trace[i]&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic&quot;&gt;      if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; (value &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1976D2;--shiki-dark:#82AAFF&quot;&gt; null&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1976D2;--shiki-dark:#C5E478&quot;&gt;        Thread&lt;/span&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt;sleep&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1976D2;--shiki-dark:#F78C6C&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1976D2;--shiki-dark:#C5E478&quot;&gt;        cache&lt;/span&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt;put&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The idea is to have an array of cache keys and request the keys from the cache. The array represents either some random
pattern or real cache operations recorded from an application. If the value is not available
from the cache, it is a cache miss and the value needs to be computed or loaded from a database. To simulate the
miss penalty we sleep for 10 milliseconds.&lt;/p&gt;
&lt;p&gt;Of course, &lt;code&gt;Thread.sleep&lt;/code&gt; is unreliable and will produce jitter, especially in the area of 10 milliseconds.
But that is not the point. Let’s say we had chosen 10 milliseconds because the average latency of
a database access in our infrastructure is 10 milliseconds. But what happens if we don’t query a
local database but an external web service and have 300 milliseconds latency? If we benchmarked
with a higher simulated latency, clearly the throughput of the cache that achieves a better hit rate
would get better, too.&lt;/p&gt;
&lt;p&gt;The performance effect of a cache miss inside the JVM and the local machine can vary per scenario, too.
The value might be computed inside the VM, the data may be retrieved from a local SSD or the request goes
over the network. We cannot simulate or try out all these variations. For our benchmarks
the idea is to isolate the different aspects. In this blog post I will focus on the efficiency of the
eviction algorithm. In the next blog post I will take a deeper look at the runtime overhead of eviction.&lt;/p&gt;
&lt;h3 id=&quot;the-caches&quot;&gt;The Caches&lt;/h3&gt;
&lt;p&gt;In this benchmark run we focus on these cache implementations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Google Guava Cache, Version 19, default settings&lt;/li&gt;
&lt;li&gt;EHCache, Version 2.10.1, default settings&lt;/li&gt;
&lt;li&gt;Caffeine, Version 2.3.0, default settings&lt;/li&gt;
&lt;li&gt;cache2k, Version 0.26-BETA, default settings&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The JVM settings and hardware are irrelevant for this benchmark, since we focus on the efficiency of the eviction algorithm
and not on the runtime performance.&lt;/p&gt;
&lt;p&gt;Caffeine needs two special setup parameters for the benchmark:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;    int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#C5E478&quot;&gt; maxSize&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1976D2;--shiki-dark:#C5E478&quot;&gt;    Caffeine&lt;/span&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt;newBuilder&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;      .&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt;maximumSize&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;maxSize&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;      .&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt;executor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;Runnable&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;run&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;      .&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt;initialCapacity&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;_maxElements&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;      ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With the &lt;code&gt;executor&lt;/code&gt; parameter the eviction happens within the same thread. If this weren’t specified, the
eviction would be delayed and the cache would hold more than the configured maximum size, thus leading
to wrong results. With the &lt;code&gt;initialCapacity&lt;/code&gt; parameter Caffeine is instructed to collect data for the eviction
algorithm right from the beginning. Setting this parameter is a courtesy to Caffeine, because otherwise
Caffeine cannot perform well for very short traces.&lt;/p&gt;
&lt;p&gt;Why are &lt;em&gt;Hazelcast&lt;/em&gt;, &lt;em&gt;Infinispan&lt;/em&gt; or &lt;em&gt;XY-Cache&lt;/em&gt; not included? Because I currently focus on cache implementations
that are optimised for use in the local JVM heap. The benchmark concentrates on the effects inside
a single VM.&lt;/p&gt;
&lt;h3 id=&quot;eviction-algorithms&quot;&gt;Eviction Algorithms&lt;/h3&gt;
&lt;p&gt;Guava and EHCache use LRU. Caffeine uses a recently invented algorithm called Window-TinyLFU.
Cache2k uses an improved and optimised version of the Clock-Pro algorithm.&lt;/p&gt;
&lt;p&gt;Still, LRU (least recently used) is the most used eviction algorithm today. It is simple and easy to
implement and achieves quite useful results. Another algorithm is LFU (least frequently used).
Instead of deciding based on the recency it decides based on the frequency.
Entries that were accessed more often will be kept. Although yielding better performance for some workloads,
LFU is not useful as a universal algorithm.&lt;/p&gt;
&lt;p&gt;On the other hand LRU, not addressing the frequency aspect, is problematic, too. For example
a scan over the whole data set will sweep out entries in the cache that were frequently accessed
before and after.&lt;/p&gt;
&lt;p&gt;Caffeine and cache2k use modern algorithms that address both aspects, the recency and the frequency.
The basic idea is to detect entries accessed more frequently and protect them in a hot set, while
evicting entries only seen once faster than LRU would do.&lt;/p&gt;
&lt;p&gt;Only cache2k uses an algorithm that allows fully concurrent access. To achieve that,
no temporal information about the access is recorded, only an access counter is incremented.&lt;/p&gt;
&lt;p&gt;Going into more detail here is beyond this article. For more information the various papers are
the best source.&lt;/p&gt;
&lt;h2 id=&quot;traces-traces-traces&quot;&gt;Traces, Traces, Traces&lt;/h2&gt;
&lt;p&gt;To compare the cache efficiency we use different access patterns and run them against a cache
implementation with a specific size limit. For this article I used traces from four different sources.&lt;/p&gt;
&lt;p&gt;The traces &lt;em&gt;Cpp&lt;/em&gt;, &lt;em&gt;Glimpse&lt;/em&gt;, &lt;em&gt;Multi2&lt;/em&gt; and &lt;em&gt;Sprite&lt;/em&gt; come from the authors of these
papers:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;J. Kim, J. Choi, J. Kim, S. Noh, S. Min, Y. Cho, and C. Kim,
“A Low-Overhead, High-Performance Unified Buffer Management Scheme
that Exploits Sequential and Looping References”,
&lt;em&gt;4th Symposium on Operating System Design &amp;amp; Implementation, October 2000.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;D. Lee, J. Choi, J. Kim, S. Noh, S. Min, Y. Cho and C. Kim,
“On the Existence of a Spectrum of Policies that Subsumes the Least Recently Used
(LRU) and Least Frequently Used (LFU) Policies”, &lt;em&gt;Proceeding of 1999 ACM
SIGMETRICS Conference, May 1999.&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All these traces are short and have limited practical value today. These traces are used
in a lot of papers about cache eviction algorithms. That is why I always run them and compare the
results to the outcomes of the papers.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;OLTP&lt;/em&gt; trace was used within the ARC paper:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Nimrod Megiddo and Dharmendra S. Modha, “ARC: A Self-Tuning, Low Overhead
Replacement Cache,” USENIX Conference on File and Storage Technologies (FAST 03),
San Francisco, CA, pp. 115-130, March 31-April 2, 2003.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The traces &lt;em&gt;UmassFinancial&lt;/em&gt; and &lt;em&gt;UmassWebsearch&lt;/em&gt; are available from the
&lt;a href=&quot;http://traces.cs.umass.edu/index.php/Storage/Storage&quot;&gt;UMass Trace Repository&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;All traces from the sources above are disk I/O traces, which means all traces contain
a sequence of disk block numbers. The block size used in these traces is usually 512 bytes. But the access
pattern of a Java object cache is different from that of a disk buffer in an operating system. We need to be aware of
this mismatch when comparing Java caches with these traces.&lt;/p&gt;
&lt;p&gt;To fill the gap of missing middleware traces, &lt;em&gt;Web07&lt;/em&gt;, &lt;em&gt;Web12&lt;/em&gt;, &lt;em&gt;OrmAccessBusy&lt;/em&gt; and &lt;em&gt;OrmAccessNight&lt;/em&gt; are
traces from a Java application provided by headissue GmbH. The traces are made public in the
&lt;a href=&quot;http://github.com/cache2k/cache2k-benchmark&quot;&gt;cache2k benchmark&lt;/a&gt;
project on GitHub. As far as I know, these are the only publicly available access traces of a Java application.&lt;/p&gt;
&lt;h2 id=&quot;the-graphs&quot;&gt;The Graphs&lt;/h2&gt;
&lt;p&gt;The graphs show the achieved hit rate for a cache implementation with a specified cache size.
The cache implementations &lt;em&gt;LRU&lt;/em&gt; and &lt;em&gt;CLOCK&lt;/em&gt; are reference implementations from the
&lt;a href=&quot;http://github.com/cache2k/cache2k-benchmark&quot;&gt;cache2k-benchmark&lt;/a&gt; package.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;OPT&lt;/em&gt; is a hypothetical cache implementation that can see into the future and only evicts entries that
will never be used again or whose reuse is farthest away. This is also known as Belady’s algorithm. To calculate OPT
the simulator code of Caffeine is used.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;RAND&lt;/em&gt; is the simplest eviction algorithm possible. It selects the eviction candidate at random. The result
shown is actually not from ‘perfect random’, but from a realistic cache implementation. The evicted entry
is chosen by a forward moving pointer inside the cache hash table.&lt;/p&gt;
&lt;h3 id=&quot;random-pattern&quot;&gt;Random Pattern&lt;/h3&gt;
&lt;p&gt;We start with a completely random pattern of 1000 different values.&lt;/p&gt;
&lt;p&gt;Trace length: 3 million, Unique keys: 1000, Maximum possible hit rate: 99.97%&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-2/traceTotalRandom1000hitrateProducts.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;OPT = optimal replacement (Belady), RAND = random eviction; LRU and CLOCK are reference implementations from cache2k-benchmark.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;Random Pattern hit rates comparison.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-2/traceTotalRandom1000hitrateProducts.dat&quot;&gt;traceTotalRandom1000hitrateProducts.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;The main purpose of this pattern is to find out whether one of the implementations is cheating. Since the pattern
is random, no algorithm has a chance to predict what will be accessed in the future. A cache implementation can
always cheat the benchmark by caching a few more entries than the requested limit. If some implementation
achieves a better hit rate for a random pattern, that means it is caching more entries. As we can see,
the results are almost identical for all implementations. All fine.&lt;/p&gt;
&lt;h3 id=&quot;oltp-trace&quot;&gt;Oltp Trace&lt;/h3&gt;
&lt;p&gt;A trace from an OLTP workload, used by the authors of the ARC algorithm.&lt;/p&gt;
&lt;p&gt;Trace length: 914145, Unique keys: 186880, Maximum possible hit rate: 79.56%&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-2/traceOltphitrateProducts.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;OPT = optimal replacement (Belady), RAND = random eviction; LRU and CLOCK are reference implementations from cache2k-benchmark.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;OLTP Trace hit rates comparison.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-2/traceOltphitrateProducts.dat&quot;&gt;traceOltphitrateProducts.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;umassfinancial2-trace&quot;&gt;UmassFinancial2 Trace&lt;/h3&gt;
&lt;p&gt;A trace from the Umass Trace Repository, described as: “I/O traces from OLTP applications running at two large
financial institutions”. From the original trace only the first 1 million requests are used. The trace contains
I/O requests for contiguous blocks. From each request only the first block address
is used. This should make the trace data more relevant to the access of objects.&lt;/p&gt;
&lt;p&gt;Trace length: 1 million, Unique keys: 102742, Maximum possible hit rate: 89.73%&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-2/traceUmassFinancial2hitrateProducts.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;OPT = optimal replacement (Belady), RAND = random eviction; LRU and CLOCK are reference implementations from cache2k-benchmark.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;UmassFinancial2 Trace hit rates comparison.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-2/traceUmassFinancial2hitrateProducts.dat&quot;&gt;traceUmassFinancial2hitrateProducts.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;umasswebsearch1-trace&quot;&gt;UmassWebSearch1 Trace&lt;/h3&gt;
&lt;p&gt;A trace from the Umass Trace Repository, described as: “I/O traces from a popular search engine”.
From the original trace only the first 1 million requests are used. The trace contains
I/O requests for contiguous blocks. From each request only the first block address
is used. This should make the trace data more relevant to the access of objects.&lt;/p&gt;
&lt;p&gt;The low possible hit rate is quite atypical for this trace.&lt;/p&gt;
&lt;p&gt;Trace length: 1 million, Unique keys: 470248, Maximum possible hit rate: 52.98%&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-2/traceUmassWebSearch1hitrateProducts.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;OPT = optimal replacement (Belady), RAND = random eviction; LRU and CLOCK are reference implementations from cache2k-benchmark.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;UmassWebSearch1 Trace hit rates comparison.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-2/traceUmassWebSearch1hitrateProducts.dat&quot;&gt;traceUmassWebSearch1hitrateProducts.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;ormaccessbusytime-trace&quot;&gt;OrmAccessBusytime Trace&lt;/h3&gt;
&lt;p&gt;This trace represents requests from a Java application to an object relational mapper for unique instances
of entities. The trace was extracted during a busy period at daytime. The requested objects are a
mixture of products and user data. Besides the user activity, some analytical jobs also happen
in short periodic bursts.&lt;/p&gt;
&lt;p&gt;Trace length: 5 million, Unique keys: 76349, Maximum possible hit rate: 85.61%&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-2/traceOrmAccessBusytimehitrateProducts.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;OPT = optimal replacement (Belady), RAND = random eviction; LRU and CLOCK are reference implementations from cache2k-benchmark.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;OrmAccessBusytime Trace hit rates comparison.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-2/traceOrmAccessBusytimehitrateProducts.dat&quot;&gt;traceOrmAccessBusytimehitrateProducts.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;web12-trace&quot;&gt;Web12 Trace&lt;/h3&gt;
&lt;p&gt;The trace represents requests to product detail pages of an e-commerce site in December, the busiest month.
For the trace we just use integer numbers. Each unique URL is represented by one number.&lt;/p&gt;
&lt;p&gt;Trace length: 95607, Unique keys: 13756, Maximum possible hit rate: 85.61%&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-2/traceWeb12hitrateProducts.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;OPT = optimal replacement (Belady), RAND = random eviction; LRU and CLOCK are reference implementations from cache2k-benchmark.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;Web12 Trace hit rates comparison.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-2/traceWeb12hitrateProducts.dat&quot;&gt;traceWeb12hitrateProducts.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;the-zipfian-pattern&quot;&gt;The Zipfian Pattern&lt;/h3&gt;
&lt;p&gt;The Zipfian pattern is a random distribution, but the probability of each value follows Zipf’s law. The result
is a typical long-tail distribution with a head of ‘hot’ values and a tail of ‘cold’ values. This pattern is
used quite often for comparing caches. The pattern generator used is from the YCSB benchmark.&lt;/p&gt;
&lt;p&gt;Trace length: 10 million, Unique keys: 10000, Maximum possible hit rate: 99.9%&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-2/traceZipf10khitrateProducts.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;OPT = optimal replacement (Belady), RAND = random eviction; LRU and CLOCK are reference implementations from cache2k-benchmark.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;Zipfian Pattern hit rates comparison.&lt;span class=&quot;chart-source&quot;&gt; Raw data: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-2/traceZipf10khitrateProducts.dat&quot;&gt;traceZipf10khitrateProducts.dat&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;more-graphs&quot;&gt;More Graphs…&lt;/h3&gt;
&lt;p&gt;The above is only a subset of all traces. The complete set of graphs is available at
&lt;a href=&quot;https://cruftex.net/2016/Java-Caching-Benchmarks-2016-Part-2-graphs&quot;&gt;Java Caching Benchmarks 2016 - Part 2 - The Graphs&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;The new kids on the block, Caffeine and cache2k, perform superior to Guava and EHCache2 when operated
with a useful cache size. Cache2k seems to be ahead for analytical workloads. Other
workloads are in favour of Caffeine (&lt;code&gt;UmassWebSearch1&lt;/code&gt;). I assume that Caffeine keeps hot data longer
than cache2k when the workload shifts.&lt;/p&gt;
&lt;p&gt;In case the cache is too small, the scenario may become ‘LRU-friendly’
as we can see in the &lt;code&gt;OrmAccessBusytime&lt;/code&gt; graph. Caffeine has a weak spot here. For cache sizes below 5000 it
actually performs below random eviction. Cache2k is also affected by this, but stays closely above random,
so at least it sometimes makes a useful decision. After informing Ben Manes, the author of Caffeine, he confirmed
this and is working on an improved version.&lt;/p&gt;
&lt;p&gt;The new eviction algorithms of cache2k and Caffeine have a lot of internal tuning parameters. At the end
of the day, it depends on how these parameters are set and which traces are considered more important to
optimise for. Vice versa, it is always possible to bias a benchmark by selecting traces your favourite
cache looks best with.&lt;/p&gt;
&lt;p&gt;As stated in the last blog post, I am the author of &lt;a href=&quot;http://cache2k.org&quot;&gt;cache2k&lt;/a&gt;. The goal of the benchmarks is
to see how cache2k performs and to be able to detect performance regressions while developing and see in which
areas to improve. Cache2k is optimised towards highly concurrent read accesses, as shown in the last
blog post. Because of that engineering direction, there is less information available for the eviction
algorithm. This is a general disadvantage for achieving the highest eviction efficiency. This comparison shows
that it is well compensated.&lt;/p&gt;
&lt;h2 id=&quot;the-missing-trace&quot;&gt;The Missing Trace&lt;/h2&gt;
&lt;p&gt;For these benchmarks altogether 10 different traces are used. While other benchmarks pick only
two or three traces, this shows a more diverse result and detects weak spots. But still this is not
a useful or representative set of traces. We need more traces hitting a Java cache under
different scenarios.&lt;/p&gt;
&lt;div class=&quot;callout&quot; data-callout=&quot;note&quot; data-collapsible=&quot;false&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;&lt;div class=&quot;callout-title-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;1em&quot; height=&quot;1em&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;line x1=&quot;18&quot; y1=&quot;2&quot; x2=&quot;22&quot; y2=&quot;6&quot;&gt;&lt;/line&gt;&lt;path d=&quot;M7.5 20.5 19 9l-4-4L3.5 16.5 2 22z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/div&gt;&lt;div class=&quot;callout-title-text&quot;&gt;Note&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;callout-content&quot;&gt;&lt;p&gt;Update 2026: Typo and wording fixes, switch to UK English.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;</content:encoded><category>Java</category><category>caching</category><category>cache2k</category><category>benchmark</category></item><item><title>cache2k - the high performance Java caching library is now Apache licensed!</title><link>https://cruftex.net/cache2k-is-now-apache-licensed</link><guid isPermaLink="true">https://cruftex.net/cache2k-is-now-apache-licensed</guid><description>Good news, everyone! After continuously receiving mails and seeing the growing popularity in the Android community I finally decided to switch the licence of cache2k to Apache. Why was it GPL in the first place? Well, read on...</description><pubDate>Thu, 07 Apr 2016 13:20:48 GMT</pubDate><content:encoded>&lt;p&gt;Good news, everyone! After continuously receiving mails and seeing the growing popularity in the Android community
I finally decided to switch the licence of cache2k to Apache. Why was it GPL in the first place? Well, read on…&lt;/p&gt;
&lt;h2 id=&quot;why-gpl-in-the-first-place&quot;&gt;Why GPL in the first place?&lt;/h2&gt;
&lt;p&gt;The GPL licence does not mean that it is required to make the source of any product that uses the library available
to the public, but it is required to make the source available to everyone who gets the bundled product.&lt;/p&gt;
&lt;p&gt;One of the key business principles at &lt;a href=&quot;https://headissue.com&quot;&gt;headissue&lt;/a&gt; is to ship the source code to our customers.
We think that it is important to convince with actions rather than lock in the customer to our business by
keeping the source for ourselves. Because of that principle, the GPL is essentially a perfect fit for us. Another factor
is that we concentrate on server applications, so the code never leaves a customer’s server.&lt;/p&gt;
&lt;p&gt;So, the general thinking was: Why should we share something with fewer restrictions than we need for
our own business from other products’ licences? Furthermore, I admit it, sticking to GPL gives more potential
for other business models such as dual licensing.&lt;/p&gt;
&lt;h2 id=&quot;why-apache-now&quot;&gt;Why Apache now?&lt;/h2&gt;
&lt;p&gt;It’s an adjustment to the facts. Interestingly, cache2k got popular in the Android community and people are using
it to build highly efficient smartphone applications. Due to the GPL rules every application distributed with
cache2k would need to be licensed under the GPL as well and the source code needs to be delivered alongside.
For Android developers, the GPL has a much more restrictive meaning than for server side developers, since,
instead of shipping (distributing in licence lingo) to a single server you put your code in the app store and
make it publicly available. Distributing your binary to everyone also means you have to make the source
code available to everyone. Did this happen for the applications using cache2k? No, it did not. So now
I have two options: Sue everyone who is using cache2k and not obeying the licence or face the facts.
Well, think about it, should I sue everyone who just put trust in a new caching library and gave valuable feedback?!&lt;/p&gt;
&lt;p&gt;It is not my intention to blame people using cache2k for not adhering to the licensing details. Instead,
I meanwhile really believe that if I market something as a library usable for Android and as open source, people
expect that they can use it without restrictions.&lt;/p&gt;
&lt;p&gt;It’s the Apache License now, as an adaptation to reality.&lt;/p&gt;
&lt;h2 id=&quot;about-cache2k&quot;&gt;About cache2k&lt;/h2&gt;
&lt;p&gt;Cache2k is a high performance Java caching library which concentrates on caching inside the JVM. The
&lt;a href=&quot;http://cache2k.org/benchmarks.html&quot;&gt;benchmarks&lt;/a&gt; show that it is the fastest available caching library for Java.
We are currently restructuring the API, improving the documentation and working towards a 1.0 version.&lt;/p&gt;
&lt;div class=&quot;callout&quot; data-callout=&quot;note&quot; data-collapsible=&quot;false&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;&lt;div class=&quot;callout-title-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;1em&quot; height=&quot;1em&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;line x1=&quot;18&quot; y1=&quot;2&quot; x2=&quot;22&quot; y2=&quot;6&quot;&gt;&lt;/line&gt;&lt;path d=&quot;M7.5 20.5 19 9l-4-4L3.5 16.5 2 22z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/div&gt;&lt;div class=&quot;callout-title-text&quot;&gt;Note&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;callout-content&quot;&gt;&lt;p&gt;Update 2026: Typo and wording fixes, switch to UK English.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;</content:encoded><category>Java</category><category>caching</category><category>cache2k</category></item><item><title>Java Caching Benchmarks 2016 - Part 1</title><link>https://cruftex.net/java-caching-benchmarks-2016-part-1</link><guid isPermaLink="true">https://cruftex.net/java-caching-benchmarks-2016-part-1</guid><description>Looking around, my benchmarks comparing several Java caches, like Guava, EHCache, Infinispan and cache2k are still the most comprehensive ones you can find focusing on Java heap only cache performance.</description><pubDate>Wed, 16 Mar 2016 15:26:16 GMT</pubDate><content:encoded>&lt;p&gt;Looking around, my &lt;a href=&quot;http://cache2k.org/benchmarks.html&quot;&gt;benchmarks&lt;/a&gt; comparing several Java caches,
like Guava, EHCache, Infinispan and &lt;a href=&quot;http://cache2k.org&quot;&gt;cache2k&lt;/a&gt; are still the most comprehensive ones
you can find focusing on Java heap only cache performance. But it’s two years since I published them.
There are new products and better ways to do benchmarks now. So, it’s time for an update!&lt;/p&gt;
&lt;p&gt;This post is about some fundamental topics and starts with a first set of benchmarks that compare
fast in heap caches with Java’s &lt;code&gt;ConcurrentHashMap&lt;/code&gt;. Why this? Well, read on…&lt;/p&gt;
&lt;h2 id=&quot;the-flaw&quot;&gt;The Flaw&lt;/h2&gt;
&lt;p&gt;Of course, there will never be a benchmark without any flaws. But my previous benchmarks have a major
flaw: Most of them are only single threaded. This has two effects:
The real world throughput of concurrent applications may be different. Furthermore,
implementations can “cheat” the benchmark by utilising more threads to do more work in less time.&lt;/p&gt;
&lt;p&gt;A second thing: The benchmark is only using integer keys and values. Although this is a flaw, too, for now
we continue with this. Testing with different types will be more interesting in case serialisation comes into play.
We will start looking into this when starting benchmarks for off heap or persistence features.&lt;/p&gt;
&lt;h2 id=&quot;switching-to-jmh&quot;&gt;Switching to JMH&lt;/h2&gt;
&lt;p&gt;JMH has become the de facto standard to do (micro) benchmarks on the JVM. The previous benchmarks had loops
to repeat the operations, let’s say, 100000 times. With JMH, that is all gone. We can just say: ‘Dear JMH, measure
the throughput in operations per second and use a benchmark duration of 10 seconds.’
JMH also makes sure that the operations are not just optimised away by the Java compiler, which is a typical source of error.&lt;/p&gt;
&lt;h2 id=&quot;comparing-apples-and-bananas&quot;&gt;Comparing Apples and Bananas&lt;/h2&gt;
&lt;p&gt;But now, back to the topic, the benchmarks themselves. For the first set of benchmarks I decided to do only
scenarios without the need for eviction. That means the whole key space of the benchmark always fits
into the cache. What? “No eviction, but isn’t that the essence of caching?”, you might say.
That is true, but for the first batch of benchmarks I would like to be able to compare against the performance of
&lt;code&gt;ConcurrentHashMap&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Why compare against the performance of a map? First of all, the performance of &lt;code&gt;ConcurrentHashMap&lt;/code&gt; is a
benchmark in the real sense of the word. Smart guys, knowing lots about concurrency, the JVM and
modern computer architecture, spent some effort to get it fast. So why not
learn from it by comparing against it? The second reason is typical software engineer thinking:
“Do I need a full fledged cache or can I go faster and just use a &lt;code&gt;HashMap&lt;/code&gt; or a &lt;code&gt;ConcurrentHashMap&lt;/code&gt;?”&lt;/p&gt;
&lt;p&gt;So, it is not like comparing apples and bananas, but like comparing a Swiss army knife with a knife.
For some scenarios the simple knife will do the job. Sometimes you need a few features that you get
from a Swiss army knife, but you fear it is too heavy. Knowing the performance difference is
critical information.&lt;/p&gt;
&lt;h2 id=&quot;test-setup&quot;&gt;Test Setup&lt;/h2&gt;
&lt;p&gt;For reproduction here is the setup. For the moment I just use my notebook for benchmarking. The hardware
may change in the future to a more stable and server grade setup.&lt;/p&gt;
&lt;p&gt;All benchmarks run with integers as keys and values.&lt;/p&gt;
&lt;h3 id=&quot;compared-caches-and-versions&quot;&gt;Compared Caches and Versions&lt;/h3&gt;
&lt;p&gt;For our first benchmark run we use these “cache” implementations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Google Guava Cache, Version 19&lt;/li&gt;
&lt;li&gt;Caffeine, Version 2.1&lt;/li&gt;
&lt;li&gt;Cache2k, Version 0.23&lt;/li&gt;
&lt;li&gt;ConcurrentHashMap (CHM) from JDK 1.8.0_45&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;EHCache, which is a very popular Java (in heap) cache, is missing at the moment. We will add it in a later update.&lt;/p&gt;
&lt;h3 id=&quot;environment&quot;&gt;Environment&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;JDK 1.8.0_45&lt;/li&gt;
&lt;li&gt;JMH 1.11.3&lt;/li&gt;
&lt;li&gt;JVM flags: -server -Xmx2G&lt;/li&gt;
&lt;li&gt;Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz, 1x4GB, 1x16GB @ DDR3 1600MHz&lt;/li&gt;
&lt;li&gt;2 cores with hyperthreading enabled&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Note: The asymmetrical RAM configuration of the test machine is not desirable for benchmarking,
we need to use other hardware in the future.&lt;/p&gt;
&lt;h3 id=&quot;jmh-configuration&quot;&gt;JMH Configuration&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Warmup iterations: 5&lt;/li&gt;
&lt;li&gt;Measure iterations: 5&lt;/li&gt;
&lt;li&gt;Max iteration time: 20 seconds&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;benchmark-1-populate-parallel-once&quot;&gt;Benchmark 1: Populate Parallel Once&lt;/h2&gt;
&lt;p&gt;The first benchmark is about inserting entries into the cache. The run variants are: The number of entries inserted and
the number of parallel threads used for insertion. The run mode is “one shot”, which means each run is doing the
task once. The result is the runtime in seconds.&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-1/populateParallelOnce-1.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;CHM = ConcurrentHashMap, no eviction, as reference.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;Populate Parallel Once benchmark result for one thread.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-1/populateParallelOnce-2.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;CHM = ConcurrentHashMap, no eviction, as reference.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;Populate Parallel Once benchmark result for two threads.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-1/populateParallelOnce-4.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;CHM = ConcurrentHashMap, no eviction, as reference.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;Populate Parallel Once benchmark result for four threads.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;reference-information&quot;&gt;Reference information&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Benchmark source: &lt;a href=&quot;https://github.com/cache2k/cache2k-benchmark/blob/master/jmh-suite/src/main/java/org/cache2k/benchmark/jmh/suite/noEviction/symmetrical/PopulateParallelOnceBenchmark.java&quot;&gt;PopulateParallelOnceBenchmark.java&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Raw data (ASCII text table): &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-1/populateParallelOnce-1.dat&quot;&gt;populateParallelOnce-1.dat&lt;/a&gt;, &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-1/populateParallelOnce-2.dat&quot;&gt;populateParallelOnce-2.dat&lt;/a&gt;, &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-1/populateParallelOnce-4.dat&quot;&gt;populateParallelOnce-4.dat&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;notes&quot;&gt;Notes&lt;/h3&gt;
&lt;p&gt;For the caches, cache2k does best on insert with a single thread. That is a little surprising, since Caffeine actually
utilises more threads internally to do its work, while cache2k does not. For two threads the runtime of cache2k doubles,
because there is lock contention and I did nothing to optimise for this scenario, but cache2k still performs better
than Guava and only a little worse than Caffeine. Only Caffeine profits a little from the additional thread.&lt;/p&gt;
&lt;p&gt;The four thread result is missing for Caffeine, since there was a VM crash with an out of memory problem on
one test run.&lt;/p&gt;
&lt;p&gt;Generally the four thread result isn’t much different from the two thread result, since it is only a 2 core machine.
I expected a far better result for the hyperthreaded cores.&lt;/p&gt;
&lt;h3 id=&quot;methodology-critique&quot;&gt;Methodology Critique&lt;/h3&gt;
&lt;p&gt;The idea of the benchmark is to get an isolated performance value for inserting entries into a cache. JMH does
multiple iterations and starts with some warmup iterations before doing the iterations that count for the result.
After each iteration the cache content is disposed and a new cache is created.
That means the GC of the previous cache content will influence the performance result. For better isolation we should
force the GC outside the measured time interval.&lt;/p&gt;
&lt;h2 id=&quot;benchmark-2-memory-consumption&quot;&gt;Benchmark 2: Memory Consumption&lt;/h2&gt;
&lt;p&gt;We use the benchmark above and measure the consumed Java heap space. After each benchmark iteration, a garbage collection
is done and the used memory in the heap is recorded.&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-1/populateParallelOnce-memory.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;CHM = ConcurrentHashMap, no eviction, as reference.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;Heap memory consumption after inserting.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;reference-information-1&quot;&gt;Reference information&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Raw data (ASCII text table): &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-1/populateParallelOnce-memory.dat&quot;&gt;populateParallelOnce-memory.dat&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;notes-1&quot;&gt;Notes&lt;/h3&gt;
&lt;p&gt;cache2k and Guava have the lowest measured memory consumption. Of course CHM has a lower memory consumption because
it is not a real cache and doesn’t need the extra overhead for it. Please keep in mind that this is the
memory consumption for this specific benchmark only, you cannot say anything general yet. Things will look different
if expiry is enabled or after some eviction took place.&lt;/p&gt;
&lt;h2 id=&quot;benchmark-3-read-only&quot;&gt;Benchmark 3: Read Only&lt;/h2&gt;
&lt;p&gt;This benchmark populates the cache with 100k entries. The workload is accessing entries in a random pattern
with different hit rates. The main goal is to check how different hit ratios influence the
throughput.&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-1/readOnly.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;CHM = ConcurrentHashMap, no eviction, as reference.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;Read Only benchmark result for 1, 2 and 4 threads and 100, 50 and 33 percent hit ratio.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;reference-information-2&quot;&gt;Reference information&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Benchmark source: &lt;a href=&quot;https://github.com/cache2k/cache2k-benchmark/blob/master/jmh-suite/src/main/java/org/cache2k/benchmark/jmh/suite/noEviction/symmetrical/ReadOnlyBenchmark.java&quot;&gt;ReadOnlyBenchmark.java&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Raw data (ASCII text table): &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-1/readOnly.dat&quot;&gt;readOnly.dat&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;notes-2&quot;&gt;Notes&lt;/h3&gt;
&lt;p&gt;Honestly, this is a good example of how not to do charts, since I squeezed three different aspects into it: Products,
threads and the hit ratio.&lt;/p&gt;
&lt;p&gt;Of course, CHM does best. For the caches, the single thread performance of cache2k is superior. For two threads Caffeine
is better in case the hit rate is 50% or 33%. For all thread counts the cache2k throughput is better than half of CHM’s
throughput for 100% hit rate.&lt;/p&gt;
&lt;h2 id=&quot;benchmark-4-combined-readwrite&quot;&gt;Benchmark 4: Combined Read/Write&lt;/h2&gt;
&lt;p&gt;Three benchmarks: “ro” doing reads only in 8 threads, “rw” doing reads and writes in 6 and 2 threads, “wo” doing writes
in 8 threads. The cache is populated in advance with the test data set. No eviction and no inserts happen during the
benchmark time. The test data size is 11k, the cache size 32k. This benchmark is almost identical to the one in Caffeine.&lt;/p&gt;
&lt;figure class=&quot;chart-figure&quot;&gt;&lt;div class=&quot;chart-card&quot;&gt;&lt;div class=&quot;chart-scroll&quot;&gt;&lt;img src=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-1/combinedReadWrite.chart.svg&quot; alt=&quot;chart&quot;&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p class=&quot;chart-sub&quot;&gt;CHM = ConcurrentHashMap, no eviction, as reference.&lt;/p&gt;&lt;/div&gt;&lt;figcaption&gt;&lt;span class=&quot;fignum&quot;&gt;&lt;/span&gt;Combined Read/Write.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;reference-information-3&quot;&gt;Reference information&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Benchmark source: &lt;a href=&quot;https://github.com/cache2k/cache2k-benchmark/blob/master/jmh-suite/src/main/java/org/cache2k/benchmark/jmh/suite/noEviction/asymmetrical/CombinedReadWriteBenchmark.java&quot;&gt;CombinedReadWriteBenchmark.java&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Raw data (ASCII text table): &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-1/combinedReadWrite.dat&quot;&gt;combinedReadWrite.dat&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;notes-3&quot;&gt;Notes&lt;/h3&gt;
&lt;p&gt;For read only and mostly read scenarios cache2k has the best performance. There is a big drop for concurrent writes
to cache2k.&lt;/p&gt;
&lt;h2 id=&quot;how-to-reproduce&quot;&gt;How to Reproduce&lt;/h2&gt;
&lt;p&gt;If you want to reproduce the values or do your own benchmarks on your favourite server hardware: Do it!
You’ll find the current stuff at the &lt;a href=&quot;https://github.com/cache2k/cache2k-benchmark&quot;&gt;cache2k-benchmark GitHub page&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;git clone https://github.com/headissue/cache2k-benchmark.git&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;git checkout 27634762258f2d4dd2abca81f5807ec9ecd73239&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;cd cache2k-benchmark&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;mvn -DskipTests package&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;# this runs the benchmark suite (takes at least 6 hours, for only a quick check use --quick)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;bash jmh-run.sh&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;# plot the graphs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;bash processJmhResult.sh process&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The results will be written to: &lt;code&gt;target/jmh-result&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The raw JSON data of this benchmark is here: &lt;a href=&quot;https://cruftex.net/java-caching-benchmarks-2016-part-1/data.json.gz&quot;&gt;data.json.gz&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;lessons-learned&quot;&gt;Lessons Learned&lt;/h2&gt;
&lt;p&gt;Some final thoughts on what we already learned from the first batch of benchmarks.&lt;/p&gt;
&lt;h3 id=&quot;simple-benchmarks-but-some-interesting-results&quot;&gt;Simple Benchmarks, but some Interesting Results&lt;/h3&gt;
&lt;p&gt;The benchmarks are not sufficient to get an idea of how real world applications will behave. But concentrating only
on these few benchmarks led to some interesting insights. Learned: Concentrate on a few things, do it correctly and
be surprised what you find with a “little” effort.&lt;/p&gt;
&lt;h3 id=&quot;lost-in-variations&quot;&gt;Lost in Variations&lt;/h3&gt;
&lt;p&gt;With all the variables we have now (cache vendors, cache sizes and threads) it is a total of 119 benchmark runs.
Doing 5 warmup and 5 measurement iterations of 20 seconds each leads to a total runtime of 6 hours. Well, but this is
only the beginning of some thorough cache benchmarks. We want to check more cache implementations, more benchmark
scenarios, different VMs and VM parameters.&lt;/p&gt;
&lt;p&gt;So we need to be very careful about what we want to benchmark. We will probably end up with benchmark times of several
days.&lt;/p&gt;
&lt;h3 id=&quot;dont-do-benchmarks-on-your-notebook&quot;&gt;Don’t do Benchmarks on Your Notebook&lt;/h3&gt;
&lt;p&gt;For a serious evaluation of how well a cache behaves in multi threaded contexts we need more cores. For stable
benchmark hardware we should have at least 4 cores. Doing benchmarks about which
cache will work better with hyperthreading is not really a worthy goal.&lt;/p&gt;
&lt;h3 id=&quot;threads-cores-and-cpu-utilisation&quot;&gt;Threads, Cores and CPU Utilisation&lt;/h3&gt;
&lt;p&gt;I am still not happy with the measurement of different thread counts. Doing the benchmarks with different threads
gives us an idea of how well a cache behaves for concurrent requests. But this can be misinterpreted. Since a cache
may use additional threads to do its work, benchmarking with one thread doesn’t mean that only a single CPU core
was utilised.&lt;/p&gt;
&lt;p&gt;If we expect to know from a benchmark how fast something will be for a fixed set of resources, the benchmarks with
varying threads are all wrong.&lt;/p&gt;
&lt;h3 id=&quot;cache2k-does-well&quot;&gt;cache2k does well&lt;/h3&gt;
&lt;p&gt;Since I am the author of cache2k and the benchmarks, this won’t be a surprise for the reader. But it is a surprise
to me. I didn’t select nice benchmarks for cache2k on purpose, nor did I tune cache2k for these benchmarks.&lt;/p&gt;
&lt;h2 id=&quot;next-on-this-channel&quot;&gt;Next on this Channel&lt;/h2&gt;
&lt;p&gt;Stand by for some more updates in the next weeks, which will feature:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Benchmarks with a new cache2k version&lt;/li&gt;
&lt;li&gt;Benchmarks with eviction&lt;/li&gt;
&lt;li&gt;Single threaded benchmarks with different hash table implementations&lt;/li&gt;
&lt;li&gt;Use of some reasonable and stable hardware&lt;/li&gt;
&lt;li&gt;A look at memory and thread resources&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;join-the-fun&quot;&gt;Join the Fun!&lt;/h2&gt;
&lt;p&gt;My own primary target is to see where cache2k can improve. But, OTOH, the benchmarks are useful for
other caching vendors and caching users, too.&lt;/p&gt;
&lt;p&gt;If you would like to contribute, you are welcome! There are a lot of tiny things that can be done:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Add more cache vendors&lt;/li&gt;
&lt;li&gt;Add JSR107 based benchmark(s)&lt;/li&gt;
&lt;li&gt;Add more benchmark scenarios&lt;/li&gt;
&lt;li&gt;Make some nicer charts with D3?!&lt;/li&gt;
&lt;li&gt;Provide CPU time on recent multi processor hardware&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Before starting work on a topic, please open and/or check the issue tracker at
&lt;a href=&quot;https://github.com/cache2k/cache2k-benchmark&quot;&gt;cache2k-benchmark&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&quot;callout&quot; data-callout=&quot;note&quot; data-collapsible=&quot;false&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;&lt;div class=&quot;callout-title-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;1em&quot; height=&quot;1em&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;line x1=&quot;18&quot; y1=&quot;2&quot; x2=&quot;22&quot; y2=&quot;6&quot;&gt;&lt;/line&gt;&lt;path d=&quot;M7.5 20.5 19 9l-4-4L3.5 16.5 2 22z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/div&gt;&lt;div class=&quot;callout-title-text&quot;&gt;Note&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;callout-content&quot;&gt;&lt;p&gt;Update 2026: Typo and wording fixes, switch to UK English.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;</content:encoded><category>Java</category><category>caching</category><category>cache2k</category><category>benchmark</category><category>JMH</category></item><item><title>Jenkins: How to Analyse Your Plugin Usage</title><link>https://cruftex.net/jenkins-analyze-your-plugin-usage</link><guid isPermaLink="true">https://cruftex.net/jenkins-analyze-your-plugin-usage</guid><description>Ever wondered whether plugins in your Jenkins installation are really used at all? Or do you need to know which plugins you need to install to take over some existing jobs from another Jenkins instance? Here is how you can find out about …</description><pubDate>Mon, 30 Nov 2015 22:51:59 GMT</pubDate><content:encoded>&lt;p&gt;Ever wondered whether plugins in your Jenkins installation are really used at all? Or do you need to know which
plugins you need to install to take over some existing jobs from another Jenkins instance? Here is how you can find
out about your plugin usage and a tiny shell script to analyse your Jenkins configuration and get an index of
all the needed plugins.&lt;/p&gt;
&lt;h2 id=&quot;the-xml-configuration-files&quot;&gt;The XML Configuration Files&lt;/h2&gt;
&lt;p&gt;Jenkins stores all its configuration in a bunch of XML files in the Jenkins home directory. The layout of the
directory is described in &lt;a href=&quot;https://wiki.jenkins-ci.org/display/JENKINS/Administering+Jenkins&quot;&gt;jenkins-ci: Administering Jenkins&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Here is an example of a job configuration file that contains a system Groovy script:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;xml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;xml&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic&quot;&gt; version&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#ECC48D&quot;&gt;1.0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic&quot;&gt; encoding&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#ECC48D&quot;&gt;UTF-8&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;project&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;  ... stripped standard job configuration parameters ... &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;  &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;builders&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;hudson.plugins.groovy.SystemGroovy&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic&quot;&gt; plugin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#ECC48D&quot;&gt;groovy@1.27&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;      &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;scriptSource&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#ECC48D&quot;&gt;hudson.plugins.groovy.StringScriptSource&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;command&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; ... groovy code left out ... &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;        &amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;command&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;      &amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;scriptSource&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;hudson.plugins.groovy.SystemGroovy&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;  &amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;builders&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;project&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The interesting part is the &lt;code&gt;plugin=&amp;quot;groovy@1.27&amp;quot;&lt;/code&gt; attribute, which contains the plugin name and its version.
This information is used by Jenkins to warn about missing plugins and to migrate older configuration versions
to new formats.&lt;/p&gt;
&lt;p&gt;So why not parse the XML files and extract the information?&lt;/p&gt;
&lt;h2 id=&quot;list-configuration-files-with-plugin-usage-information&quot;&gt;List Configuration Files with Plugin Usage Information&lt;/h2&gt;
&lt;p&gt;For extracting the information from the XML files we use the shell tools &lt;code&gt;xml2&lt;/code&gt; and &lt;code&gt;awk&lt;/code&gt;. The &lt;code&gt;xml2&lt;/code&gt; utility
converts an XML file to a simple line by line representation so you can parse it with standard Unix tools like &lt;code&gt;grep&lt;/code&gt;
and &lt;code&gt;awk&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;xml2&lt;/code&gt; output of the above file is:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;/project=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;/project=  ... stripped standard job configuration parameters ... &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;/project=  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;/project/builders/hudson.plugins.groovy.SystemGroovy/@plugin=groovy@1.27&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;/project/builders/hudson.plugins.groovy.SystemGroovy/scriptSource/@class=hudson.plugins.groovy.StringScriptSource&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;/project/builders/hudson.plugins.groovy.SystemGroovy/scriptSource/command= ... groovy code left out ... &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;/project/builders/hudson.plugins.groovy.SystemGroovy/scriptSource/command= &lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next we use &lt;code&gt;awk&lt;/code&gt; to extract only the plugin information:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;$ cat config.xml | xml2 | awk -F= &amp;#39;$1 ~ /^.*@plugin/ { print $2; }&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;groovy@1.27&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let’s scan through all XML files of a Jenkins installation and extract the plugin information.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic&quot;&gt;cd&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#C5E478&quot;&gt; $JENKINS_HOME&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2B5581;--shiki-dark:#ECC48D&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2B5581;--shiki-dark:#82AAFF&quot;&gt; -maxdepth&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1976D2;--shiki-dark:#F78C6C&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2B5581;--shiki-dark:#82AAFF&quot;&gt; -type&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2B5581;--shiki-dark:#ECC48D&quot;&gt; f&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2B5581;--shiki-dark:#82AAFF&quot;&gt; -name&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#F78C6C&quot;&gt; \*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2B5581;--shiki-dark:#ECC48D&quot;&gt;.xml&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2B5581;--shiki-dark:#82AAFF&quot;&gt; -printf&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#ECC48D&quot;&gt;%P\n&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#7FDBCA&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic&quot;&gt; while&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic&quot;&gt; read&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2B5581;--shiki-dark:#ECC48D&quot;&gt; I&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;; &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic&quot;&gt;do&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt;    xml2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#7FDBCA&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#C5E478&quot;&gt;$I&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#7FDBCA&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt; awk&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2B5581;--shiki-dark:#82AAFF&quot;&gt; -vf=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#C5E478&quot;&gt;$I&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2B5581;--shiki-dark:#82AAFF&quot;&gt; -F=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#ECC48D&quot;&gt;$1 ~ /^.*@plugin/ { print f&amp;quot;^&amp;quot;$2; }&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#7FDBCA&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt; sort&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#7FDBCA&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt; uniq&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic&quot;&gt;  done&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#7FDBCA&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt; sort&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#7FDBCA&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt; awk&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2B5581;--shiki-dark:#82AAFF&quot;&gt; -F^&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#ECC48D&quot;&gt;$1 != header { print &amp;quot;## &amp;quot;$1; header=$1; } { print &amp;quot;- &amp;quot;$2; }&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This tiny script produces Markdown formatted output with the configuration file as headline and a list of the used
plugins. Example output:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;markdown&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-weight:bold;--shiki-dark:#82B1FF;--shiki-dark-font-weight:inherit&quot;&gt;## cloudbees-disk-usage-simple.xml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; cloudbees-disk-usage-simple@0.5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-weight:bold;--shiki-dark:#82B1FF;--shiki-dark-font-weight:inherit&quot;&gt;## credentials.xml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; credentials@1.24&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; ssh-credentials@1.11&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-weight:bold;--shiki-dark:#82B1FF;--shiki-dark-font-weight:inherit&quot;&gt;## hudson.maven.MavenModuleSet.xml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; maven-plugin@2.12.1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-weight:bold;--shiki-dark:#82B1FF;--shiki-dark-font-weight:inherit&quot;&gt;## hudson.plugins.analysis.core.GlobalSettings.xml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; analysis-core@1.74&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-weight:bold;--shiki-dark:#82B1FF;--shiki-dark-font-weight:inherit&quot;&gt;## hudson.plugins.copyartifact.TriggeredBuildSelector.xml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; copyartifact@1.37&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;. . .&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;plugin-usage-index&quot;&gt;Plugin Usage Index&lt;/h2&gt;
&lt;p&gt;The above result is not too bad. However, we want to know which plugin is used by which jobs. So let’s reverse the
index. The script is actually almost the same, we just need to twist the list output of the inner &lt;code&gt;awk&lt;/code&gt; script:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic&quot;&gt; cd&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#C5E478&quot;&gt; $JENKINS_HOME&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2B5581;--shiki-dark:#ECC48D&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2B5581;--shiki-dark:#82AAFF&quot;&gt; -maxdepth&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1976D2;--shiki-dark:#F78C6C&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2B5581;--shiki-dark:#82AAFF&quot;&gt; -type&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2B5581;--shiki-dark:#ECC48D&quot;&gt; f&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2B5581;--shiki-dark:#82AAFF&quot;&gt; -name&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#F78C6C&quot;&gt; \*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2B5581;--shiki-dark:#ECC48D&quot;&gt;.xml&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2B5581;--shiki-dark:#82AAFF&quot;&gt; -printf&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#ECC48D&quot;&gt;%P\n&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#7FDBCA&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic&quot;&gt; while&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic&quot;&gt; read&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2B5581;--shiki-dark:#ECC48D&quot;&gt; I&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;; &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic&quot;&gt;do&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt;    xml2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#7FDBCA&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#C5E478&quot;&gt;$I&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#7FDBCA&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt; awk&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2B5581;--shiki-dark:#82AAFF&quot;&gt; -vf=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#C5E478&quot;&gt;$I&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2B5581;--shiki-dark:#82AAFF&quot;&gt; -F=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#ECC48D&quot;&gt;$1 ~ /^.*@plugin/ { print $2&amp;quot;^&amp;quot;f; }&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#7FDBCA&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt; sort&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#7FDBCA&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt; uniq&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic&quot;&gt;  done&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#7FDBCA&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt; sort&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#7FDBCA&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt; awk&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2B5581;--shiki-dark:#82AAFF&quot;&gt; -F^&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#ECC48D&quot;&gt;$1 != header { print &amp;quot;## &amp;quot;$1; header=$1; } { print &amp;quot;- &amp;quot;$2; }&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#D9F5DD&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The result:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;markdown&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-weight:bold;--shiki-dark:#82B1FF;--shiki-dark-font-weight:inherit&quot;&gt;## analysis-collector@1.45&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; jobs/build=cache2k/config.xml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-weight:bold;--shiki-dark:#82B1FF;--shiki-dark-font-weight:inherit&quot;&gt;## analysis-core@1.74&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; hudson.plugins.analysis.core.GlobalSettings.xml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; jobs/build=cache2k/config.xml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-weight:bold;--shiki-dark:#82B1FF;--shiki-dark-font-weight:inherit&quot;&gt;## ant@1.2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; hudson.tasks.Ant.xml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-weight:bold;--shiki-dark:#82B1FF;--shiki-dark-font-weight:inherit&quot;&gt;## cloudbees-disk-usage-simple@0.5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; cloudbees-disk-usage-simple.xml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-weight:bold;--shiki-dark:#82B1FF;--shiki-dark-font-weight:inherit&quot;&gt;## copyartifact@1.37&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; hudson.plugins.copyartifact.TriggeredBuildSelector.xml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-weight:bold;--shiki-dark:#82B1FF;--shiki-dark-font-weight:inherit&quot;&gt;## credentials@1.24&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; credentials.xml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-weight:bold;--shiki-dark:#82B1FF;--shiki-dark-font-weight:inherit&quot;&gt;## cvs@2.12&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; hudson.scm.CVSSCM.xml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-weight:bold;--shiki-dark:#82B1FF;--shiki-dark-font-weight:inherit&quot;&gt;## git@2.4.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; hudson.plugins.git.GitSCM.xml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; jobs/build=cache2k/config.xml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;. . .&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;missing-information-with-workflow&quot;&gt;Missing Information with Workflow&lt;/h2&gt;
&lt;p&gt;In the above example the job &lt;code&gt;build=cache2k&lt;/code&gt; is a Maven job. But with the new Jenkins Workflow plugin, the way
you define your Jenkins jobs changes. Instead of a structured XML file which contains the plugin configuration,
with Workflow a job is just a Groovy script, or, to be more precise, a Groovy-ish Workflow DSL. So, no plugin
information any more, just script code.&lt;/p&gt;
&lt;p&gt;It should be technically feasible to log the information about the used plugins at runtime. For this
I opened &lt;a href=&quot;https://issues.jenkins-ci.org/browse/JENKINS-31582&quot;&gt;JENKINS-31582&lt;/a&gt;, please vote for it.&lt;/p&gt;
&lt;h2 id=&quot;alternatives&quot;&gt;Alternatives&lt;/h2&gt;
&lt;p&gt;There is a &lt;a href=&quot;http://documentation.cloudbees.com/docs/cje-user-guide/plugin-usage-using.html&quot;&gt;Plugin Usage Plugin&lt;/a&gt; in the
commercial CloudBees Jenkins Platform offering. There is also the
&lt;a href=&quot;https://wiki.jenkins-ci.org/display/JENKINS/Plugin+Usage+Plugin+(Community)&quot;&gt;Plugin Usage Plugin Community&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;While these plugins are useful and just run OOTB, I think the approach presented above has some value of
its own:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Easily analyse a Jenkins configuration without running it&lt;/li&gt;
&lt;li&gt;Simple customisable text output, to be included in an infrastructure documentation&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;callout&quot; data-callout=&quot;note&quot; data-collapsible=&quot;false&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;&lt;div class=&quot;callout-title-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;1em&quot; height=&quot;1em&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;line x1=&quot;18&quot; y1=&quot;2&quot; x2=&quot;22&quot; y2=&quot;6&quot;&gt;&lt;/line&gt;&lt;path d=&quot;M7.5 20.5 19 9l-4-4L3.5 16.5 2 22z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/div&gt;&lt;div class=&quot;callout-title-text&quot;&gt;Note&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;callout-content&quot;&gt;&lt;p&gt;Update 2026: Typo and wording fixes, switch to UK English.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;</content:encoded><category>Jenkins</category></item><item><title>About caching exceptions</title><link>https://cruftex.net/about-caching-exceptions</link><guid isPermaLink="true">https://cruftex.net/about-caching-exceptions</guid><description>In version 0.20 of cache2k we shipped enhanced exception support. So it is time for some mumblings on caching and how to handle exceptions.</description><pubDate>Sun, 07 Dec 2014 17:43:59 GMT</pubDate><content:encoded>&lt;p&gt;In version 0.20 of cache2k we shipped enhanced exception support. So it is time for some
mumblings on caching and how to handle exceptions.&lt;/p&gt;
&lt;h2 id=&quot;cache-aside-or-read-through-caching&quot;&gt;Cache aside or read-through caching&lt;/h2&gt;
&lt;p&gt;Let’s start with some basics. There are two typical usage patterns to integrate a cache into an application.&lt;/p&gt;
&lt;p&gt;In the cache aside pattern the application queries the cache and in the case of a miss
it requests or generates the data. Here is what it typically looks like:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;    Data&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#C5E478&quot;&gt; data&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1976D2;--shiki-dark:#C5E478&quot;&gt; cache&lt;/span&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;key&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; (data &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1976D2;--shiki-dark:#82AAFF&quot;&gt; null&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;      data &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt; generateData&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#D9F5DD&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#82AAFF&quot;&gt;key&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#D9F5DD&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1976D2;--shiki-dark:#C5E478&quot;&gt;      cache&lt;/span&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt;put&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;key&lt;/span&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; data&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The other approach is the read-through configuration. The cache is configured with a data source and calls it
in case of a cache miss.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic&quot;&gt;    // cache building:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;    Cache&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#C5E478&quot;&gt; cache&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1976D2;--shiki-dark:#C5E478&quot;&gt; CacheBuilder&lt;/span&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt;newCache&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1976D2;--shiki-dark:#C5E478&quot;&gt;Data&lt;/span&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1976D2;--shiki-light-font-style:inherit;--shiki-dark:#FAF39F;--shiki-dark-font-style:italic&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1976D2;--shiki-dark:#C5E478&quot;&gt; String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1976D2;--shiki-light-font-style:inherit;--shiki-dark:#FAF39F;--shiki-dark-font-style:italic&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;      .&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt;source&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt; CacheSource&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#D9F5DD&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic&quot;&gt;        public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt; Data&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt; get&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D7DBE0&quot;&gt; key&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt;          generateData&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#D9F5DD&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#82AAFF&quot;&gt;key&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#D9F5DD&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;      }&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;      .&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt;build&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic&quot;&gt;    // cache usage:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;    Data&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#C5E478&quot;&gt; data&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1976D2;--shiki-dark:#C5E478&quot;&gt; cache&lt;/span&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;key&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I prefer the read-through configuration for many reasons. The code is cleaner and it does not need
“to know” how to generate or fetch the data. It is simply a more object oriented approach. Another reason
to use the read-through pattern is the blocking feature of cache2k. Blocking means that there will be no more
than one request for the same key to the cache source.&lt;/p&gt;
&lt;p&gt;What happens in case &lt;code&gt;generateData&lt;/code&gt; throws an exception? In the cache aside pattern this is obvious: Since
&lt;code&gt;generateData&lt;/code&gt; is called by the application, it has to handle it. If the read-through pattern is used, it is possible
for the cache to do “its thing” with the exception.&lt;/p&gt;
&lt;h2 id=&quot;the-jsr107-cache-loader-versus-the-cache2k-cache-source&quot;&gt;The JSR107 cache loader versus the cache2k cache source&lt;/h2&gt;
&lt;p&gt;For the read-through pattern the Java caching standard (JSR107) defines the &lt;code&gt;CacheLoader&lt;/code&gt; interface to fetch the data:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic&quot;&gt;      public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic&quot;&gt; interface&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#FFCB8B&quot;&gt; CacheLoader&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;K&lt;/span&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt; V&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;&amp;gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;        V&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt; load&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;K&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D7DBE0&quot;&gt; key&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic&quot;&gt; throws&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt; CacheLoaderException&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;        Map&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;K&lt;/span&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt; V&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;&amp;gt; &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt;loadAll&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;Iterable&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;?&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt; K&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;&amp;gt; &lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D7DBE0&quot;&gt;keys&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic&quot;&gt; throws&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt; CacheLoaderException&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;      }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;JSR107 applications need to wrap any exception in a &lt;code&gt;CacheLoaderException&lt;/code&gt;. The cache2k &lt;code&gt;CacheSource&lt;/code&gt; interface is
simpler:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic&quot;&gt;      public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic&quot;&gt; interface&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#FFCB8B&quot;&gt; CacheSource&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;K&lt;/span&gt;&lt;span style=&quot;--shiki-light:#212121;--shiki-dark:#D6DEEB&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt; V&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;&amp;gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic&quot;&gt;        public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt; V&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt; get&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;K&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D7DBE0&quot;&gt; key&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D9F5DD&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic&quot;&gt; throws&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt; Exception&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;      }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Applications implementing the &lt;code&gt;CacheSource&lt;/code&gt; interface are allowed to throw any exception. I opted against the JSR107
design for two reasons: First, it leads to boilerplate wrapping code. Second, the cache needs to have some
countermeasures against exceptions anyway, so the wrapping can be done within the cache without additional overhead.&lt;/p&gt;
&lt;h2 id=&quot;ouch-an-exception-what-to-do-with-it&quot;&gt;Ouch, an exception, what to do with it?&lt;/h2&gt;
&lt;p&gt;So what are the options for the cache when the cache source throws an exception? Let’s do some quick brainstorming:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Wrap the exception and throw it. Don’t do any caching, so the next time the application calls &lt;code&gt;get()&lt;/code&gt; the data
is fetched again from the source.&lt;/li&gt;
&lt;li&gt;Store the exception in the cache. Each time the key is requested with &lt;code&gt;get()&lt;/code&gt;, throw a new exception wrapping
the original one.&lt;/li&gt;
&lt;li&gt;If there is a valid value in the cache from a previous call of the cache source, return that value instead of
throwing an exception.&lt;/li&gt;
&lt;li&gt;Store the exception in the cache, but do a quick expiry, so the fetch from the source is retried after a
short period of time.&lt;/li&gt;
&lt;li&gt;Combinations of the above…&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Did I miss anything? What is useful in your application scenario?&lt;/p&gt;
&lt;h2 id=&quot;cache2k-exception-support&quot;&gt;cache2k exception support&lt;/h2&gt;
&lt;p&gt;cache2k supports all the possible behaviours outlined above. For tweaking the semantics when exceptions
turn up, there are the following builder options:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;suppressExceptions(boolean)&lt;/em&gt;: Switch the suppression of exceptions on or off, if the cache already contains
a value for the requested key.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;exceptionExpiryDuration(long v, TimeUnit u)&lt;/em&gt;: After the expiry time the cache tries another fetch from the source.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;exceptionExpiryCalculator(ExceptionExpiryCalculator c)&lt;/em&gt;: Provides a calculator that returns a custom expiry
for an exception. This way temporary exceptions may be treated differently.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The methods &lt;code&gt;get()&lt;/code&gt; and &lt;code&gt;peek()&lt;/code&gt; throw a wrapped exception, called &lt;code&gt;PropagatedCacheException&lt;/code&gt;, if the cache source
had thrown an exception and it was not possible to suppress it.&lt;/p&gt;
&lt;p&gt;The iteration of the cache entries never throws an exception. Thus, the &lt;code&gt;CacheEntry&lt;/code&gt; interface has the method
&lt;code&gt;getException()&lt;/code&gt; to check whether there was an exception for that specific entry. If an exception is suppressed, it
is not stored in the cache. In this case there is no way to determine from the &lt;code&gt;CacheEntry&lt;/code&gt; properties whether the
last fetch operation was successful or caused an exception.&lt;/p&gt;
&lt;p&gt;There are statistics counters that get incremented for each exception thrown by the cache source. It is possible
to access them via JMX:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic&quot;&gt;        /**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic&quot;&gt;         * Number of exceptions thrown by the {@link org.cache2k.CacheSource}.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic&quot;&gt;         */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;        long&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt; getFetchExceptionCnt&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#D9F5DD&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic&quot;&gt;        /**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic&quot;&gt;         * Number of exceptions thrown by the CacheSource that were ignored and&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic&quot;&gt;         * the previous data value got returned.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic&quot;&gt;         */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;        long&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic&quot;&gt; getSuppressedExceptionCnt&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#D9F5DD&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;bulk-operations-and-exceptions&quot;&gt;Bulk operations and exceptions&lt;/h2&gt;
&lt;p&gt;It is possible to retrieve multiple cache entries in one method call via &lt;code&gt;Cache.getAll()&lt;/code&gt;. There is also a
&lt;code&gt;BulkCacheSource&lt;/code&gt; interface, which is used by the cache to fetch multiple entries at once. What should happen if
an exception occurs during a bulk operation?&lt;/p&gt;
&lt;p&gt;Let’s consider an example: The cache client wants to retrieve keys 1, 2 and 3. For key 3 there is a mapped entry in
the cache, so it calls the bulk source to fetch the values for keys 1 and 2. This operation is not successful and leads
to an exception. What should be the outcome of the operation? Should &lt;code&gt;getAll()&lt;/code&gt; throw an exception or return only
the value for key 3?&lt;/p&gt;
&lt;p&gt;The current implementation goes by the rule “be as specific as possible and return as many valid values as possible”.
This leads to: The &lt;code&gt;Cache.getAll()&lt;/code&gt; operation always returns a map with the requested keys and never produces
an exception. Only when the map is accessed is an exception thrown, if that key had caused an exception.&lt;/p&gt;
&lt;h2 id=&quot;the-default-behaviour&quot;&gt;The default behaviour&lt;/h2&gt;
&lt;p&gt;The default behaviour tries to be useful in most situations. If nothing is configured explicitly, the caching
of exceptions is switched on. If there is an entry expiry, the expiry time of an exception will be one
tenth of the configured value expiry time.&lt;/p&gt;
&lt;h2 id=&quot;is-this-really-useful&quot;&gt;Is this really useful?&lt;/h2&gt;
&lt;p&gt;Since exceptions are an intrinsic Java feature you have to deal with them anyway. Advanced exception handling
adds some complexity to a cache implementation. So is it really worthwhile or better to just
ignore it and throw an exception right when it happens?&lt;/p&gt;
&lt;p&gt;Inexperienced programmers might overuse exceptions instead of defining proper return values.
But if exceptions are not cached and exceptions are used to communicate some conditions to the caller (e.g.
a &lt;code&gt;FileNotFoundException&lt;/code&gt;), the performance characteristics of the application may suffer dramatically. The cache
is just bypassed.&lt;/p&gt;
&lt;p&gt;There are many conditions in applications that may lead to exceptions. E.g.: A temporary network outage,
a resource shortage (connection pool limit, thread pool limit) or a seldom requested corner case. In many situations
the cache can do something useful. One useful thing is to suppress an exception if there is still a value in
the cache. This makes the whole application more robust against temporary problems without the need to write
any additional code.&lt;/p&gt;
&lt;h2 id=&quot;the-cons-of-exception-caching&quot;&gt;The cons of exception caching&lt;/h2&gt;
&lt;p&gt;There are some pitfalls with cached exceptions. The most subtle one is the fact that when the cache rethrows
an exception and you analyse it, you may think it just happened. However, that exception might have happened in the past
and you have no idea when exactly. Second, you also don’t know whether an exception is the identical
one just thrown multiple times or whether it happened multiple times in reality. So besides caching exceptions
being useful, this all may be very confusing.&lt;/p&gt;
&lt;p&gt;For this pitfall it is probably a good idea to add a timestamp to an exception. If an exception gets logged, you
can compare the log timestamp and the exception timestamp and see that it was a cached exception. That is something
that will be added in the next releases.&lt;/p&gt;
&lt;div class=&quot;callout&quot; data-callout=&quot;note&quot; data-collapsible=&quot;false&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;&lt;div class=&quot;callout-title-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;1em&quot; height=&quot;1em&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;line x1=&quot;18&quot; y1=&quot;2&quot; x2=&quot;22&quot; y2=&quot;6&quot;&gt;&lt;/line&gt;&lt;path d=&quot;M7.5 20.5 19 9l-4-4L3.5 16.5 2 22z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/div&gt;&lt;div class=&quot;callout-title-text&quot;&gt;Note&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;callout-content&quot;&gt;&lt;p&gt;Update 2026: Typo and wording fixes, switch to UK English.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;</content:encoded><category>Java</category><category>caching</category><category>cache2k</category></item><item><title>cache2k v0.20 &quot;Utsire&quot; released</title><link>https://cruftex.net/cache2k-v0-20-released</link><guid isPermaLink="true">https://cruftex.net/cache2k-v0-20-released</guid><description>It has been a while since the last release, so this release is rather meaty. Most of the changes and new lines of code are for the upcoming persistence support.</description><pubDate>Mon, 01 Dec 2014 16:43:41 GMT</pubDate><content:encoded>&lt;p&gt;It has been a while since the last release, so this release is rather meaty. Most of the changes and new lines of
code are for the upcoming persistence support. Persistence is still unfinished, there is a lot of restructuring and
stabilising going on. Here are the highlights.&lt;/p&gt;
&lt;h2 id=&quot;no-dependencies&quot;&gt;No dependencies&lt;/h2&gt;
&lt;p&gt;The dependencies on a logging framework and the JSR305 annotations package were removed. The JSR305 annotations are useful, but
the spec is unfinished or “dormant” as the JCP page states. Using the annotations within the cache2k API would have meant
that these annotations “sneak into” all applications that use cache2k. Bad idea!&lt;/p&gt;
&lt;p&gt;From now on we will try to keep a “no dependencies” policy. If we need a dependency, then it is optional and/or a
separate Maven module.&lt;/p&gt;
&lt;h2 id=&quot;logging-abstraction&quot;&gt;Logging abstraction&lt;/h2&gt;
&lt;p&gt;There is a simple logging abstraction. By default java.util.logging is used. If Apache Commons Logging is present, all
output is routed through Commons Logging. I have plans to switch to SLF4J in the future. Vote for it in
&lt;a href=&quot;https://github.com/headissue/cache2k/issues/7&quot;&gt;GitHub issue 7&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;new-interface-for-the-expiry-policy&quot;&gt;New interface for the expiry policy&lt;/h2&gt;
&lt;p&gt;The interface to determine the entry expiry received a major overhaul. The old interface is still present but will be removed
in the near future.&lt;/p&gt;
&lt;h2 id=&quot;exception-support&quot;&gt;Exception support&lt;/h2&gt;
&lt;p&gt;Version 0.19 already had support for exceptions. In a read through configuration, exceptions from the cache source
are cached and rethrown wrapped in a &lt;code&gt;PropagatedCacheException&lt;/code&gt;. Caching exceptions needs some deeper elaboration, I will
spend a separate blog entry on that.&lt;/p&gt;
&lt;h2 id=&quot;android-support&quot;&gt;Android support&lt;/h2&gt;
&lt;p&gt;For Android support we switched back to language level 1.6. The only thing I really miss is the diamond operator.
All code that needs packages below &lt;code&gt;javax.*&lt;/code&gt; which are not available on Android, especially the JMX support,
went to another module (cache2k-ee). If you want an Android compatible cache, use the following Maven POM snippet:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;xml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;dependency&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;  &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;org.cache2k&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;  &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;cache2k-api&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;  &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;${cache2k-version}&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;dependency&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;dependency&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;org.cache2k&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;cache2k-core&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;${cache2k-version}&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;scope&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;runtime&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;scope&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;dependency&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you want to have a full featured cache, use this:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;xml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;dependency&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;  &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;org.cache2k&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;  &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;cache2k-api&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;  &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;${cache2k-version}&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;dependency&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;dependency&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;org.cache2k&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;cache2k-ee&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;${cache2k-version}&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;scope&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;runtime&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;scope&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#22863A;--shiki-dark:#CAECE6&quot;&gt;dependency&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#7FDBCA&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;configuration-for-tunable-constants&quot;&gt;Configuration for “tunable constants”&lt;/h2&gt;
&lt;p&gt;Within the cache2k implementation there are a lot of constants. One example is the hash load factor, which
is 64 percent by default. While there is no need to change this value, it feels unwise to bury such values
deep in the code and have no mechanism to change them. This magic number and others are factored out and there
is some documentation for them on the implementing class. Look out for the Tunable subclass.&lt;/p&gt;
&lt;p&gt;It is possible to change these values by providing a properties file at &lt;code&gt;/org/cache2k/tuning.properties&lt;/code&gt;.
Here is some example content:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes min-light night-owl&quot; style=&quot;--shiki-light:#24292eff;--shiki-dark:#d6deeb;--shiki-light-bg:#ffffff;--shiki-dark-bg:#011627;overflow-x:auto;--file-name-offset:-0.75rem&quot; tabindex=&quot;0&quot; data-language=&quot;properties&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic&quot;&gt;# increase load factor to save memory&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;org.cache2k.impl.BaseCache.Tunable.hashLoadPercent&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#D6DEEB&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;80&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic&quot;&gt;# switch off randomization for tests&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#C792EA&quot;&gt;org.cache2k.impl.BaseCache.Tunable.disableHashRandomization&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D32F2F;--shiki-dark:#D6DEEB&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292EFF;--shiki-dark:#D6DEEB&quot;&gt;true&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For the normal usage of cache2k there is no need to change or tune such a value. This mechanism is intentionally totally
decoupled from the normal cache configuration. Maybe this concept proves useful to provide a “tuning” for different
platforms and CPU architectures, like Android, Java 7, amd64, PowerPC, etc.&lt;/p&gt;
&lt;p&gt;The fine print: The “tunable constants” configuration is not an official API, so there will be no notice of changes.&lt;/p&gt;
&lt;h2 id=&quot;iteration&quot;&gt;Iteration&lt;/h2&gt;
&lt;p&gt;The method &lt;code&gt;Cache.iterator()&lt;/code&gt; was added. It supports a fully concurrent iteration of the cache contents.
The iterator also implements the Closeable interface. After the iteration is finished or the iterator is not used any more,
it is best to close it to free resources immediately.&lt;/p&gt;
&lt;h2 id=&quot;persistence-preview&quot;&gt;Persistence preview&lt;/h2&gt;
&lt;p&gt;The persistence support is not finished yet and should be considered “alpha level”. It is
included and can be tested. The upcoming release will finalise the persistence support.
Maybe it is ready for Christmas?&lt;/p&gt;
&lt;p&gt;The default and so far only persistence implementation writes the data to a single file and
keeps the index with the keys in the Java heap. To activate persistence simply add &lt;code&gt;persistence()&lt;/code&gt;
to the configuration builder. Some options are available, see
&lt;a href=&quot;http://cache2k.org/cache2k-api/apidocs/index.html?org/cache2k/StorageConfiguration.Builder.html&quot;&gt;JavaDoc StorageConfiguration.Builder&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;complete-list-of-changes&quot;&gt;Complete list of changes&lt;/h2&gt;
&lt;p&gt;You will find the brief list of changes in the &lt;a href=&quot;http://cache2k.org/0/20.html&quot;&gt;0.20 release notes&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&quot;callout&quot; data-callout=&quot;note&quot; data-collapsible=&quot;false&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;&lt;div class=&quot;callout-title-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;1em&quot; height=&quot;1em&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;line x1=&quot;18&quot; y1=&quot;2&quot; x2=&quot;22&quot; y2=&quot;6&quot;&gt;&lt;/line&gt;&lt;path d=&quot;M7.5 20.5 19 9l-4-4L3.5 16.5 2 22z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/div&gt;&lt;div class=&quot;callout-title-text&quot;&gt;Note&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;callout-content&quot;&gt;&lt;p&gt;Update 2026: Typo and wording fixes, switch to UK English.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;</content:encoded><category>Java</category><category>caching</category><category>cache2k</category></item></channel></rss>