Garbage Collection Flashcards

1
Q

A PHP variable is stored in a container called…

A

…zval

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

What does a zval container contain?

A
  • the variable’s type
  • the variable’s value
  • is_ref: a boolean value indicating whether or not the variable is part of a “reference set”
  • refcount: an internal reference counting mechanism to optimize memory usage
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does refcount contain?

A

It contains how many variable names (also called “symbols”) point to this one zval container. All symbols are stored in a symbol table, of which there is one per scope. There is a scope for the main script, as well as one for every function or method.

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

When is a zval container created?

A

When a new variable is created with a constant value.

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

What is the is_ref bit set to by default?

A

FALSE. Note that if refcount is 1, is_ref is always FALSE.

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

How can you display zval information

A

If you have Xdebug installed, you can display it with xdebug_debug_zval.

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

When are variable containers destroyed?

A

When the refcount reaches 0.

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

When does the refcount get decreased by 1?

A

When any symbol linked to the variable container leaves the scope (e.g. when the function ends), or when unset() is called on a symbol.

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

How many zval containers are created from a 2-element array?

A
  1. One for the array, and one for each element in the array.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the zend.enable_gc php.ini setting?

A

It enables or disables the circular reference garbage collector. It’s enabled by default.

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

How can the garbage collecting mechanism be turned on and off?

A

By calling gc_enable() or gc_disable().

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

What does gc_collect_cycles() do?

A

It forces the collection of cycles even if the possible root buffer is not full yet. It returns how many cycles were collected by the algorithm.

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

Why is it wise to call gc_collect_cycles() just before you call gc_disable()?

A

To free up the memory that could be lost through possible roots that are already recorded in the root buffer. This then leaves an empty buffer so that there is more space for storing possible roots while the cycle collecting mechanism is turned off.

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

What is a memory leak?

A

It’s when memory that is no longer needed is not released.

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

What are the two performance considerations of garbage collection?

A
  1. Reduced memory usage.

2. Run-time delay when the garbage collection mechanism performs its memory cleanups.

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

What is the whole purpose for implementing the garbage collection mechanism?

A

To reduce memory usage by cleaning up the circular-referenced variables as soon as the prerequisites are fulfilled.

17
Q

When does garbage collection occur in PHP?

A

As soon as the root-buffer is full, or when gc_collect_cycles() is called.