PHP Array Memory Usage -


i have tab-delimited file need in array (so can values in it). had thought iterate through lines , push exploded values array, so:

error_reporting(e_all); ini_set("memory_limit","20m");  $holder = array(); $csv = array(); $lines = file('path\\to\\file\\2014_04_05_08_48.txt');  ($a=0; $a<count($lines); $a++) {     $csv = '';     $csv = $lines[$a];     $csv = explode("\t", $csv);     array_splice($csv, 10);     array_push($holder, $csv); }  print_r($holder); 

this file 11000 lines, 170 fields/line (output of current inventory pos system). file less 6mb. kept getting errors memory limits being exceeded. looking here on found increasing memory limit way of covering memory leak/poor memory usage.

i got work setting memory limit 20m , stripping each array after 10th value.

my question why need such high memory limit file contents array? more 3x size seems lot. doing wrong?

each element in array variable potentially 48 bytes of overhead, , each element in holder array has 76 bytes of overhead

so 170 fields per line have overhead of 170x48=8160 bytes; , have 11000 lines, that's 89,760,000 bytes total if loaded memory rather in blocks of 10 rows. each line array, has 76 bytes of overhead (11000x76 = 836,000 bytes) plus further 76 bytes holder array..... , doesn't include actual size of data in fields

edit

you're building $holder 10 fields in csv line; still equates 10x11000x48 overhead data elements 5,280,000 bytes (plus data in each element), plus 11000x76 = 836,000 bytes plus $holder array @ 78 bytes giving total of 6,116,078 (about 6mb)... 6mb loading entirety of $file memory leaving 8mb 20mb

add fact php takes memory, plus code of script, plus other variables in script, , you're rapidly approaching 20mb limit


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -