Professional Documents
Culture Documents
7 - Lec - Final Keyword
7 - Lec - Final Keyword
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
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.
– Easier programming
– 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
...
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