CLEANING UP UNUSED OBJECTS

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Garbage Collection What is garbage and how can we deal with it?
From Theory to Practice 2 OOP Overview Performance issues: –Preparing classes for inheritance –Memory management and release of obsolete object.
1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer.
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
Rounding Out Classes The objectives of this chapter are: To discuss issues surrounding passing parameters to methods What is "this"? To introduce class.
Road Map Introduction to object oriented programming. Classes
Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Using final We use the notion of constant data to represent data that cannot be changed. public class Test { static final int someInt = 10; static final.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Reference Types. 2 Objectives Introduce reference types –class –array Discuss details of use –declaration –allocation –assignment –null –parameter –aggregation.
Reference Counters Associate a counter with each heap item Whenever a heap item is created, such as by a new or malloc instruction, initialize the counter.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Overview Assignment 6: hints  Living with a garbage collector Assignment 5: solution  Garbage collection.
Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CMSC 132 Week2 Lab1 Comparable vs Comparator clone() finalize()
Java and C++, The Difference An introduction Unit - 00.
CSC Programming I Lecture 8 September 9, 2002.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Session 7 Methods Strings Constructors this Inheritance.
Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Finalizers, this reference and static Sangeetha Parthasarathy 06/13/2001.
CSI 3125, Preliminaries, page 1 Compiling the Program.
G ARBAGE C OLLECTION CSCE-531 Ankur Jain Neeraj Agrawal 1.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Multithreading and Garbage Collection Session 16.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Minimising memory churn
Chapter No. : 2 Classes and Objects.
Garbage Collection What is garbage and how can we deal with it?
Core Java Garbage Collection LEVEL – PRACTITIONER.
Java Basics Classes and Objects.
Topic: Java Garbage Collection
Lecture 6 Object Oriented Programming Using Java
Static data members Constructors and Destructors
Unit-2 Objects and Classes
University of Central Florida COP 3330 Object Oriented Programming
Lecture 2 Memory management.
CSC 113 Tutorial QUIZ I.
Object Oriented Programming in java
METHOD OVERRIDING in JAVA
Method Overloading in JAVA
Sampath Kumar S Assistant Professor, SECE
Classes Lecture 7 from Chapter /1/11.
CS2011 Introduction to Programming I Arrays (II)
JAVA Constructors.
Sampath Kumar S Assistant Professor, SECE
Assessment – Java Basics: Part 1
class PrintOnetoTen { public static void main(String args[]) {
Why did the programmer quit his job?
Variables and Java vs C++
Web Design & Development Lecture 4
Developing Java Applications with NetBeans
Developing Java Applications with NetBeans
Lecture 2 Memory management.
Classes and Objects Object Creation
Garbage Collection What is garbage and how can we deal with it?
CMSC 202 Constructors Version 9/10.
Presentation transcript:

CLEANING UP UNUSED OBJECTS GARBAGE Collector Finalization

Garbage Collector Java garbage collection is the process by which Java programs perform automatic memory management.  When Java programs run on the JVM, objects are created on the heap Eventually, some objects will no longer be needed. The garbage collector finds these unused objects and deletes them to free up memory. How Garbage Collector Works? 1. Java garbage collection is an automatic process. The programmer does not need to explicitly mark objects to be deleted. The garbage collection implementation lives in the JVM. 2. In the first step, unreferenced objects are identified and marked as ready for garbage collection. In the second step, marked objects are deleted.

Advantages It makes java memory efficient because garbage collector removes the unreferenced objects from heap memory. It is automatically done by the garbage collector(a part of JVM) so we don't need to make extra efforts.

What is unreferenced object? Three ways are possible for an object to be as unreferenced: 1. By nullifying the reference 2. By assigning a reference to another 3. By anonymous object By nullifying the reference Student obj=new  Student();   obj=null;  2. By assigning a reference to another Student obj1=new  Student(); Student obj2=new  Student(); obj1=obj2; 3. By anonymous object new  Student();

Sample Program //Student s3; //s3=s2; //s3.showData(); //s2=null; class Student{ int a; int b; public void setData(int c,int d) { a=c; b=d; } public void showData() { System.out.println("Value of a = "+a); System.out.println("Value of b = "+b); } public static void main(String args[]) Student s1 = new Student(); Student s2 = new Student(); s1.setData(1,2); s2.setData(3,4); s1.showData(); s2.showData(); //Student s3; //s3=s2; //s3.showData(); //s2=null; //s3=null; }

AFTER COMPILATION & RUN

NEXT STEP

NEXT STEP

NEXT STEP

GARBAGE COLLECTION SAMPLE EXAMPLE class Student{  public void finalize() { System.out.println(“This object is garbage collected"); }   int a; int b; public void setData(int c,int d) { a=c; b=d; } public void showData() System.out.println("Value of a = "+a); System.out.println("Value of b = "+b); } public static void main(String args[]) { Student s1 = new Student(); Student s2 = new Student(); s1.setData(1,2); s2.setData(3,4); s1.showData(); s2.showData(); Student s3; s3=s2; s3.showData(); s2=null; s3=null; System.gc(); } } OUTPUT This object is garbage collected

Finalize () & GC() finalize() method [java.lang.Object.finalize()] The finalize() method is invoked each time before the object is garbage collected. This method can be used to perform cleanup processing. This method is defined in Object class as: protected void finalize(){}   Note: The Garbage collector of JVM collects only those objects that are created by new keyword. So if you have created any object without new, you can use finalize method to perform cleanup processing (destroying remaining objects). We can do: obj.finalize(); for the garbage collection gc() method [java.lang.System.gc()] The gc() method is used to invoke the garbage collector to perform cleanup processing. The gc() is found in System and Runtime classes. public static void gc(){}   Note: Garbage collection is performed by a daemon thread called Garbage Collector(GC). This thread calls the finalize() method before object is garbage collected.

COMMAND LINE ARGUMENTS Used to specify configuration information while launching your application. no restriction on the number of java command line arguments. You can specify any number of arguments Information is passed as Strings. They are captured into the String args of your main method class Demo{ public static void main(String args[]) { System.out.println("Argument one = "+args[0]); System.out.println("Argument two = "+args[1]); }

Another example

ARRAY OF OBJECTS public static void main(String args[]){ Account obj[] = new Account[2] ; //obj[0] = new Account(); //obj[1] = new Account(); obj[0].setData(1,2); obj[1].setData(3,4); System.out.println("For Array Element 0"); obj[0].showData(); System.out.println("For Array Element 1"); obj[1].showData(); } class Account{ int a; int b; public void setData(int c,int d){ a=c; b=d; public void showData(){ System.out.println("Value of a ="+a); System.out.println("Value of b ="+b);

THANK YOU