SlideShare a Scribd company logo
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 performs 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
● Allocation 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.
Source:
https://himanshugpt.wordpress.com/2010/03/17/life-cycle-of-java-object/
https://dzone.com/articles/ocajp-7-object-lifecycle-java

More Related Content

What's hot

What’s new in Kotlin?
What’s new in Kotlin?What’s new in Kotlin?
What’s new in Kotlin?
Squareboat
 
An introduction to Object Oriented JavaScript
An introduction to Object Oriented JavaScriptAn introduction to Object Oriented JavaScript
An introduction to Object Oriented JavaScript
TO THE NEW | Technology
 
Groovy AST Transformations
Groovy AST TransformationsGroovy AST Transformations
Groovy AST Transformations
hendersk
 
JavaScript Execution Context
JavaScript Execution ContextJavaScript Execution Context
JavaScript Execution Context
Juan Medina
 
JavaScript, qué hermoso eres
JavaScript, qué hermoso eresJavaScript, qué hermoso eres
JavaScript, qué hermoso eres
Alea Soluciones, S.L.
 
Groovy AST Demystified
Groovy AST DemystifiedGroovy AST Demystified
Groovy AST Demystified
Andres Almiray
 
Intro Ruby Classes Part I
Intro Ruby Classes Part IIntro Ruby Classes Part I
Intro Ruby Classes Part I
Juan Leal
 
Introduction of Object Oriented JavaScript
Introduction of Object Oriented JavaScriptIntroduction of Object Oriented JavaScript
Introduction of Object Oriented JavaScript
NexThoughts Technologies
 
Constructors, Intro to Ruby Classes Part II
Constructors, Intro to Ruby Classes Part IIConstructors, Intro to Ruby Classes Part II
Constructors, Intro to Ruby Classes Part II
Juan Leal
 
jQuery (intermediate)
jQuery (intermediate)jQuery (intermediate)
jQuery (intermediate)
Madhukar Anand
 
Designpatterns illustrated
Designpatterns illustratedDesignpatterns illustrated
Designpatterns illustrated
Nhat Vo Van
 
Meta Programming in Groovy
Meta Programming in GroovyMeta Programming in Groovy
Meta Programming in Groovy
NexThoughts Technologies
 
AST Transformations: Groovy’s best kept secret by Andres Almiray
AST Transformations: Groovy’s best kept secret by Andres AlmirayAST Transformations: Groovy’s best kept secret by Andres Almiray
AST Transformations: Groovy’s best kept secret by Andres Almiray
ZeroTurnaround
 
Groovy intro
Groovy introGroovy intro
Java (advanced and core)online training in Hyderabad|course content
Java (advanced and core)online training in Hyderabad|course contentJava (advanced and core)online training in Hyderabad|course content
Java (advanced and core)online training in Hyderabad|course content
RS Trainings
 
Ast transformation
Ast transformationAst transformation
Ast transformation
Gagan Agrawal
 
Javascript for Intermediates
Javascript for IntermediatesJavascript for Intermediates
Javascript for Intermediates
Ankit Agrawal
 
Groovy MOPping
Groovy MOPpingGroovy MOPping
Groovy MOPping
Izzet Mustafaiev
 
Java8
Java8Java8
Java8
fbenault
 
JavaScript: Patterns, Part 3
JavaScript: Patterns, Part  3JavaScript: Patterns, Part  3
JavaScript: Patterns, Part 3
Chris Farrell
 

What's hot (20)

What’s new in Kotlin?
What’s new in Kotlin?What’s new in Kotlin?
What’s new in Kotlin?
 
An introduction to Object Oriented JavaScript
An introduction to Object Oriented JavaScriptAn introduction to Object Oriented JavaScript
An introduction to Object Oriented JavaScript
 
Groovy AST Transformations
Groovy AST TransformationsGroovy AST Transformations
Groovy AST Transformations
 
JavaScript Execution Context
JavaScript Execution ContextJavaScript Execution Context
JavaScript Execution Context
 
JavaScript, qué hermoso eres
JavaScript, qué hermoso eresJavaScript, qué hermoso eres
JavaScript, qué hermoso eres
 
Groovy AST Demystified
Groovy AST DemystifiedGroovy AST Demystified
Groovy AST Demystified
 
Intro Ruby Classes Part I
Intro Ruby Classes Part IIntro Ruby Classes Part I
Intro Ruby Classes Part I
 
Introduction of Object Oriented JavaScript
Introduction of Object Oriented JavaScriptIntroduction of Object Oriented JavaScript
Introduction of Object Oriented JavaScript
 
Constructors, Intro to Ruby Classes Part II
Constructors, Intro to Ruby Classes Part IIConstructors, Intro to Ruby Classes Part II
Constructors, Intro to Ruby Classes Part II
 
jQuery (intermediate)
jQuery (intermediate)jQuery (intermediate)
jQuery (intermediate)
 
Designpatterns illustrated
Designpatterns illustratedDesignpatterns illustrated
Designpatterns illustrated
 
Meta Programming in Groovy
Meta Programming in GroovyMeta Programming in Groovy
Meta Programming in Groovy
 
AST Transformations: Groovy’s best kept secret by Andres Almiray
AST Transformations: Groovy’s best kept secret by Andres AlmirayAST Transformations: Groovy’s best kept secret by Andres Almiray
AST Transformations: Groovy’s best kept secret by Andres Almiray
 
Groovy intro
Groovy introGroovy intro
Groovy intro
 
Java (advanced and core)online training in Hyderabad|course content
Java (advanced and core)online training in Hyderabad|course contentJava (advanced and core)online training in Hyderabad|course content
Java (advanced and core)online training in Hyderabad|course content
 
Ast transformation
Ast transformationAst transformation
Ast transformation
 
Javascript for Intermediates
Javascript for IntermediatesJavascript for Intermediates
Javascript for Intermediates
 
Groovy MOPping
Groovy MOPpingGroovy MOPping
Groovy MOPping
 
Java8
Java8Java8
Java8
 
JavaScript: Patterns, Part 3
JavaScript: Patterns, Part  3JavaScript: Patterns, Part  3
JavaScript: Patterns, Part 3
 

Similar to Java object

Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
Elizabeth alexander
 
Java class loading tips and tricks - Java Colombo Meetup, January, 2014
Java class loading  tips and tricks - Java Colombo Meetup, January, 2014Java class loading  tips and tricks - Java Colombo Meetup, January, 2014
Java class loading tips and tricks - Java Colombo Meetup, January, 2014
Sameera Jayasoma
 
Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
Abhijeet Dubey
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
Somya Bagai
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2
Rakesh Madugula
 
Core Java interview questions-ppt
Core Java interview questions-pptCore Java interview questions-ppt
Core Java interview questions-ppt
Mayank Kumar
 
java basic .pdf
java basic .pdfjava basic .pdf
java basic .pdf
Satish More
 
Garbage Collection in Java.pdf
Garbage Collection in Java.pdfGarbage Collection in Java.pdf
Garbage Collection in Java.pdf
SudhanshiBakre1
 
Java Interview Questions Answers Guide
Java Interview Questions Answers GuideJava Interview Questions Answers Guide
Java Interview Questions Answers Guide
DaisyWatson5
 
Exploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App developmentExploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App development
Jayaprakash R
 
Why using finalizers is a bad idea
Why using finalizers is a bad ideaWhy using finalizers is a bad idea
Why using finalizers is a bad idea
PVS-Studio
 
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAdvanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sir
AVINASH KUMAR
 
RxJava@DAUG
RxJava@DAUGRxJava@DAUG
RxJava@DAUG
Maxim Volgin
 
OOPC_Unit-I.pdf
OOPC_Unit-I.pdfOOPC_Unit-I.pdf
OOPC_Unit-I.pdf
KRaviChythanyaCSE
 
JavaScript
JavaScriptJavaScript
JavaScript
Ivano Malavolta
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
Satya Johnny
 
Adv kvr -satya
Adv  kvr -satyaAdv  kvr -satya
Adv kvr -satya
Jyothsna Sree
 
Understanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG JuneUnderstanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG June
Deepu S Nath
 
Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it works
Mindfire Solutions
 
Concurrency
ConcurrencyConcurrency
Concurrency
Isaac Liao
 

Similar to Java object (20)

Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Java class loading tips and tricks - Java Colombo Meetup, January, 2014
Java class loading  tips and tricks - Java Colombo Meetup, January, 2014Java class loading  tips and tricks - Java Colombo Meetup, January, 2014
Java class loading tips and tricks - Java Colombo Meetup, January, 2014
 
Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2
 
Core Java interview questions-ppt
Core Java interview questions-pptCore Java interview questions-ppt
Core Java interview questions-ppt
 
java basic .pdf
java basic .pdfjava basic .pdf
java basic .pdf
 
Garbage Collection in Java.pdf
Garbage Collection in Java.pdfGarbage Collection in Java.pdf
Garbage Collection in Java.pdf
 
Java Interview Questions Answers Guide
Java Interview Questions Answers GuideJava Interview Questions Answers Guide
Java Interview Questions Answers Guide
 
Exploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App developmentExploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App development
 
Why using finalizers is a bad idea
Why using finalizers is a bad ideaWhy using finalizers is a bad idea
Why using finalizers is a bad idea
 
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAdvanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sir
 
RxJava@DAUG
RxJava@DAUGRxJava@DAUG
RxJava@DAUG
 
OOPC_Unit-I.pdf
OOPC_Unit-I.pdfOOPC_Unit-I.pdf
OOPC_Unit-I.pdf
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
 
Adv kvr -satya
Adv  kvr -satyaAdv  kvr -satya
Adv kvr -satya
 
Understanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG JuneUnderstanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG June
 
Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it works
 
Concurrency
ConcurrencyConcurrency
Concurrency
 

Recently uploaded

Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
Miro Wengner
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
Edge AI and Vision Alliance
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
FREE A4 Cyber Security Awareness Posters-Social Engineering part 3
FREE A4 Cyber Security Awareness  Posters-Social Engineering part 3FREE A4 Cyber Security Awareness  Posters-Social Engineering part 3
FREE A4 Cyber Security Awareness Posters-Social Engineering part 3
Data Hops
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
Javier Junquera
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
AstuteBusiness
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their MainframeDigital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Precisely
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
AWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptxAWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptx
HarisZaheer8
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
ScyllaDB
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 

Recently uploaded (20)

Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
FREE A4 Cyber Security Awareness Posters-Social Engineering part 3
FREE A4 Cyber Security Awareness  Posters-Social Engineering part 3FREE A4 Cyber Security Awareness  Posters-Social Engineering part 3
FREE A4 Cyber Security Awareness Posters-Social Engineering part 3
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their MainframeDigital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
AWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptxAWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptx
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 

Java object

  • 1. 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
  • 2. 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();
  • 3. Object creation (cont.) Object creation Statement performs 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
  • 4. Phases in Objects Life Cycle
  • 5. “Created” Phase of Java Object Lifecycle Creation of an Object means ● Allocation of memory ● Calling constructor ● Initializing its properties When Object is created ● Ready for use ● Lives in memory heap of the JVM
  • 6. “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
  • 7. “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) { ……………. …………. …………. } }
  • 8. “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.
  • 9. “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
  • 10. “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.
  • 11. 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
  • 12. 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.

Editor's Notes

  1. https://dzone.com/articles/ocajp-7-object-lifecycle-java
  2. https://himanshugpt.wordpress.com/2010/03/17/life-cycle-of-java-object/