Constructor and Garbage Collection in java
from Head First Java chapter 09
As you know java is an object-oriented programming language. The creation of a lot of objects is acceptable but as per memory management for efficiency, the unused objects are temporarily kept far away. This chapter is talking about the mechanism.
Where does the created object live?
Memory Area. The Memory area actually has five-part.
Everything java lives in heap and stack.
- heap: where the objects are living.
- stack: where the local variables and parameters of methods are living. actually, the parameters are living in frame still the methods completed their process.
So, where are the instance variables living?
- Instance variables are living in an object that belongs to them.
Variables
- Local variables are variables that are declared in the method or parameter of methods. It can be primitive or reference. All the primitive variables are stored in frames that are temporarily created in the stack when executing the particular method.
- If a local variable is a reference variable then it is stored in the stack, while the object of this reference will be stored in the heap.
- Instance variables are variables that are declared inside the class but outside of the methods. It can be primitive or reference
- If an instance variable is primitive it is stored in an object belonging to it.
- if an instance variable is a reference variable, both the object and the reference go to the heap.
Creation of an object
The constructor
- The constructors are used to initialize the instance variables and state of the object and create a new instance as well.
- looks like a method but not,
- It does not have a return type as it is not going to return anything like a method.
- Constructors have a name like the particular class in which it is defined.
- can take any public, private, default access modifiers.
- can be overloaded and can not be overridden.
- this() keyword is used to invoke the overloaded constructor from the current class.
- the compiler will provide a default constructor when you did not define any constructor in your class.
- a default constructor is a constructor which does not have any arguments
- In the above image, The Animal() is a default constructor.
- you can define your own constructor with or without arguments also.
- It can be called only with the keyword new.
- the code inside a constructor will run only creating a new instance
- all constructor has super() to invoke its superclass constructor and it should be the first line internally.
- all constructors of a subclass will call its superclass default constructor implicitly. anyway, you can also call any superclass constructor using the super() keyword with appropriate parameters.
- This() and super() must be the first line statement in the constructor. meaning that a constructor can not call both.
As you know now, the creation of an object/instance happens when calling a constructor.
So, What about the life of an object after it was created? how long does it live in heap?
- The life of an object depends on how long a reference lives, does not matter, it is an instance or a local variable. It means that the object is alive until the reference is alive.
- local variable alive until the method completes the running process.
- An instance variable is alive until the object is alive in heap. the particular object alive until the reference is alive.
- Life of local variable: until its method completes.
- Scope of local variable: their access in only the methods where they declared
What about the reference variable?
- If the reference variable is a local variable, it is alive until the method completes the running process as same as primitive variables.
- If the method is completed, the reference variable is out of scope but is alive still. So, at that time, the assigned object becomes eligible for garbage collection(GC).
- GC provides memory management by destroying unused objects.
There are three ways for a reference to rid of an object and the objects eligible for GC.
- The reference comes out of scope permanently. when the method is completed, the reference may die.
- The reference is assigned to another object.
- The reference is explicitly set to null
an object becomes eligible for garbage collection,when its last live reference disappears
The method Finalize()
The Garbage collector always calls this method just before collecting an object which is eligible for garbage collection to release the object from any database connection or network connection. The process is known as finalization.
The finalize() method belongs to the Object class with no implementation. So, we need to override it. Garbage collectors call this method on any object. Because object class is the super(Top)class of any class.
Why finalize() method is used?
finalize() method releases system resources before the garbage collector runs for a specific object. JVM allows finalize() to be invoked only once per object.