Memory Management Flashcards

1
Q

STACK

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

HEAP

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

String Pool

located in the HEAP

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

== relational operator and equals() method

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

GARBAGE COLLECTION

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

HOW TO EXPLICITLY PERFORM GARBAGE COLLECTION

A
  1. System.gc() method

2. Runtime.getRuntime().gc() methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

FINALIZE() METHOD

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Garbage Collection

DEFINE

A

In our programs some object loose reference and they need to be cleared from the heap and this process happens implicitly

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Stack and Heap

DEFINE

A

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is finalize() method?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What final and finalize()?

A

final is a non-access modifier ued with instance variables, methods and classes
finalize() is method related to garbage collection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is an immutable String

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a mutable String

A
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly