<p>As it seems noone else had the same issue or using primitive objects as cache entries, thus haven't noticed the issue.
Upon replicating and fortunately traced the root cause, the below points are coming up :</p>
<ul>
<li>Always implement Serializable / <code>hashCode</code> / <code>equals</code> for custom objects that are going to end been transmitted through a replicated/synchronized cache.</li>
<li>Never put primitive arrays, as the <code>hashcode</code> / <code>equals</code> would not be calculated - efficiently-</li>
<li>Dont enable eviction with remove strategy on replicated caches, as upon reaching the maximum limit, the entries are getting removed randomly - based on <a href="https://arxiv.org/abs/1512.00727" rel="nofollow noreferrer">TinyLFU</a> - and not based on the expired timer and never getting removed from the JVM heap.</li>
</ul>
Infinispan 9, Replicated Cache is Expiring Entries but never allows them to be removed from JVM heap
<p>Was doing some internal testing about a clustering solution on top of infinispan/jgroups and noticed that the expired entries were never becoming eligible for GC, due to a reference on the expiration-reaper, while having more than 1 nodes in the cluster with expiration enabled / eviction disabled.
Due to some system difficulties the below versions are being used :</p>
<ul>
<li>JDK 1.8</li>
<li>Infinispan 9.4.20</li>
<li>JGroups 4.0.21</li>
</ul>
<p>In my example I am using a simple Java main scenario, placing a specific number of data, expecting them to expire after a specific time period. The expiration is indeed happening, as it can be confirmed both while accessing the expired entry and by the respective event listener(if its configured), by it looks that it is never getting removed from the available memory, even after an explicit GC or while getting close to an OOM error.</p>
<p>So the question is :</p>
<p>Is this really expected as default behavior, or I am missing a critical configuration as per the cluster replication / expiration / serialization ?</p>
<p>Example :</p>
<p>Cache Manager :</p>
<pre><code>return new DefaultCacheManager(&quot;infinispan.xml&quot;);
</code></pre>
<p>infinispan.xml :</p>
<pre><code>  &lt;jgroups&gt;
     &lt;stack-file name=&quot;udp&quot; path=&quot;jgroups.xml&quot; /&gt;
  &lt;/jgroups&gt;

  &lt;cache-container default-cache=&quot;default&quot;&gt;
     &lt;transport stack=&quot;udp&quot; node-name=&quot;${nodeName}&quot; /&gt;
     &lt;replicated-cache name=&quot;myLeakyCache&quot; mode=&quot;SYNC&quot;&gt;
        &lt;expiration interval=&quot;30000&quot; lifespan=&quot;3000&quot; max-idle=&quot;-1&quot;/&gt;
     &lt;/replicated-cache&gt;
  &lt;/cache-container&gt;
</code></pre>
<p>Default UDP jgroups xml as in the packaged example :</p>
<p>.....</p>
<pre><code>&lt;UDP
        mcast_addr=&quot;${jgroups.udp.mcast_addr:x.x.x.x}&quot;
        mcast_port=&quot;${jgroups.udp.mcast_port:46655}&quot;
        bind_addr=&quot;${jgroups.bind.addr:y.y.y.y}&quot;
        tos=&quot;8&quot;
        ucast_recv_buf_size=&quot;200k&quot;
        ucast_send_buf_size=&quot;200k&quot;
        mcast_recv_buf_size=&quot;200k&quot;
        mcast_send_buf_size=&quot;200k&quot;
        max_bundle_size=&quot;64000&quot;
        ip_ttl=&quot;${jgroups.udp.ip_ttl:2}&quot;
        enable_diagnostics=&quot;false&quot;
        bundler_type=&quot;old&quot;
        thread_naming_pattern=&quot;pl&quot;
        thread_pool.enabled=&quot;true&quot;
        thread_pool.max_threads=&quot;30&quot;
        /&gt;
</code></pre>
<p>The dummy cache entry :</p>
<pre><code>public class CacheMemoryLeak implements Serializable {
    private static final long serialVersionUID = 1L;
    Date date = new Date();
}
</code></pre>
<p>An example usage from the &quot;service&quot; :</p>
<pre><code>Cache&lt;String, Object&gt; cache = cacheManager.getCache(&quot;myLeakyCache&quot;);
cache.put(key, new CacheMemoryLeak());
</code></pre>
<p>Some info / tryouts :</p>
<ul>
<li>When there is only one node in the cluster or restarting them
sequentially the references are getting cleared.</li>
<li>Enabling Max-idle shows the same behavior (makes sense expiration
reaper is the same)</li>
<li>Enabling eviction does not resolve the issue, just keeps the
&quot;expired&quot; references count between the max limit. In case this is
reached pretty fast, random eviction is happening on the live entries
as well(default remove strategy)!!</li>
<li>If i change the Cache entry to be a native String, then, the
infinispan.MortalCacheEntries are getting removed from the heap space
on the next GC cycle, upon getting expired and marked from expiration reaper, compared to the custom object!!</li>
<li>Enabling the expiration reaper only in one node didn't resolve the
issue, and might break the failover mechanism.</li>
<li>Upgraded to infinispan 10.1.8 Final , but faced the same issue.</li>
</ul>