简体   繁体   中英

findbugs report wrong line number and suppress error?

I have a java class which use System.GC() to calculate memory usage. However when I run findbugs, it always report wrong line number. Question 1: I ran this with intellij findbugs plugin as well as findbugs native GUI. Same result. Am I doing something wrong here?

  1. util.MemoryTestBench.lotsOfGC() forces garbage collection; extremely dubious except in benchmarking code Class: MemoryTestBench (util) Line: 35 - 35 ====> which is Thread.sleep(100) ????????

  2. Dead store to handle Class: MemoryTestBench (util) Line: 23 - 23 ====> which is return usedMemory() - prevUsedMemory ?????

Question 2: how can I suppress error DM_GC ? I tried @SuppressWarnings which didn't work.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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