忽略FindBugs的单一发现

时间:2013-05-23 11:41:49

标签: eclipse findbugs

我使用Findbugs插件运行Eclipse Java EE juno SR2 win32。

以下代码示例让FindBugs显示一个发现:

catch (OutOfMemoryError e) {
     tryAgain = true;
     log.error("try to recover", e);
     System.gc(); // not so happy about this
}

这是描述

Bug: [..] forces garbage collection; extremely dubious except in benchmarking code

Code explicitly invokes garbage collection. Except for specific use in benchmarking, this is very dubious.

In the past, situations where people have explicitly invoked the garbage collector in routines such as close or 
finalize methods has led to huge performance black holes. Garbage collection can be expensive. Any situation that forces 
hundreds or thousands of garbage collections will bring the machine to a crawl.

Confidence: High, Rank: Of Concern (16)
Pattern: DM_GC 
Type: Dm, Category: PERFORMANCE (Performance)

那么我怎样才能对这一行进行注释而不进行检查呢?

我尝试过(source

@edu.umd.cs.findbugs.annotations.SuppressWarnings("DM_GC")
System.gc();

这不起作用所以我把它移到方法

@edu.umd.cs.findbugs.annotations.SuppressWarnings("DM_GC")
private bla(){

这说:“edu无法解析为某种类型” 所以我尝试了:

@SuppressWarnings("DM_GC")
private bla(){

我收到了“不支持的@SuppressWarnings(”DM_GC“)”

这不是关于解决findbugs错误的请求,它是如何禁用特定错误的。

谢谢!

仅供参考:我知道打电话给gc并不是一个好主意。

1 个答案:

答案 0 :(得分:4)

  

“edu无法解析为类型”

这是因为您尚未将FindBugs jar添加到项目中。如果您正在使用Maven,请将此依赖项添加到pom.xml:

<dependency>
    <groupId>com.google.code.findbugs</groupId>
    <artifactId>annotations</artifactId>
    <version>2.0.0</version>
</dependency>

如果您不使用Maven,请将jar manually添加到您的类路径中。然后会找到FindBugs注释SuppressWarnings,你不应再看到问题了。