How to free memory after array in PHP? unset and = null seems to not working -
i ve got 16 mb size csv file , try parse it, , stuff, script run out of memory after time. realize code generate around 200 mb of used space , unset not working.
$countrows = 1; var_dump("3. ".memory_get_usage()." begindiff: ".(memory_get_usage() - $this->startingmemory)); while(($row = fgetcsv($fp, 300000, ';', '"')) !== false) { if ($row == '') continue; if($firstrow == true) { foreach($row $k => $v) { $this->columnmapping[$k] = trim(mb_strtolower($v)); } $firstrow = false; continue; }else { foreach($row $k => $v) { $row[$this->columnmapping[$k]] = $v; unset($row[$k]); } } ... //$this->theircategoriestoproducts[$row['kategorie']][]['kodproduktu'] = $row['kodproduktu']; $this->theircategoriestoproducts[$row['kategorie']][] = $row; } var_dump("3,5. ".memory_get_usage()." begindiff: ".(memory_get_usage() - $this->startingmemory)); ... var_dump("7. - before unset total: ".memory_get_usage()." begindiff: ".(memory_get_usage() - $this->startingmemory)); unset($this->theircategoriestoproducts); var_dump("8. - after unset total: ".memory_get_usage()." begindiff: ".(memory_get_usage() - $this->startingmemory));die;
generating output:
string '3. 72417440 begindiff: 34730040' (length=31) string '3,5. 292748528 begindiff: 255061136' (length=36) string '7. - before unset total: 299039360 begindiff: 261351984' (length=55) string '8. - after unset total: 297364432 begindiff: 259677056' (length=54)
with setting variable equals null output similar. switching comments between 2 lines
$this->theircategoriestoproducts[$row['kategorie']][]['kodproduktu'] = $row['kodproduktu']; //$this->theircategoriestoproducts[$row['kategorie']][] = $row;
will output:
string '3. 72417784 begindiff: 34730040' (length=31) string '3,5. 81081984 begindiff: 43394248' (length=34) string '7. - before unset total: 87256544 begindiff: 49568824' (length=53) string '8. - after unset total: 85581520 begindiff: 47893800' (length=52)
so 200 mb of "lost" memory (almost half of dedicated).
recursive function unseting parts of arrays eat more memory, able free crashed also.
in script never used array & there should no references other variables.
file closing right after 3.5 dump.
any other ideas, how unset array?
as of php > 5.3 there garbage collection mechanisms available, theoretically think of example in docs
//memory cleanup long-running scripts. gc_enable(); // enable garbage collector var_dump(gc_enabled()); // true var_dump(gc_collect_cycles()); // # of elements cleaned gc_disable(); // disable garbage collector
but unfortunately, in case have bear in mind (according can trigger php garbage collection happen automatically if have circular references?) garbage collector "will not run, example, when memory limit hit. result, script can still abort when hitting memory limit because php dumb collect cycles in case!".
in end, can try using gc, possibly won't solve problem.
so, else there try? try splitting master data array import smaller chunks , import them sequentially 1 after another. fetch chunks in loop same variable , loop through process records.
Comments
Post a Comment