mirror of
https://github.com/kamranahmedse/developer-roadmap.git
synced 2026-09-24 15:00:31 +08:00
Fix example code in "Memory Management" item of PHP roadmap (#9993)
* Fix example code in "Memory Management" item of PHP roadmap * Update memory-management@bt7dK2PcOZ72B9HXPyMwL.md * Update memory-management@bt7dK2PcOZ72B9HXPyMwL.md
This commit is contained in:
@@ -2,14 +2,17 @@
|
||||
|
||||
Memory Management is a crucial part of PHP performance optimization. Efficient memory use can significantly boost the speed and reliability of your PHP applications. PHP automatically provides a garbage collector which cleans up unused memory, but understanding and managing your script's memory usage can result in better use of resources. For instance, `unset()` function can help in freeing up memory by destroying the variables that are no longer used. Here is an example:
|
||||
|
||||
$string = "This is a long string that's going to use a lot of memory!";
|
||||
echo memory_get_usage(); // Outputs: 36640
|
||||
$string = str_repeat("very long string", 10240);
|
||||
echo memory_get_usage(); // Outputs: 514576
|
||||
unset($string);
|
||||
echo memory_get_usage(); // Outputs: 36640
|
||||
|
||||
echo memory_get_usage(); // Outputs: 346672
|
||||
|
||||
In this code snippet, you'll notice that the memory used remains the same even when the `$string` variable is unset. This is because `unset()` only reduces the reference count of the variable in PHP's memory manager, and the memory will be cleared at the end of script execution. Avoiding unnecessary data storage and using inherent PHP functions, can help optimize memory management.
|
||||
In this example, memory usage decreases after the `$string` variable is unset. This occurs because `unset()` decreases the reference count, and when it reaches zero, the memory associated with the variable is freed and returned to PHP’s internal memory manager.
|
||||
|
||||
However, this memory is not necessarily returned to the operating system. Instead, it remains allocated to PHP and can be reused for subsequent operations within the script.
|
||||
|
||||
Avoiding unnecessary data storage and leveraging built-in PHP functions can help improve memory efficiency.
|
||||
|
||||
Visit the following resources to learn more:
|
||||
|
||||
- [@official@Memory Management](https://www.php.net/manual/en/features.gc.php)
|
||||
- [@official@Memory Management](https://www.php.net/manual/en/features.gc.php)
|
||||
|
||||
Reference in New Issue
Block a user