我正在研究java的垃圾收集器部分,我已经编写了下面的java类,但是它在选择它时在方法printGCStats()上显示了eclipse编辑器本身的编译时错误,它显示了错误“void”@expected,请建议如何克服这个...
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
class demoGrbage {
public static void main(String[] args) {
public void printGCStats()
{
long totalGarbageCollections = 0;
long garbageCollectionTime = 0;
for(GarbageCollectorMXBean gc :
ManagementFactory.getGarbageCollectorMXBeans()) {
long count = gc.getCollectionCount();
if(count >= 0) {
totalGarbageCollections += count;
}
long time = gc.getCollectionTime();
if(time >= 0) {
garbageCollectionTime += time;
}
}
System.out.println("Total Garbage Collections: "
+ totalGarbageCollections);
System.out.println("Total Garbage Collection Time (ms): "
+ garbageCollectionTime);
}
}
}