Calculate correct lastRecords size

Motivation:
ResourceLeakDetector records at most MAX_RECORDS+1 records

Modifications:
Make room before add to lastRecords

Result:
ResourceLeakDetector will record at most MAX_RECORDS records
This commit is contained in:
louxiu 2017-07-17 11:04:35 +08:00 committed by Scott Mitchell
parent c43e09da5a
commit 96e06aa74d
1 changed files with 6 additions and 5 deletions

View File

@ -364,18 +364,19 @@ public class ResourceLeakDetector<T> {
}
private void record0(Object hint, int recordsToSkip) {
if (creationRecord != null) {
// Check MAX_RECORDS > 0 here to avoid similar check before remove from and add to lastRecords
if (creationRecord != null && MAX_RECORDS > 0) {
String value = newRecord(hint, recordsToSkip);
synchronized (lastRecords) {
int size = lastRecords.size();
if (size == 0 || !lastRecords.getLast().equals(value)) {
if (size >= MAX_RECORDS) {
lastRecords.removeFirst();
++removedRecords;
}
lastRecords.add(value);
}
if (size > MAX_RECORDS) {
lastRecords.removeFirst();
++removedRecords;
}
}
}
}