SlideShare a Scribd company logo
Long running
PHP
Kamil Szymanski
Did you go through that already?
❏ My script slows down with a time
❏ My script consumes extreme amount of RAM
❏ My script dies from time to time with no reason
How to fix it?
Use Java
Use Java
But really...
Get familiar with Garbage Collector
The Composer case
Generally if you create many many objects, you pretty much want to always disable GC. This is
because PHP has a hard-coded limit (compile-time) of root objects that it can track in its GC
implementation (I believe it's set to 10000 by default).
If you get close to this limit, GC kicks in. If it cannot clean-up, it will still keep trying in frequent
intervals. If you go above the limit, any new root objects are not tracked anymore, and cannot be
cleaned-up whether GC is enabled, or not.
This might also be why the memory consumption for bigger projects does not vary. If GC is enabled,
it's just not working anymore even if there is potentially something to clean-up. For smaller projects,
you might see a memory difference.
https://github.com/composer/composer/pull/3482#issuecomment-65199153
So why is my script slowing down?
When the garbage collector is turned on, the cycle-finding algorithm as described above is executed
whenever the root buffer runs full. The root buffer has a fixed size of 10,000 possible roots (although you
can alter this by changing the GC_ROOT_BUFFER_MAX_ENTRIES constant in Zend/zend_gc.c in the PHP
source code, and re-compiling PHP). When the garbage collector is turned off, the cycle-finding algorithm will
never run. However, possible roots will always be recorded in the root buffer, no matter whether the
garbage collection mechanism has been activated with this configuration setting.
If the root buffer becomes full with possible roots while the garbage collection mechanism is turned off,
further possible roots will simply not be recorded. Those possible roots that are not recorded will never be
analyzed by the algorithm. If they were part of a circular reference cycle, they would never be cleaned up
and would create a memory leak.
From http://php.net/manual/en/features.gc.collecting-cycles.php
https://www.reddit.com/r/lolphp/comments/2o1w4a/php_garbage_collector_at_its_finest/
The Garbage Collector at it’s best
The Garbage Collector at it’s best
The Garbage Collector at it’s best
Solution?
Thank you
Further reading
http://notjam.es/blog/2014/06/18/the-problems-with-long-running-php.html
http://hackingwithphp.com/18/1/11/be-wary-of-garbage-collection-part-2
https://dracony.org/php-memory-leaks-and-garbage-collection/

More Related Content

KEY
CoffeeScript presentation
PDF
Happy Programming with CoffeeScript
KEY
End to-End CoffeeScript
ODP
MongoDB and Node.js
PPTX
Tutorial sederhana netbeans & xampp
PPT
Ruby On Google App Engine 2nd Athens Ruby Me
PDF
Coffeescript: An Opinionated Introduction
PPTX
Backbone the Good Parts
CoffeeScript presentation
Happy Programming with CoffeeScript
End to-End CoffeeScript
MongoDB and Node.js
Tutorial sederhana netbeans & xampp
Ruby On Google App Engine 2nd Athens Ruby Me
Coffeescript: An Opinionated Introduction
Backbone the Good Parts

What's hot (13)

PPTX
URL Shorteners made my day
PDF
Koha Cronjobs
PDF
The Mysteries Of JavaScript-Fu (RailsConf Ediition)
ZIP
Practical project automation
PDF
Wso2 esb-rest-integration
PPT
No more script tag soup!
PDF
Block the System - building blocks in Gutenberg
ODP
Web Crawlers in Perl
PDF
Open Social Data (Jaca), Alejandro Rivero
ODP
Chef, Vagrant, and VirtualBox
PDF
Ruby and Rails for womens
PDF
Frontend Track NodeJS
URL Shorteners made my day
Koha Cronjobs
The Mysteries Of JavaScript-Fu (RailsConf Ediition)
Practical project automation
Wso2 esb-rest-integration
No more script tag soup!
Block the System - building blocks in Gutenberg
Web Crawlers in Perl
Open Social Data (Jaca), Alejandro Rivero
Chef, Vagrant, and VirtualBox
Ruby and Rails for womens
Frontend Track NodeJS
Ad

Similar to Long Running PHP by Kamil Szymanski (20)

PDF
2019 PHP Serbia - Boosting your performance with Blackfire
PPTX
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
PPT
Performance and Scalability
PDF
JCON World 2023 - Cache, but Cache Wisely.pdf
PPTX
Exploring .NET memory management (iSense)
PPTX
Low latency in java 8 by Peter Lawrey
PPTX
Low latency in java 8 v5
PDF
Scaling PHP apps
PPT
Top 10 Scalability Mistakes
PPT
Apache Con 2008 Top 10 Mistakes
PPTX
Native Code, Off-Heap Data & JSON Facet API for Solr (Heliosearch)
ODP
Low level java programming
PDF
Magento Performance Optimization 101
KEY
Concurrency in ruby
PDF
JVM Performance Tuning
PDF
2019 StartIT - Boosting your performance with Blackfire
PDF
Choosing Right Garbage Collector to Increase Efficiency of Java Memory Usage
PPTX
Improving Drupal Performances
ODP
Garbage collection
PPT
Drupal caching
2019 PHP Serbia - Boosting your performance with Blackfire
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
Performance and Scalability
JCON World 2023 - Cache, but Cache Wisely.pdf
Exploring .NET memory management (iSense)
Low latency in java 8 by Peter Lawrey
Low latency in java 8 v5
Scaling PHP apps
Top 10 Scalability Mistakes
Apache Con 2008 Top 10 Mistakes
Native Code, Off-Heap Data & JSON Facet API for Solr (Heliosearch)
Low level java programming
Magento Performance Optimization 101
Concurrency in ruby
JVM Performance Tuning
2019 StartIT - Boosting your performance with Blackfire
Choosing Right Garbage Collector to Increase Efficiency of Java Memory Usage
Improving Drupal Performances
Garbage collection
Drupal caching
Ad

Recently uploaded (20)

PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
project resource management chapter-09.pdf
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Enhancing emotion recognition model for a student engagement use case through...
PPTX
TLE Review Electricity (Electricity).pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Getting Started with Data Integration: FME Form 101
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PPTX
Tartificialntelligence_presentation.pptx
PDF
Heart disease approach using modified random forest and particle swarm optimi...
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
project resource management chapter-09.pdf
NewMind AI Weekly Chronicles - August'25-Week II
Digital-Transformation-Roadmap-for-Companies.pptx
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
A comparative analysis of optical character recognition models for extracting...
Univ-Connecticut-ChatGPT-Presentaion.pdf
Enhancing emotion recognition model for a student engagement use case through...
TLE Review Electricity (Electricity).pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Encapsulation theory and applications.pdf
Assigned Numbers - 2025 - Bluetooth® Document
Getting Started with Data Integration: FME Form 101
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
Group 1 Presentation -Planning and Decision Making .pptx
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Tartificialntelligence_presentation.pptx
Heart disease approach using modified random forest and particle swarm optimi...

Long Running PHP by Kamil Szymanski

  • 2. Did you go through that already? ❏ My script slows down with a time ❏ My script consumes extreme amount of RAM ❏ My script dies from time to time with no reason
  • 7. Get familiar with Garbage Collector
  • 8. The Composer case Generally if you create many many objects, you pretty much want to always disable GC. This is because PHP has a hard-coded limit (compile-time) of root objects that it can track in its GC implementation (I believe it's set to 10000 by default). If you get close to this limit, GC kicks in. If it cannot clean-up, it will still keep trying in frequent intervals. If you go above the limit, any new root objects are not tracked anymore, and cannot be cleaned-up whether GC is enabled, or not. This might also be why the memory consumption for bigger projects does not vary. If GC is enabled, it's just not working anymore even if there is potentially something to clean-up. For smaller projects, you might see a memory difference. https://github.com/composer/composer/pull/3482#issuecomment-65199153
  • 9. So why is my script slowing down? When the garbage collector is turned on, the cycle-finding algorithm as described above is executed whenever the root buffer runs full. The root buffer has a fixed size of 10,000 possible roots (although you can alter this by changing the GC_ROOT_BUFFER_MAX_ENTRIES constant in Zend/zend_gc.c in the PHP source code, and re-compiling PHP). When the garbage collector is turned off, the cycle-finding algorithm will never run. However, possible roots will always be recorded in the root buffer, no matter whether the garbage collection mechanism has been activated with this configuration setting. If the root buffer becomes full with possible roots while the garbage collection mechanism is turned off, further possible roots will simply not be recorded. Those possible roots that are not recorded will never be analyzed by the algorithm. If they were part of a circular reference cycle, they would never be cleaned up and would create a memory leak. From http://php.net/manual/en/features.gc.collecting-cycles.php https://www.reddit.com/r/lolphp/comments/2o1w4a/php_garbage_collector_at_its_finest/
  • 10. The Garbage Collector at it’s best
  • 11. The Garbage Collector at it’s best
  • 12. The Garbage Collector at it’s best