Stack Heap Lazy instantiation Flashcards

1
Q

When is stack and heap used

A

Stack memory stores the active methods and their local variables.
Heap memory stores all created objects

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

What is garbage collection

A

Items in the Heap which are no longer referenced from the stack are eligible for garbage collection.
Java offers garbage collection, i.e., the garbage collector runs in the background on its own thread.

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

2 errors related to memory

A

Out of memory:
if heap memory is full, it throws java.lang.OutOfMemoryError.
Stack overflow:
when stack memory is full, Java runtime throws java.lang.StackOverFlowError.
Often this is due to infinite (or very high numbered) recursion, where each frame in the recursion has some local memory requirements.

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

Caching data and lazy instantiation

A

Caching data is to prevent wasteful re-computation.
Lazy instantiation is to compute only the things that we
need, when we need them, but leveraging caching to not waste this computation.
Lazy initialization is particularly useful for GUIs which take a long time to construct.

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