Laravel使用Laravel Excel上传Excel时内存不足加载异常
Hey there, let's dig into this frustrating issue you're facing with Laravel Excel. Even though your 70KB Excel file seems tiny and you've cranked PHP's memory limit up to 8GB, that endless page load and memory exhaustion error are definitely head-scratchers. Let's break down common fixes tailored to multi-sheet Excel files in Laravel Excel:
1. Fix Memory Leaks from Uncontrolled Multi-Sheet Loading
Even small files can hide thousands of rows across multiple sheets, and Laravel Excel might be loading all that data into memory at once without cleanup. Try processing sheets and rows in chunks, then manually freeing up memory:
// For multi-sheet files, process each sheet individually with chunking $reader = Excel::load($uploadedFile->getPathname()); foreach ($reader->getSheets() as $sheet) { // Process 1000 rows at a time to avoid memory overload $sheet->chunk(1000, function($rows) { foreach ($rows as $row) { // Handle your row processing logic here (e.g., save to DB) } // Manually clear the current batch from memory unset($rows); gc_collect_cycles(); // Force garbage collection }); // Clean up after processing each sheet unset($sheet); gc_collect_cycles(); } unset($reader); gc_collect_cycles();
2. Verify Your PHP Memory Limit is Actually Applied
Sometimes modifying php.ini doesn't take effect immediately, or you might have edited the wrong config file (e.g., CLI vs. web server). Add this line somewhere in your upload logic to confirm:
dd(ini_get('memory_limit')); // Check if this outputs 8192M or your intended limit
If it doesn't match, restart your web server (Nginx/Apache) and double-check that you edited the correct php.ini file (use phpinfo() to find the loaded config path).
3. Disable Unnecessary Excel Features
If your file contains formulas, conditional formatting, or other non-data elements, Laravel Excel might be wasting memory processing them. Disable formula calculation explicitly:
Excel::load($uploadedFile->getPathname(), function($reader) { $reader->setCalculateFormulas(false); // Skip formula calculations });
Also, make sure you're using the latest stable version of Laravel Excel (v3.x+), as older versions had known memory leaks with multi-sheet files.
4. Audit Your Data Processing Logic
Even if Excel loading is efficient, your post-processing might be leaking memory:
- Avoid using
Model::create()in a loop for every row. Instead, collect rows into an array and useModel::insert($dataArray)for bulk inserts. - Remove any debug logs that dump full row data (e.g.,
Log::debug($row)), as this can bloat memory over time.
5. Pinpoint the Exact Memory Hogs
Add memory tracking logs to see where memory spikes happen:
Log::info('Initial memory usage: ' . round(memory_get_usage(true)/1024/1024, 2) . 'MB'); // After loading the first sheet Log::info('After loading sheet 1: ' . round(memory_get_usage(true)/1024/1024, 2) . 'MB'); // After processing the first sheet Log::info('After cleaning up sheet 1: ' . round(memory_get_usage(true)/1024/1024, 2) . 'MB');
This will help you spot if memory isn't being freed after processing each sheet or batch.
内容的提问来源于stack exchange,提问作者user7747472

