Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -632,25 +632,26 @@ private String getRetainedHeapTopConsumersDescription(int[] objects, IProgressLi
{ return Messages.LeakHunterQuery_RetainedHeapComponentEmpty; }
int[] minRetainedSet = snapshot.getMinRetainedSet(objects, listener);

Map<String, Long> sumHeapSizes = new HashMap<>();
Map<String, Integer> counts = new HashMap<>();
Map<Integer, Long> sumHeapSizes = new HashMap<>();
Map<Integer, Integer> counts = new HashMap<>();
Comment on lines +635 to +636
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also have org.eclipse.mat.collect.HashMapIntLong as an option, to avoid the boxing.

for (int objectId : minRetainedSet)
{
IObject object = snapshot.getObject(objectId);
String name = snapshot.isClass(objectId) ? ((IClass) object).getName() : object.getClazz().getName();
sumHeapSizes.merge(name, snapshot.getHeapSize(objectId), Long::sum);
counts.merge(name, 1, Integer::sum);
if (listener.isCanceled())
throw new IProgressListener.OperationCanceledException();
Integer key = snapshot.isClass(objectId) ? objectId : snapshot.getClassOf(objectId).getObjectId();
sumHeapSizes.merge(key, snapshot.getHeapSize(objectId), Long::sum);
counts.merge(key, 1, Integer::sum);
}
StringBuilder result = new StringBuilder();
Set<Entry<String, Long>> entries = sumHeapSizes.entrySet();
Set<Entry<Integer, Long>> entries = sumHeapSizes.entrySet();
boolean multipleItems = entries.size() > 1 && topItems > 1;
PriorityQueue<Entry<String, Long>> pq = new PriorityQueue<Entry<String, Long>>(entries.size(),
PriorityQueue<Entry<Integer, Long>> pq = new PriorityQueue<Entry<Integer, Long>>(entries.size(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In hindsight, wondering if you found this PQ better than a sort?

(e1, e2) -> e2.getValue().compareTo(e1.getValue()));
pq.addAll(entries);
int maxItems = Math.min(pq.size(), topItems);
while (maxItems-- > 0 && pq.size() > 0)
{
Entry<String, Long> entry = pq.poll();
Entry<Integer, Long> entry = pq.poll();
if (result.length() > 0)
{
result.append(", "); //$NON-NLS-1$
Expand All @@ -660,16 +661,15 @@ private String getRetainedHeapTopConsumersDescription(int[] objects, IProgressLi
result.append(Messages.LeakHunterQuery_RetainedHeapComponentAnd);
}
int count = counts.get(entry.getKey());
if (count == 1)
{
result.append(MessageUtil.format(Messages.LeakHunterQuery_RetainedHeapComponentInstance,
HTMLUtils.escapeText(entry.getKey()), count, bytesFormatter.format(entry.getValue())));
}
else
{
result.append(MessageUtil.format(Messages.LeakHunterQuery_RetainedHeapComponentInstances,
HTMLUtils.escapeText(entry.getKey()), count, bytesFormatter.format(entry.getValue())));
}

int objectId = entry.getKey();
IObject object = snapshot.getObject(objectId);
String name = snapshot.isClass(objectId) ? ((IClass) object).getName() : object.getClazz().getName();

result.append(MessageUtil.format(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😎 great

count == 1 ? Messages.LeakHunterQuery_RetainedHeapComponentInstance
: Messages.LeakHunterQuery_RetainedHeapComponentInstances,
HTMLUtils.escapeText(name), count, bytesFormatter.format(entry.getValue())));
}
return result.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,10 @@ public DominatorsSummary getDominatorsOf(int[] objectIds, Pattern excludePattern
* Get object abstracting the real Java Object from the heap dump identified
* by the given id.
* <p>
* Performance: Relatively fast - single index operation.
* Performance: Relatively fast - single index operation; however, there
* may be a fixed size cache of objects and heavy use of this method can induce
* overhead in cache management. When possible, prefer to use {@code objectId}
* until absolutely necessary to gather object information.
*
* @param objectId
* id of object you want a convenient object abstraction for
Expand Down