Repository navigation
PHP fix 6/10: Allowed memory size exhausted — stream a CSV instead of loading it all #16
vistabylara-dev
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
نفاد الذاكرة قد يحدث عندما تُحمَّل ملفات كبيرة دفعة واحدة. المثال يقرأ ملف CSV سطراً بسطر دون الاحتفاظ بكل الصفوف في الذاكرة.
Free code series · Topic 13/50 · PHP fix 6/10
The problem
A PHP import loads an entire CSV and then creates several copies in arrays. Streaming can reduce that particular source of memory pressure. It does not fix every memory leak or an oversized individual row.
Where this belongs in Shopify
PHP runs in an external app, integration service, or server you control. Shopify themes use Liquid, HTML, CSS, and JavaScript; do not paste this PHP into a theme section or Custom Liquid block. If a third-party app owns the failing server, share a sanitized error with that app's support team.
Placement and free suggested code
Save as csv-demo.php. It creates a tiny in-memory fixture for demonstration. In your private background importer, adapt the stream to an approved local file and validate its schema before any writes.
csv-demo.php
Verify before using it
The fixture should print Rows read: 2. In staging, test realistic file sizes, malformed rows, and encoding; measure peak memory. This sample validates shape only, not SKU existence or quantity. Establish file and record-size limits because a single huge CSV record can still consume memory.
What was checked for this guide: The demo ran in a PHP WebAssembly CLI runtime. Local fixture checks passed; this is not a test of your host, store, production workflow, or every failure mode.
Precautions and rollback
Do not set memory_limit to unlimited as a blanket fix. A database query buffered before this loop or an accumulated result array can still exhaust memory. For imports, validate rows, use bounded batches and checkpoints, and make retry behavior safe before writing stock or prices.
Back up or commit the existing application before adapting anything. Test on a development copy with dummy data. Restore the previous application version if behavior regresses; reverting code does not undo database writes or external actions. These are educational suggestions, not a universal drop-in repair.
Check the ecommerce workflow
After adapting a fix, verify the affected operation from input to its intended result in your test environment. Check failure handling as well as success. Do not mark a sale or analytics purchase event merely because a PHP script finished. Payment/order state and analytics reporting need their own checks, and repeated requests must not duplicate side effects.
Official references
Share your error and join the free discussion
Comment with the exact error text, PHP version, whether this is your own app or a third-party app, and a short sanitized code sample. Remove passwords, tokens, customer details, and private server paths. What happened just before the error?
Need free Shopify guidance or a free consultation? Call +971 50 799 4292 or message Dr Lara on WhatsApp.
Free guidance. No commitment. No package to sell.
Dr Lara · Dubai, UAE
All reactions