Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 27

Topics:

final keyword
and
Garbage
Collection
final keyword
• final is a non-access modifier
• Applicable to
– Variable
– Method
– Class
final –> Named Constant
• Name for a memory location that cannot be changed after
declared
• Contains a value of primitive or reference type
• Its name is a Java identifier and its value must be set on the
same line it is declared
• Declared by preceding variable name with data type and
the keyword final

public final double PI = 3.14;


public final String WEEK_DAY_1 = "Monday";
Use of final keyword
Final Variable
• Final variable
– Value can not be changed (read only)
– Must be initialized in every constructor
– Attempts to modify final are caught at compile time
– It is good practice to represent final variables in all uppercase, using
underscore to separate words.
• Final static variable
– Used for constants
– Example
final static int INCREMENT = 5;
final float THRESHOLD = 7.50;
Final Variable – Where to use
• Final variables must be used only for the values that we want to
remain constant throughout the execution of program.
Final Variable – attempt to modify (example)
Final Method
• Final method
– Method can not be overridden by subclass
– It means a sub class can call the final method of parent class
without any issues but it cannot override it.
– Private methods are implicitly final
Final Method - example
Final Class
• Final class
– Class can not be a superclass (extended)
– Prevent inheritance / polymorphism
– Methods in final class are implicitly final
• Example – Java class String is final
• Any attempt to inherit final classes will cause an error.
Final Class - example
Points to Remember
1) A constructor cannot be declared as final.
2) Local final variable must be initialized during declaration.
3) All variables declared in an interface are by default final.
4) We cannot change the value of a final variable.
5) A final method cannot be overridden.
6) A final class not be inherited.
7) If method parameters are declared final then the value of these parameters cannot
be changed.
8) It is a good practice to name final variable in all CAPS.
9) final, finally and finalize are three different terms. finally is used in
exception handling and finalize is a method that is called by JVM during
garbage collection.
Garbage Collection in Java
Memory layout
per process
virtual memory
new pages allocated physical memory
via calls to OS

heap

static data
A translation lookaside
TLB address buffer (TLB) is a memory
translation cache that stores recent
translations of virtual
memory to physical
stack addresses for faster
retrieval.
grows to preset limit
How Objects are Created in Java
• An object is created in Java by invoking the new()
operator.

• Calling the new() operator, the JVM will do the


following:
• allocate memory;
• assign fields their default values;
• run the constructor;
• a reference is returned.
How Java Reclaims Objects Memory
• Java does not provide the programmer any means to destroy objects explicitly

• The advantages are

– No dangling reference problem in Java

– Easier programming

– No memory leak problem


What is Garbage?
Garbage: unreferenced objects Ali Object

Student ali = new Student(); ail


Student khalid = new Student();
ali = khalid;
khalid

Now ali Object becomes a garbage,


It is unreferenced Object Khalid Object
What is Garbage Collection?
• What is Garbage Collection?
– Finding garbage and reclaiming memory allocated to it.

• Why Garbage Collection?


– the heap space occupied by an un-referenced object can be recycled
and made available for subsequent new objects

• When is the Garbage Collection process invoked?


– When the total memory allocated to a Java program exceeds some
threshold.

• Is a running program affected by garbage collection?


– Yes, the program suspends during garbage collection.
Memory Allocation in Java
• The JVM divided the memory into following sections.

– Heap
– Stack
– Code
– Static
Memory Allocation in Java
This division of memory is required for its effective management.
• The Heap section contains Objects (may also contain reference
variables).
• The Stack section of memory contains methods, local variables,
and reference variables.
• The code section contains your bytecode.
• The Static section contains Static data/methods.
Freeing Memory
• Some systems require user to call free when finished with memory
– C / C++
– reason for destructors in C++
• Other systems detect unused memory and reclaim it
– Garbage Collection
– this is what Java does
Garbage Collection
• Basic idea
– keep track of what memory is referenced and when it is
no longer accessible, reclaim the memory
• Example
– linked list
A Heap
Free List

...

Object 1 Object 2 Object 3

23
Mark-and-Sweep
• Basic idea
– go through all memory and mark every chunk that is
referenced
– make a second pass through memory and remove all
chunks not marked

OS 0 1 2 3

p2 = 650 p2 = 360

0 100 350 450 600

• Mark chunks 0, 1, and 3 as marked


• Place chunk 2 on the free list (turn it into a hole)
Reference Counting
• Basic idea
– give each chunk a special field that is the
number of references to chunk
– whenever a new reference is made, increment
field by 1
– whenever a reference is removed, decrement
field by 1
– when reference count goes to zero, collect
chunk
• Requires compiler support
Generational Garbage Collection
• Basic idea
– occasionally create a new area in memory
– every time an object is referenced, move it to that new area
– when the original area becomes sparse, run mark-and-sweep on it
• move surviving chunks to the new area

• This method also compacts data in new region


• Garbage collection in Java happens automatically.
System.gc() Method / class Runtime.gc() Method
• The System.gc() method runs the garbage collector.
• System.gc() is a class method.
• The Runtime.gc() method runs the garbage collector.
• Runtime.gc() is an instance method (requires an object).

You might also like