Good way to clear nested Maps in Java <pre><code>public class MyCache { AbstractMap&lt;String, AbstractMap&lt;String, Element&gt;&gt; cache = new TreeMap&lt;String, AbstractMap&lt;String, Element&gt;&gt;(); public Boolean putElement(String targetNamespace, Element element) { ... } public void clear() { cache.clear(); } // is it better this way? public void deepClear() { for(AbstractMap&lt;String, Element&gt; innerMap : cache.values()) { innerMap.clear(); } cache.clear(); } } </code></pre> <p>Is it necessary to iterate over the values of the root map and clear all the maps nested in the root first, or is it enough to clear the outermost map? My main question is, if there is any difference in the memory consumption of the JVM between these two methods?</p>