Laravel-Excel icon indicating copy to clipboard operation
Laravel-Excel copied to clipboard

[Bug]: Run out of memory with certain excel files and toArray

Open manstie opened this issue 2 years ago • 4 comments

Is the bug applicable and reproducable to the latest version of the package and hasn't it been reported before?

  • [X] Yes, it's still reproducable

What version of Laravel Excel are you using?

3.1.40

What version of Laravel are you using?

8.83.16

What version of PHP are you using?

7.4.27

Describe your issue

See related issue on PhpSpreadsheet

When I use Excel::toArray on an excel spreadsheet which has every cell formatted as Arial / 10, the method reads every column from A to WVI even though there are only two columns of data. This causes a massive runtime and eventually an exhaustion of memory from more than ~100 rows.

How can the issue be reproduced?

Send this file in form-data to a laravel endpoint:

Bad File.xlsx

use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class TestController extends Controller
{
    public function import(Request $request)
    {
        $file = $request->file('file');
        $array = Excel::toArray(null, $file);
        dd($array);
    }
}

produces

Symfony\Component\ErrorHandler\Error\FatalError: Allowed memory size of 536870912 bytes exhausted (tried to allocate 83886080 bytes) in file /vendor/maatwebsite/excel/src/Cache/MemoryCache.php on line 97

What should be the expected behaviour?

Should be able to import the data from A1:B201 hastily and without any memory issues.

manstie avatar Jun 21 '22 02:06 manstie

You could try WithColumnLimit.

Other than that there isn't really anything we can do in this package, as the toArray logic is from PhpSpreadsheet

patrickbrouwers avatar Jun 21 '22 10:06 patrickbrouwers

WithQuery is too will not help. Use https://github.com/rap2hpoutre/fast-excel you can use chunk for export and import

yakhyomus avatar Jun 21 '22 13:06 yakhyomus

Could you not choose to use rangeToArray instead as stated in this issue?

manstie avatar Jun 22 '22 12:06 manstie

I have similar problem with file approximately 300K rows, 5MB and import to collection, even I chunk read the whole collection have almost 3GB in RAM nazwiska-meskie-w-polsce-stan-na-2019-04-15.xlsx .

kerog avatar Jul 13 '22 21:07 kerog

This bug report has been automatically closed because it has not had recent activity. If this is still an active bug, please comment to reopen. Thank you for your contributions.

stale[bot] avatar Sep 12 '22 23:09 stale[bot]