简体   繁体   English

findbugs报告错误的行号并抑制错误?

[英]findbugs report wrong line number and suppress error?

I have a java class which use System.GC() to calculate memory usage. 我有一个Java类,它使用System.GC()计算内存使用量。 However when I run findbugs, it always report wrong line number. 但是,当我运行findbugs时,它总是报告错误的行号。 Question 1: I ran this with intellij findbugs plugin as well as findbugs native GUI. 问题1:我使用intellij findbugs插件以及findbugs本机GUI来运行它。 Same result. 结果相同。 Am I doing something wrong here? 我在这里做错什么了吗?

  1. util.MemoryTestBench.lotsOfGC() forces garbage collection; util.MemoryTestBench.lotsOfGC()强制进行垃圾收集; extremely dubious except in benchmarking code Class: MemoryTestBench (util) Line: 35 - 35 ====> which is Thread.sleep(100) ???????? 除了基准测试代码类外,这非常可疑。类:MemoryTestBench(util)行:35-35 ====>这是Thread.sleep(100)????????

  2. Dead store to handle Class: MemoryTestBench (util) Line: 23 - 23 ====> which is return usedMemory() - prevUsedMemory ????? 死存储来处理类:MemoryTestBench(util)行:23-23 ====>这是返回usedMemory()-prevUsedMemory ???

Question 2: how can I suppress error DM_GC ? 问题2:如何抑制错误DM_GC? I tried @SuppressWarnings which didn't work. 我尝试了@SuppressWarnings,它没有用。

test code is as follows: 测试代码如下:

package util;

import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

import edu.umd.cs.findbugs.annotations.SuppressWarnings;

public class MemoryTestBench {
    @SuppressWarnings (value = "DM_GC",
                       justification = "call GC to measure memory size")
    public static long calculateMemoryUsage(ObjectFactory factory) {
        Object handle = factory.makeObject();
        long prevUsedMemory = usedMemory();
        handle = null;
        lotsOfGC();
        prevUsedMemory = usedMemory();
        handle = factory.makeObject();
        lotsOfGC();
        return usedMemory() - prevUsedMemory;
    }

    private static long usedMemory() {
        return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
    }


    private static void lotsOfGC() {
        for (int i = 0; i < 20; i++) {
            System.gc();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }

    public static void showMemoryUsage(ObjectFactory factory) {
        long mem = calculateMemoryUsage(factory);
        System.out.println(
                factory.getClass().getSimpleName() + " produced " +
                        factory.makeObject().getClass().getSimpleName() +
                        " which took " + mem + " bytes");
    }

    public static void main(String[] args) {
        showMemoryUsage(new BigConcurrentHashMapFactory());
    }
}

你应该把@SuppressWarnings lotsOfGC方法之前,而不是calculateMemoryUsage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM