1

I'm only using the acm package as per my instructor's preference.
This program is supposed to allocate 10000 rational objects so that they will become garbage, and then calculate the free memory before and after the use of the garbage collector. Then, it's supposed to print the amount of memory that the garbage collector has cleared.

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");
    }
}


The problem with this, is that when i try to compile the code, the cmd shows the following:

:8: error: cannot find symbol 
new Rational(); 
with an arrow right below the R. 

Could it be that the problem is the creation of the objects within the curly brackets?

Bosko Mijin
  • 3,287
  • 3
  • 32
  • 45
user113377
  • 29
  • 6
  • 1
    There is probably an import missing. If you use Eclipse you can choose `Organize Imports` from the context menu, other IDEs provide similar functions – user1781290 Dec 31 '13 at 13:06
  • In which package Rational class resides? – akhikhl Dec 31 '13 at 13:14
  • Like i said, i'm pretty sure that acm.program.* is the only import i'm supposed to use. – user113377 Dec 31 '13 at 13:15
  • According to the acm-Javadocs, there is no `Rational` class in it – user1781290 Dec 31 '13 at 13:54
  • And as a side note, the whole experiment seems a bit tricky to me. The garbage collector is afaik allowed to kick in even while your loop is running (and collect some of the objects you created before) and also it is allowed to ignore your call to `gc` – user1781290 Dec 31 '13 at 13:57
  • I wasn't aware of that, that's indeed interesting. But i'm just following instructions given in the exercise, so i guess that doesn't matter. – user113377 Dec 31 '13 at 14:09

1 Answers1

0

What compiler is saying is that it does not know where the type Rational is defined. Yes, you can create object in the code block of the for loop.

According to google the type Rational is not defined in the package acm

rational site:www-cs-faculty.stanford.edu/~eroberts/jtf/javadoc/student/acm/

so it must have been defined somewhere else.

It does not look it belongs to built-in java types either http://docs.oracle.com/javase/7/docs/api/

matcheek
  • 4,887
  • 9
  • 42
  • 73