Skip to content

Commit bcd4e95

Browse files
committed
GH-309 cache all ThreadObjectHprofGCRoot instances not just the last one
1 parent b5a82f2 commit bcd4e95

File tree

1 file changed

+21
-15
lines changed
  • visualvm/libs.profiler/lib.profiler/src/org/graalvm/visualvm/lib/jfluid/heap

1 file changed

+21
-15
lines changed

visualvm/libs.profiler/lib.profiler/src/org/graalvm/visualvm/lib/jfluid/heap/HprofGCRoots.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,18 @@
5555
class HprofGCRoots {
5656

5757
final HprofHeap heap;
58-
private ThreadObjectHprofGCRoot lastThreadObjGC;
59-
final private Object lastThreadObjGCLock = new Object();
58+
final private Object threadSerialMapLock = new Object();
59+
private LongHashMap threadSerialMap;
60+
private int rootThreadsCount;
6061
private Map gcRoots;
6162
final private Object gcRootLock = new Object();
62-
private List gcRootsList;
63+
private List<GCRoot> gcRootsList;
6364

6465
HprofGCRoots(HprofHeap h) {
6566
heap = h;
6667
}
6768

68-
Collection getGCRoots() {
69+
List<GCRoot> getGCRoots() {
6970
synchronized (gcRootLock) {
7071
if (gcRoots == null) {
7172
gcRoots = new HashMap(16384);
@@ -106,20 +107,24 @@ Object getGCRoots(Long instanceId) {
106107
}
107108

108109
ThreadObjectGCRoot getThreadGCRoot(int threadSerialNumber) {
109-
synchronized (lastThreadObjGCLock) {
110-
if (lastThreadObjGC != null && threadSerialNumber == lastThreadObjGC.getThreadSerialNumber()) {
111-
return lastThreadObjGC;
112-
}
110+
List<GCRoot> roots = getGCRoots();
111+
synchronized (threadSerialMapLock) {
112+
if (threadSerialMap == null) {
113+
threadSerialMap = new LongHashMap(rootThreadsCount);
113114

114-
for (GCRoot gcRoot : heap.getGCRoots()) {
115-
if (gcRoot instanceof ThreadObjectHprofGCRoot) {
116-
ThreadObjectHprofGCRoot threadObjGC = (ThreadObjectHprofGCRoot) gcRoot;
117-
if (threadSerialNumber == threadObjGC.getThreadSerialNumber()) {
118-
lastThreadObjGC = threadObjGC;
119-
return threadObjGC;
115+
for (int i = 0; i < roots.size(); i++) {
116+
GCRoot gcRoot = roots.get(i);
117+
if (gcRoot instanceof ThreadObjectHprofGCRoot) {
118+
ThreadObjectHprofGCRoot threadObjGC = (ThreadObjectHprofGCRoot) gcRoot;
119+
threadSerialMap.put(threadObjGC.getThreadSerialNumber(), i);
120120
}
121121
}
122122
}
123+
int threadIndex = (int)threadSerialMap.get(threadSerialNumber);
124+
125+
if (threadIndex != -1) {
126+
return (ThreadObjectGCRoot)roots.get(threadIndex);
127+
}
123128
return null;
124129
}
125130
}
@@ -136,7 +141,8 @@ private void computeGCRootsFor(TagBounds tagBounds) {
136141
if (heap.readDumpTag(offset) == rootTag) {
137142
HprofGCRoot root;
138143
if (rootTag == HprofHeap.ROOT_THREAD_OBJECT) {
139-
root = new ThreadObjectHprofGCRoot(this, start);
144+
root = new ThreadObjectHprofGCRoot(this, start);
145+
rootThreadsCount++;
140146
} else if (rootTag == HprofHeap.ROOT_JAVA_FRAME) {
141147
root = new JavaFrameHprofGCRoot(this, start);
142148
} else if (rootTag == HprofHeap.ROOT_JNI_LOCAL) {

0 commit comments

Comments
 (0)