GC Flashcards

1
Q

Why we have GC?

A

1) Auto memory management(allocate/free) - less memory leak

2) Executed by a Daemon thread ‘Gabbage Collector’

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

GC supposed to work on 2 hypothesis?

A

1) Most obj will soon become unreachable - Most objects are exists in a method scope
2) ref from ‘old’ obj to ‘young’ obj only exists in small numbers
Object exists long in the app does not usually reference to newly created objs: a cache needs clean up

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

When objects get GCed?

A

1) Obj not referenced from anywhere else

2) isolation of island(A -> B. B-> C. C->A)

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

System.gc()

A

1) not force GC to happen

2) just a hit to trigger a GC

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

GC steps

A

1) MARK - from root node(main()), walk through the object graph, marks objects reachable as live
2) SWEEP - delete unreachable objs
3) COMPACTING- compacting the memory, moveing love objs, making the allocation contiguous rather than fragmented(slow).

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

How heap is divided for GC?

A

1) Young Generation
Eden Space - new objs are created, when full, a minor GC triggered
Survivor 1 - all survived Objs from minor GC + All objs in Survivor 2, swap with S2
Survivor 2
2) Old Generation
after survive N minor GC, objs moves to Old generation

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

Minor Vs Major GC?

A

1) Minor GC runs in young generation
2) Majoy GC runs across the heap
3) They all stop the word for some time

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

Process of Minor GC?

A

Every time copy all live obj to the empty S

1) New Objs Allocated in Eden
2) Eden is full, minor GC, move live obj to S1
3) Eden is full again, minor GC, move live to S2, also copy all S1 live obj to S2(avoid compacting)
4) Eden is full, minor GC, copy all live obj to S1, all S2 live obj to S1, swap s1, s2

when any obj have survive N GC, move them to the old generation

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

Deep Copy Vs shallow Copy?

A

1) Shallow Copy:
primitive types: values are copies
Objs: only reference are copied - any change made to the objs will impact the original obj

2) Deep Copy
clone object and original object will be 100% disjoint.
Change made to the cloned one will not impact the original one

3) java.lang.Object clone() method - provide a shallow copy. Deep copy need to override Clone() method

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

Process of Major GC?

A

1) Triggered when old Gen is almost full
2) Runs across both young/old - whole heap
3) Time consuming, cause app halt

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

JVM performance measures

A

Responsiveness/Latency
How long does it take to handle a request

Throughput
Amount of work done by the app over some time

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

Different types of GC?

A
  1. Serial GC
    - Runs in single thread, STW for whole process
  2. Concurrent GC
    - Performs GC along with App thread
    - only STW on MARK/RE-MARK, SWEEP/COMPATING can happen concurrently
    - Use when there are high amounts of CPU/memory, app demands short pauses
    - CMS, G1(java7)
  3. Parallel GC
    - multi-threaded GC process
    - not run concurrently with App, SWT for the whole process
    - Use when there is low amounts of cpu/memory, app demands high throughput and can withstand pauses
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When G1 is the new replacement of CMS?

A

Divides heap in to small regions, region with the most garbage will get collected first

  1. more predictable for GC pauses
  2. Parallel and concurrent together
  3. low pauses with fragmentation
  4. Better heap utilisation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

JVM Parameters for GC

A
  • XX:+UseSerialGC: young/old use serial GC
  • XX:+UseParallelGC: young - parallel, old - serial
  • XX:+UseParNewGC: only young parallel
  • XX:+UseParallelOldGC: both young/old parallel
  • XX:+UseConcMarkSweepGC: young useParNewGC, old use CMS GC
  • XX:+UseG1GC

default: parallel 1.6, G1 since 1.7

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

finalize()

A

will be called before GC only once per obj

not gauranteed to be GCed immediately.

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