Skip to content

Commit

Permalink
#98 vjtop继续提升性能降低消耗, 正确设置Map的大小
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin1978 committed Aug 17, 2018
1 parent fd5faaf commit cc1859b
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions vjtop/src/main/java/com/vip/vjtools/vjtop/VMDetailView.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,15 @@ private void printTopCpuThreads(DetailMode mode) throws IOException {
return;
}

Map<Long, Long> threadCpuTotalTimes = new HashMap<Long, Long>();
Map<Long, Long> threadCpuDeltaTimes = new HashMap<Long, Long>();
Map<Long, Long> threadSysCpuTotalTimes = new HashMap<Long, Long>();
Map<Long, Long> threadSysCpuDeltaTimes = new HashMap<Long, Long>();

long threadsHaveValue = 0;

long tids[] = vmInfo.getThreadMXBean().getAllThreadIds();
int mapSize = tids * 1.5;
Map<Long, Long> threadCpuTotalTimes = new HashMap<Long, Long>(mapSize);
Map<Long, Long> threadCpuDeltaTimes = new HashMap<Long, Long>(mapSize);
Map<Long, Long> threadSysCpuTotalTimes = new HashMap<Long, Long>(mapSize);
Map<Long, Long> threadSysCpuDeltaTimes = new HashMap<Long, Long>(mapSize);

// 批量获取CPU times,性能大幅提高。
// 两次获取之间有间隔,在低流量下可能造成负数
Expand Down Expand Up @@ -305,7 +306,6 @@ private void printTopCpuThreads(DetailMode mode) throws IOException {
lastThreadSysCpuTotalTimes = threadSysCpuTotalTimes;
}


private void printTopMemoryThreads(DetailMode mode) throws IOException {

if (!vmInfo.threadMemoryAllocatedSupported) {
Expand All @@ -315,9 +315,9 @@ private void printTopMemoryThreads(DetailMode mode) throws IOException {
}

long tids[] = vmInfo.getThreadMXBean().getAllThreadIds();

Map<Long, Long> threadMemoryTotalBytesMap = new HashMap<Long, Long>();
Map<Long, Long> threadMemoryDeltaBytesMap = new HashMap<Long, Long>();
int mapSize = tids.length * 1.5;
Map<Long, Long> threadMemoryTotalBytesMap = new HashMap<Long, Long>(mapSize);
Map<Long, Long> threadMemoryDeltaBytesMap = new HashMap<Long, Long>(mapSize);

long totalDeltaBytes = 0;
long totalBytes = 0;
Expand Down Expand Up @@ -386,9 +386,9 @@ private void printTopMemoryThreads(DetailMode mode) throws IOException {
long allocationRate = threadDelta == null ? 0 : (threadDelta * 1000) / vmInfo.upTimeMills.delta;
System.out.printf(dataFormat, tid, threadName, Utils.leftStr(info.getThreadState().toString(), 10),
Utils.toFixLengthSizeUnit(allocationRate),
getThreadMemoryUtilization(threadMemoryDeltaBytesMap.get(tid), totalDeltaBytes),
getMemoryUtilization(threadMemoryDeltaBytesMap.get(tid), totalDeltaBytes),
Utils.toFixLengthSizeUnit(threadMemoryTotalBytesMap.get(tid)),
getThreadMemoryUtilization(threadMemoryTotalBytesMap.get(tid), totalBytes));
getMemoryUtilization(threadMemoryTotalBytesMap.get(tid), totalBytes));
}

// 打印线程汇总信息,这里因为最后单位是精确到秒,所以bytes除以毫秒以后要乘以1000才是按秒统计
Expand Down Expand Up @@ -482,7 +482,7 @@ public void cleanupThreadsHistory() {
this.lastThreadMemoryTotalBytes.clear();
}

private static double getThreadMemoryUtilization(Long threadBytes, long totalBytes) {
private static double getMemoryUtilization(Long threadBytes, long totalBytes) {
if (threadBytes == null || totalBytes == 0) {
return 0;
}
Expand Down

0 comments on commit cc1859b

Please sign in to comment.