0

As exercise I wrote program which should clean object from memory by using garbage collector and method finalize()

class Dog {
    boolean dogAlive = false;
    String dogName;
    Dog(String dogName) {
        this.dogName = dogName;
    }
    void makeDogAlive() {
        dogAlive = true;
    }
    void makeDogDead() {
        dogAlive = false;
    }
    protected void finalize() {
        System.out.println("test");
        if (dogAlive)
            System.out.println("Error: you need to finish life of your dog");
    }
}
public class FinalisationExc1 {
    public static void main(String[] args) {
        Dog dog1 = new Dog("Azor");
        dog1.makeDogAlive();
        System.out.println("Your dog name is " + dog1.dogName);
        System.gc();
    }
}

but it looks like System.gc() is not calling my finalize() method? I've done all same as in example from book (I mean a methode used there but code is mine) and when example from book is working, mine not :(. Can you give me any clue where I made mistake?

EDIT: Ok, I work a bit on my program and it looks like System.gc() is called when I create object (do I?) by new Dog("Azor") (1st method) instead of `Dog dog1 = new Dog("Azor")(2nd method). I'm just started to learn Java and to be when I see difference (in 2nd method we create object dog1, but do we create anything in 1st method?) I don't understand it.

blekione
  • 10,152
  • 3
  • 20
  • 26
  • possible duplicate of [Java Finalize method call](http://stackoverflow.com/questions/2506488/java-finalize-method-call) – arshajii Mar 20 '14 at 22:23
  • @arshajii Definitely not. – user207421 Mar 20 '14 at 22:24
  • @EJP Please elaborate. – arshajii Mar 20 '14 at 22:24
  • 1
    A quick read of the documentation for `System.gc()` (actually, [`Runtime.gc()`](http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#gc%28%29) and related methods in `Runtime` will yield the answer to this question. Also the documentation for [`Object.finalize()`](http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#finalize%28%29) describes what's happening, as well as http://stackoverflow.com/questions/2506488/java-finalize-method-call (which this may very well be a duplicate of). SO is not a replacement for Google and documentation. – Jason C Mar 20 '14 at 22:29
  • 3
    This question appears to be off-topic because documentation should be read before asking questions about a method's behavior, and SO is not a replacement for Google or JDK documentation. – Jason C Mar 20 '14 at 22:30
  • 1
    @arshajii Please elaborate why a question about System.gc() not calling finalize() is a duplicate of a question about finalize() never being called at all. – user207421 Mar 20 '14 at 22:51
  • I would say, that I'm new on stackoverflow as new on Java. For many of you my question will look dumb, but treating newcomers the way "documentation should be read 1st" or "this is duplicat of something what sound similar" instead of trying to help, you just make feel newcomers as you are unfriendly community. – blekione Mar 21 '14 at 00:32

3 Answers3

3

System#gc() does not trigger garbage collection. It suggests garbage collection to the JVM. Consequently, Object#finalize() is not guaranteed to run at any particular time. It's called by the garbage collector, which your code does not (and should not) have direct control over.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • 2
    I don't believe `finalize` is guaranteed to run at all... – Boris the Spider Mar 20 '14 at 22:24
  • @BoristheSpider it's guaranteed to be called during GC: _"Called by the garbage collector on an object when garbage collection determines that there are no more references to the object."_ Of course, when GC happens is not guaranteed. – Matt Ball Mar 20 '14 at 22:26
2

As far as I know calling System.gc is no guarantee that the garbage collection process will actually run. According to the documentation:

Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse.

In your case, most probably you program ends before the GC has even a chance to run.

Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
2

System.gc() isn't obliged to do anything. It's only a hint to the garbage collector. See the Javadoc. Also your program exits immediately you've called it, which doesn't give it much scope for action.

user207421
  • 305,947
  • 44
  • 307
  • 483