shohan_slideshare
193 views
13 slides
Aug 09, 2018
Slide 1 of 13
1
2
3
4
5
6
7
8
9
10
11
12
13
About This Presentation
Succinct description about Java Object Lifecycle.
Size: 385.81 KB
Language: en
Added: Aug 09, 2018
Slides: 13 pages
Slide Content
Java Object Lifecycle What do we mean by Java Object: ~ a collection of data and actions. ~ an instance of a class ~ have states and behaviors Car, Bird and Human are considered as Class in Java. Mr. Mofiz is an object of human and Tata Nano is the object of car
Object Creation Using new keyword is the most common way to create an object in java. ClassName Obj.Name = new ClassName(); // Human mr_mofiz = new Human(); // Car tata_nano = new Car();
Object creation (cont.) Object creation Statement p erforms 3 actions: Declaration ~ a variable declaration which simply declares to the compiler To refer to an object whose type is Human Instantiation new operator instantiates the Human class Initialization Human initializes the object
Phases in Objects Life Cycle
“Created” Phase of Java Object Lifecycle Creation of an Object means A llocation of memory Calling constructor Initializing its properties When Object is created Ready for use Lives in memory heap of the JVM
“In Use” Phase of Java Object Lifecycle Any Object that is held by any strong reference is said to be in use Car tata_nano = new Car(); // Here tata_nano is strong reference to Car Object May have multiple strong reference of any object. Never approached by GC Create an Object and forget it without having its reference
“Invisible” Phase of Java Object Lifecycle ~ are strong references which are not accessible while the reference and Object are both are in scope public void execute() { Try { Object obj = new Object(); } catch(Exception e) { e.printStackTrace(); } while(true) { ……………. …………. …………. } }
“Invisible” Phase of Java Object Lifecycle ~ referenced by obj is out of scope and is eligible for GC but in fact it lives in the same stack frame and occupies memory in heap area ~can cause serious memory blockage and there are chances to get OutOfMemoryException. Fix: To fix this we have to explicitly set the references to null after using them.
“Unreachable” Phase of Java Object Lifecycle ~ no more strong references to it exist and it can’t be accessed ~ is eligible for GC GC is also smart enough to detect circular references and collect them too. Car tata_nano = new Car(); //line 1 tata_nano = null; //line 2(unreachable) Car tata_nano = new Car (); //line 1 Owner owner = new Owner(); //line 2 tata_nano .addOwner(owner); //line 3 owner.addVehicle( tata_nano ); //line 4 tata_nano = null; //line 5 owner = null; //line 6
“Collected” Phase of Java Object Lifecycle ~ is in the “collected” state when the garbage collector has recognized an object as unreachable ~is just a phase before its deallocation. ~ (if ) has finalize method then it is marked for finalization otherwise it directly moves to finalized state.
Finalized Phase of Java Object Lifecycle ~ is in the “finalized” state if it is still unreachable after it’s finalize method ~A finalized object is awaiting de-allocation ~to ensure that important resources are freed in a timely manner ~To lengthening object lifetimes, finalize methods can increase object size
Deallocated Phase of Java Object Lifecycle ~is the final step in garbage collection If an object is still unreachable after all the above work has occurred, then it is a candidate for deallocation. deallocation occurs is up to the JVM.