Memory Management Flashcards
STACK
stack stores all methods and local variables (including object references and primitives)
primitives allocates less memory thus are faster to work with compared to objects
When you run a program, different layers will be created in stack and they will be executed in the order of program
-(Last in, first out)
Mutable objects are stored here
- StringBuilder
- StringBuffer
HEAP
Heap stores all objects and their instance variables
STRING POOL area stores String values and values get reassigned in the String Pool but do not get reassigned in the Heap.
Heap is where objects (instances) are stored
- Accessing object is slower compared to accessing a local variable
- Instance variables are stored together with their objects in the heap
String Pool
located in the HEAP
STRING POOL area stores String values and values get reassigned in the String Pool not the Heap.
2 ways to create String
1. String s1 = "value"; -> String Pool 2. String s2 = new String("value"); -> Heap
Strings created w/o new keyword stored-> String Pool
The reason of having String Pool is to save memory
STRING POOL area stores String values and values get reassigned in the String Pool but do not get reassigned in the Heap.
== relational operator and equals() method
When == is used with reference types, it compares their location in the memory and each object created with new keyword will have a unique location.
equals() is used to check 2 objects of same data type has same value or not and return a boolean as true or false
GARBAGE COLLECTION
Garbage means unreferenced objects (objects that lost their references)
Garbage Collection is a process of destroying objects that lost references
Garbage Collection runs automatically (implicitly) for better memory management in Java
- NOTE: Garbage Collection applies only to reference types data (objects)
- NOTE: Although garbage collection happens implicitly, we can do explicitly as well by calling System.gc() method or runtime.getRuntime().gc() methods
HOW TO EXPLICITLY PERFORM GARBAGE COLLECTION
- System.gc() method
2. Runtime.getRuntime().gc() methods
FINALIZE() METHOD
finalize() method can be used to run a block of code before object reference is garbage collected
Purpose of using this method is to do proper clean up before removing the object
By default, finalize() method is empty. However, you can write your own finalize method to take certain action by overriding it
This method is located in Object class and can be overridden wherever it is needed
Garbage Collection
DEFINE
In our programs some object loose reference and they need to be cleared from the heap and this process happens implicitly
Stack and Heap
DEFINE
These are terms used for Java Memory management
- Heap stores all objects and their instance variables while stack stores all methods and local variables(including object references)
- We also have a special area in the heap to store strings (String Pool)
What is finalize() method?
This method belongs to Object class and it is empty
- Some classes may override this and provide a body
- This body will be executed whenever an object of the class is garbage collected
- Aim to override this method could be running a block of code when object is garbage collected
What final and finalize()?
final is a non-access modifier ued with instance variables, methods and classes
finalize() is method related to garbage collection
What is an immutable String
String -> immutable
immutable: cannot be changed
String name = "John"; name = "Johnathan"; name = "John"; name = "Abe"; main(String[] args){ String city = "Chicago"; String address = "Chicago"; }
it is being replaced with a new location in the pool and not being changed
What is a mutable String
StringBuilder - StringBuffer -> mutable mutable: Their value can be changed "Johnathan" StringBuilder vs StringBuffer StringBuffer is thread-safe! Thread-safe = synchronized StringBuffer is slow compared to StringBuilder
The value is being changed in the Stack