Skip to content

Commit feeac59

Browse files
committed
Heap.getGCRoots() returns all GCRoots including duplicates (issue 215)
1 parent daf82ab commit feeac59

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

visualvm/heapviewer.truffle/src/org/graalvm/visualvm/heapviewer/truffle/TruffleObjectsProvider.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.Iterator;
3939
import java.util.List;
4040
import java.util.Set;
41+
import java.util.stream.Collectors;
4142
import javax.swing.SortOrder;
4243
import org.graalvm.visualvm.lib.jfluid.heap.GCRoot;
4344
import org.graalvm.visualvm.lib.jfluid.heap.Heap;
@@ -232,15 +233,17 @@ protected String getNodesContainerString(String firstNodeIdx, String lastNodeIdx
232233

233234
public HeapViewerNode[] getGCRoots(HeapViewerNode parent, HeapContext context, String viewID, HeapViewerNodeFilter viewFilter, List<DataType> dataTypes, List<SortOrder> sortOrders, Progress progress, int aggregation) throws InterruptedException {
234235
final Heap heap = context.getFragment().getHeap();
235-
236-
final List<GCRoot> gcroots = new ArrayList(heap.getGCRoots());
237-
236+
final List<GCRoot> gcrootsS = (List<GCRoot>) heap.getGCRoots();
237+
final List<Instance> gcrootInstances = gcrootsS.stream()
238+
.map(GCRoot::getInstance)
239+
.distinct()
240+
.collect(Collectors.toList());
238241
try {
239242
progress.setupUnknownSteps();
240243

241-
Iterator<GCRoot> gcrootsI = gcroots.iterator();
244+
Iterator<Instance> gcrootsI = gcrootInstances.iterator();
242245
while (gcrootsI.hasNext()) {
243-
Instance instance = gcrootsI.next().getInstance();
246+
Instance instance = gcrootsI.next();
244247
if (!language.isLanguageObject(instance)) gcrootsI.remove();
245248
progress.step();
246249
}
@@ -250,17 +253,17 @@ public HeapViewerNode[] getGCRoots(HeapViewerNode parent, HeapContext context, S
250253

251254
HeapViewerNode[] nodes;
252255
if (aggregation == 0) {
253-
NodesComputer<GCRoot> computer = new NodesComputer<GCRoot>(UIThresholds.MAX_TOPLEVEL_INSTANCES) {
256+
NodesComputer<Instance> computer = new NodesComputer<Instance>(UIThresholds.MAX_TOPLEVEL_INSTANCES) {
254257
protected boolean sorts(DataType dataType) {
255258
return !DataType.COUNT.equals(dataType);
256259
}
257-
protected HeapViewerNode createNode(GCRoot gcroot) {
258-
O object = language.createObject(gcroot.getInstance());
260+
protected HeapViewerNode createNode(Instance gcRootInstance) {
261+
O object = language.createObject(gcRootInstance);
259262
return (HeapViewerNode)language.createObjectNode(object, object.getType(heap));
260263
}
261-
protected ProgressIterator<GCRoot> objectsIterator(int index, Progress progress) {
262-
Iterator<GCRoot> gcRootsIt = gcroots.listIterator(index);
263-
return new ProgressIterator(gcRootsIt, index, false, progress);
264+
protected ProgressIterator<Instance> objectsIterator(int index, Progress progress) {
265+
Iterator<Instance> iterator = gcrootInstances.listIterator(index);
266+
return new ProgressIterator(iterator, index, false, progress);
264267
}
265268
protected String getMoreNodesString(String moreNodesCount) {
266269
return Bundle.TruffleObjectsProvider_MoreNodesGcRoots(moreNodesCount);
@@ -280,8 +283,8 @@ protected String getNodesContainerString(String firstNodeIdx, String lastNodeIdx
280283
try {
281284
progress.setupUnknownSteps();
282285

283-
for (GCRoot gcroot : gcroots) {
284-
tcomputer.addObject(language.createObject(gcroot.getInstance()));
286+
for (Instance gcroot : gcrootInstances) {
287+
tcomputer.addObject(language.createObject(gcroot));
285288
progress.step();
286289
}
287290
} finally {

visualvm/libs.profiler/profiler.oql/src/org/graalvm/visualvm/lib/profiler/oql/engine/api/impl/Snapshot.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,8 @@ public Iterator getRoots() {
325325
return delegate.getGCRoots().iterator();
326326
}
327327

328-
private List getRootsInstancesList() {
329-
List<Object> roots = new ArrayList<Object>();
328+
private Set getRootsInstances() {
329+
Set<Object> roots = new HashSet<Object>();
330330
for(Object rootObj : delegate.getGCRoots()) {
331331
GCRoot root = (GCRoot)rootObj;
332332
Instance inst = root.getInstance();
@@ -365,7 +365,7 @@ public State(ReferenceChain path, Iterator<Instance> iterator) {
365365

366366
List<ReferenceChain> result = new ArrayList<ReferenceChain>();
367367

368-
Iterator toInspect = getRootsInstancesList().iterator();
368+
Iterator toInspect = getRootsInstances().iterator();
369369
ReferenceChain path = null;
370370
State s = new State(path, toInspect);
371371

0 commit comments

Comments
 (0)