Why can I use Runtime.getRuntime() in Eclipse
Hi Folks,
I am using Eclipse and I can't use Runtime.getRuntime() or freememory in order to make garbage collection program.Any ideas?
Code:
/*File:Rational.java
* Write a program that allocates 10000 Rational objects without saving any of them
in variables so that they all become garbage. Once you’ve done so, measure the
amount of free memory before and after garbage collection and use the difference to
report how many bytes were freed, as shown in the following sample run:
*/
import acm.program.*;
import java.util.*;
import java.lang.*;
import acm.util.*;
/**
* The Rational class is used to represent rational numbers, which
* are defined to be the quotient of two integers.
*/
public class Rational {
private static int totalrational=1000;
public static void main (String[]arg){
Runtime myRuntime = new Runtime.getRuntime();
myRuntime.freememory();
for(int i=0; i<totalrational; i++){
Rational a= new Rational(1, 2);
Rational b = new Rational(1, 3);
Rational c = new Rational(1, 6);
Rational sum = a.add(b).add(c);
System.out.println(a + " + " + b + " + " + c + " = " + sum);
}
}
/**
* Creates a new Rational initialized to zero.
*/
public Rational() {
this(0);
}
/**
* Creates a new Rational from the integer argument.
* @param n The initial value
*/
public Rational(int n) {
this(n, 1);
}
And this is the error I am getting:
Code:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Runtime.getRuntime cannot be resolved to a type
The method freememory() is undefined for the type Runtime
at Rational.main(Rational.java:33)
According to a website we can get this kind of error if appropriate import is missing at the top.is that right?