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