Thread: Java finalize

  1. #1
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278

    Java finalize

    I am getting no output from finalize function.

    Code:
    class A
    {
    	public static int c = 5;
    	int i,j;
    	
    	void showij()
    	{
    	    System.out.println("i = "+i+" j = "+j);
    	}
    	
    	protected void finalize()
    	{
    		c--;
    		System.out.println("Yes done!");
    	}
    }
    
    
    class B extends A
    {
    	int k;
    	
    	void showk()
    	{
    		System.out.println("k = "+k);
    	}
    	
    	void sum() 
    	{
    		System.out.println("i+j+k: " + (i+j+k));
    	}
    }
    
    class SimpleInheritance
    {
    	public static void main(String args[]) 
    	{
    		A ob = new A();
    		ob.i = 4;
    		ob = null;
    		System.out.println("c = "+A.c);
    	}
    	
    	
    }

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    2
    Quote Originally Posted by anirban View Post
    I am getting no output from finalize function.

    Code:
    class A
    {
        public static int c = 5;
        int i,j;
        
        void showij()
        {
            System.out.println("i = "+i+" j = "+j);
        }
        
        protected void finalize()
        {
            c--;
            System.out.println("Yes done!");
        }
    }
    
    
    class B extends A
    {
        int k;
        
        void showk()
        {
            System.out.println("k = "+k);
        }
        
        void sum() 
        {
            System.out.println("i+j+k: " + (i+j+k));
        }
    }
    
    class SimpleInheritance
    {
        public static void main(String args[]) 
        {
            A ob = new A();
            ob.i = 4;
            ob = null;
            System.out.println("c = "+A.c);
        }
        
        
    }
    The garbage collector is not guaranteed to run everytime you tend to free references by making it point to null. Stupid Fix: Manually add System.gc( ) at the end. Should Work

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Here are some links that might help:
    Object finalization and cleanup - JavaWorld,
    finalize never called? - Java | DaniWeb,
    Java Programmer's SourceBook : Thinking in Java.

    I haven't done Java in over a decade, and I only skimmed those articles, but it seems like there is no requirement that the garbage collector be run at a certain point in the progrram. It could be that, since you allocate so little memory, and ob's life is over when the program is over, the JVM recognizes this and says "I don't really need to explicitly destroy this object, it will happen when the program terminates".

  4. #4
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    Okay thanks a lot! Let me check out.

    EDIT : Seen the links but could not make out somehow exactly how to make my program work!
    Last edited by anirban; 02-10-2012 at 01:52 PM.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    My point (and I think the point of the articles) is that your method may not be called. The finalize method is not to be relied upon for freeing resources. And that keeps with the Java JVM specification. Why do you think it needs to call your finalize function? The object 'ob' only drops out of scope at the end of the program, at which point the JVM is already going to free up all the resources it used, including ob. Even if ob fell out of scope before the end of the program, finalize might not get called. The JVM is under no obligation to free up the resources immediately, only if and when it sees fit. 'ob' only uses a tiny bit of memory. If you had to do some stuff after ob fell out of scope and the last reference to it was removed, the JVM might decide that it's a waste of time to free that memory at that time, and wait until it really needs it. But if the program ends before the JVM decides it really needs that memory back from ob, it won't call finalize.

    The gist of the articles as far as I can tell is that if you want to explicitly free resources used by an object that wouldn't otherwise be cleaned up by the garbage collector, then you have to do it manually, before you release the last reference to it.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    In other words:
    Code:
    class A
    {
        public static int c = 5;
        int i,j;
         
        void showij()
        {
            System.out.println("i = "+i+" j = "+j);
        }
         
        public void close()
        {
            c--;
            System.out.println("Yes done!");
        }
    }
    
    class SimpleInheritance
    {
        public static void main(String args[])
        {
            A ob = new A();
            try {
              ob.i = 4;
              ob = null;
              System.out.println("c = "+A.c);
            } finally {
              ob.close();
            }
        }
    }
    Or in Java7:
    Code:
    class A implements Closeable
    {
        public static int c = 5;
        int i,j;
         
        void showij()
        {
            System.out.println("i = "+i+" j = "+j);
        }
         
        public void close()
        {
            c--;
            System.out.println("Yes done!");
        }
    }
    
    class SimpleInheritance
    {
        public static void main(String args[])
        {
            try (A ob = new A()) {
              ob.i = 4;
              ob = null;
              System.out.println("c = "+A.c);
            }
        }
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where's java
    By jaylc185 in forum C++ Programming
    Replies: 5
    Last Post: 05-24-2005, 01:38 PM
  2. Multiple Java files for one Java project
    By doubleanti in forum Windows Programming
    Replies: 2
    Last Post: 11-22-2004, 02:06 AM
  3. Java...?
    By Korn1699 in forum Windows Programming
    Replies: 6
    Last Post: 10-29-2003, 01:23 PM
  4. Java, C++?
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 10-07-2002, 09:59 PM
  5. help with java
    By qwertiop in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 09-10-2002, 05:08 PM