我只根据导师的喜好使用 acm 包。
这个程序应该分配10000个有理对象,让它们变成垃圾,然后计算垃圾收集器使用前后的空闲内存。然后,它应该打印垃圾收集器已清除的内存量。
import acm.program.*;
public class RuntimeGarbage extends ConsoleProgram {
public void run() {
println("Allocating 10000 Rational Objects");
for(int i=1; i==10000; i++) {
new Rational();
}
Runtime myRuntime = Runtime.getRuntime();
long free_before = myRuntime.freeMemory();
myRuntime.gc();
long free_after = myRuntime.freeMemory();
println("Garbage collection freed" + ((free_after)-(free_before)) + "bytes");
}
}
问题在于,当我尝试编译代码时,cmd显示以下内容:
:8: error: cannot find symbol
new Rational();
with an arrow right below the R.
问题可能是在大括号内创建对象吗?