Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Reference Counting in PHP | Reference Counting and Copy on Write
PHP Memory Management

bookReference Counting in PHP

Note
Definition

Reference counting is PHP's memory management mechanism that tracks how many variables point to the same value in memory. When the count reaches zero, the value is automatically removed from memory.

PHP uses reference counting to decide when a value can be safely freed. Each assignment increases the reference count, and each unset or scope exit decreases it. When no variables reference a value anymore, PHP releases the memory.

Reference counting is especially important when using the & operator. Variables assigned by reference share the same memory location, so the reference count reflects how many variables point to that value. When a reference is removed and the count drops to zero, PHP’s memory manager frees the memory.

Next, you will see how reference counts change as variables are assigned, referenced, and unset in a simple PHP script.

reference_counting.php

reference_counting.php

copy
123456789101112131415
<?php // Demonstrating reference counting in PHP $a = "hello"; // $a points to "hello", reference count = 1 $b = $a; // $b is a copy, so now "hello" has 2 references: $a and $b (reference count = 2) $c = &$a; // $c is a reference to $a, so now $a and $c point to the same value (reference count for $a's value = 2, $b is still a separate copy) unset($a); // $a is unset, but $c still points to the value, so reference count decreases by 1 unset($c); // $c is unset, now no variables point to the original value, so reference count = 0 and memory is freed unset($b); // $b is unset, its copy of "hello" is no longer needed, so reference count = 0 for $b's value and memory is freed ?>
question mark

Which of the following best describes how reference counting helps PHP manage memory?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 1

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookReference Counting in PHP

Scorri per mostrare il menu

Note
Definition

Reference counting is PHP's memory management mechanism that tracks how many variables point to the same value in memory. When the count reaches zero, the value is automatically removed from memory.

PHP uses reference counting to decide when a value can be safely freed. Each assignment increases the reference count, and each unset or scope exit decreases it. When no variables reference a value anymore, PHP releases the memory.

Reference counting is especially important when using the & operator. Variables assigned by reference share the same memory location, so the reference count reflects how many variables point to that value. When a reference is removed and the count drops to zero, PHP’s memory manager frees the memory.

Next, you will see how reference counts change as variables are assigned, referenced, and unset in a simple PHP script.

reference_counting.php

reference_counting.php

copy
123456789101112131415
<?php // Demonstrating reference counting in PHP $a = "hello"; // $a points to "hello", reference count = 1 $b = $a; // $b is a copy, so now "hello" has 2 references: $a and $b (reference count = 2) $c = &$a; // $c is a reference to $a, so now $a and $c point to the same value (reference count for $a's value = 2, $b is still a separate copy) unset($a); // $a is unset, but $c still points to the value, so reference count decreases by 1 unset($c); // $c is unset, now no variables point to the original value, so reference count = 0 and memory is freed unset($b); // $b is unset, its copy of "hello" is no longer needed, so reference count = 0 for $b's value and memory is freed ?>
question mark

Which of the following best describes how reference counting helps PHP manage memory?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 1
some-alt