Class method object

24
Garbage Collection Minal Maniar

Transcript of Class method object

Garbage Collection

Minal Maniar

Automatically

Automatically

Automatic garbage collection

The process of looking at heap memory,

identifying which objects are in use and

which are not, and deleting the unused

objects.

Depending upon heap size , can take plenty

of time

Automatically

Step1: Marking

Automatically

Step 2: Normal Deletion

Automatically

Step 3: Deletion with Compacting

Automatically

JVM Generations

Automatically

The developer may choose among several garbage collector algorithms

STW Phase

Available Collectors Serial Collector

Parallel Collector

Concurrent Mark Sweep

(CMS) Collector

Garbage First G1 Collector

(G1GC)

http://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/

Automatically

What new features added in Java8 G1GC? String deduplication.

Removing the permanent gen part of the heap

What is changing in Java9? What is staying?

OpenJDK 8 has several Garbage Collector

algorithms, such as Parallel GC, CMS and G1

Default one is Parallel GC

G1 to be the default garbage collector in Java 9

Google uses a modified CMS Collector

Automatically

What new features added in Java8 G1GC? String deduplication.

Removing the permanent gen part of the heap

What is changing in Java9? What is staying?

OpenJDK 8 has several Garbage Collector

algorithms, such as Parallel GC, CMS and G1

Default one is Parallel GC

G1 to be the default garbage collector in Java 9

Google uses a modified CMS Collector

Automatically

http://blog.takipi.com/garbage-collectors-

serial-vs-parallel-vs-cms-vs-the-g1-and-

whats-new-in-java-8/

http://www.oracle.com/webfolder/technet

work/tutorials/obe/java/gc01/index.html

https://docs.oracle.com/javase/8/docs/tech

notes/guides/vm/gctuning/collectors.html

Introduction to Java Programming,

Comprehensive Version (10th Edition), Y. Daniel

Liang

Class Objects and

Methods

Minal Maniar

Automatically

Automatically

Instance variables belong to the instances

and have memory storage independent of

one another.

Static variables are shared by all the

instances of the same class.

Refer CircleWithStatic.java for reference

Automatically

Automatically

Automatically

It is a common design error

to define an instance

method that should have

been defined as static.

For example, the method

factorial(int n) should be

defined as static,

because it is independent

of any specific instance.

Automatically

So what are the advantages of static blocks?

If you’re loading drivers and other items into the namespace.

For ex: Class class has a static block where it registers the natives.

If you need to do computation in order to initialize your static variables, you can declare a static block which gets executed exactly once, when the class is first loaded.

Security related issues or logging related tasks

Limitations for static blocks

There is a limitation of JVM that a static initializer block should not exceed 64K.

You cannot use this keyword since there is no instance.

You shouldn’t try to access super since there is no such a thing for static blocks.

You should not return anything from this block.

Static blocks make testing a nightmare.

Automatically

public class Test {

public static void main(String[] args) {

CircleWithPrivateData c = new CircleWithPrivateData(5.0);

printCircle(c);

}

public static void printCircle(CircleWithPrivateData c1) {

System.out.println("The area of the circle of radius "

+ c1.getRadius() + " is " + c1.getArea());

}

}

Automatically

An inner class, or nested class, is a class defined within the

scope of another class.

Inner classes are useful for defining handler classes.

public class My_class {

public static void main(String args[]) {

// Instantiating the outer class

Outer_Demo o = new Outer_Demo();

// Accessing the display_Inner() method

o.display_Inner();

}

}

class Outer_Demo {

int num;

private class Inner_Demo {

public void print() {

System.out.println("This is an inner class");

}

}

// Accessing he inner class

void display_Inner() {

Inner_Demo inner = new Inner_Demo();

inner.print();

}

}

Automatically

Automatically

To combine dependent classes into a primaryclass. This reduces the number of sourcefiles. It also makes class files easy toorganize since they are all named with theprimary class as the prefix.For example, rather than creating the two source

files Test.java and A.java as shown, you can mergeclass A into class Test and create just one sourcefile, Test.java

The resulting class files are Test.class andTest$A.class.

Another practical use of inner classes is to avoidclass-naming conflicts.

Automatically

An inner class is compiled into a class named OuterClassName$InnerClassNameclass. For example, the inner class A in Test is compiled into Test$A.class

An inner class can reference the data and the methods defined in the outer class in which it nests, so you need not pass the reference of an object of the outer class to the constructor of the inner class. For this reason, inner classes can make programs simple and concise.

An inner class can be defined with a visibility modifier subject to the same visibility rules applied to a member of the class.

An inner class can be defined as static. A static inner class can be accessed using the outer class name. A static inner class cannot access nonstatic members of the outer class.

Objects of an inner class are often created in the outer class. But you can also create an object of an inner class from another class. If the inner class is nonstatic, you must first create an instance of the outer class, then use the following syntax to create an object for the inner class:

OuterClass.InnerClass innerObject= outerObject.new InnerClass();

If the inner class is static, use the following syntax to create an object for it OuterClass.InnerClassinnerObject = new OuterClass.InnerClass();

Automatically

http://www.geeksforgeeks.org/g-fact-79/

http://beginnersbook.com/2013/04/java-static-

class-block-methods-variables/

Introduction to Java Programming,

Comprehensive Version (10th Edition), Y. Daniel

Liang