/** * Pruning duplicates reduces memory pressure from hprof bloat added in Marshmallow. */ void deduplicateGcRoots(Snapshot snapshot) { // THashMap has a smaller memory footprint than HashMap. final THashMap<String, RootObj> uniqueRootMap = new THashMap<>(); final Collection<RootObj> gcRoots = snapshot.getGCRoots(); for (RootObj root : gcRoots) { String key = generateRootKey(root); if (!uniqueRootMap.containsKey(key)) { uniqueRootMap.put(key, root); } } // Repopulate snapshot with unique GC roots. gcRoots.clear(); uniqueRootMap.forEach(new TObjectProcedure<String>() { @Override public boolean execute(String key) { return gcRoots.add(uniqueRootMap.get(key)); } }); }
/** * Executes <tt>procedure</tt> for each key in the map. * * @param procedure a <code>TObjectProcedure</code> value * @return false if the loop over the keys terminated because * the procedure returned false for some key. */ public boolean forEachKey(TObjectProcedure procedure) { return forEach(procedure); }