• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2225
  • Last Modified:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 11 bytes) in

file_get_contents(BIG_FILE);

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 11 bytes) in


should I change a variable in php.ini
0
rgb192
Asked:
rgb192
  • 3
  • 3
  • 3
2 Solutions
 
Dave BaldwinFixer of ProblemsCommented:
That depends.  How big a file are you trying to get?  Note that it has to fit in memory along with all the PHP code.
0
 
rgb192Author Commented:
46,000 kb

I am reading the skype sql file
0
 
Dave BaldwinFixer of ProblemsCommented:
Change 'memory_limit' in 'php.ini' from 128M to 256M.  You are probably reading the file in Ok but processing such a large file will take more memory.  You may have to restart Apache.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Dave BaldwinFixer of ProblemsCommented:
Or you might be able to do:

ini_set('memory_limit','256M');
0
 
Ray PaseurCommented:
The line numbers matter here.  What line of the script fails?  What is the PHP code on that line?  

I am fairly certain that the 46 megabyte file is getting processed somehow and in PHP standard (not OOP) notation, PHP often makes copies of the variables to pass the copies to the functions.  PHP arrays are also amazingly large when compared to the string data.  You might want to get familiar with memory_get_usage()
0
 
rgb192Author Commented:
Sorry about not providing code.


Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 11 bytes) in C:\wamp\www\test\readingsql.php on line 3

note: sql-file has no extension.  
Code runs on nusphere phpED ide, but not wamp

<?php
$filename = "C:/Users/Acer/AppData/Roaming/Skype/respondto/sql-file";
$lines = explode("\n", file_get_contents($filename));
foreach($lines as $l) {
   if (substr($l, 0, 13) == 'CREATE TABLE ') {
      echo $l . '<br>';
   }
}

Open in new window

0
 
Ray PaseurCommented:
Please see AntiPractice #9, in play here because you have two functions on line 3, so you cannot readily isolate the cause of the failure.

$lines = explode("\n", file_get_contents($filename));

But that aside, the cause of the failure is data proliferation caused by internal designs in PHP.  Instead of that, I recommend that you try this:

$str = file_get_contents($filename);

I expect that if your data set is 46 megabytes you will be able to get it into a string variable.

Also, I would recommend that you revisit this question.  I am sure that @JulianH meant well, but that is a technically incompetent solution and you should be using the regular expression on a string variable instead of trying to use strpos() on elements of an array.  There is sometimes a bit of "deep background" thinking involved in application design, and when I saw the sample data in that question, I knew instantly that any design using an array would run out of memory very, very soon.
0
 
Ray PaseurCommented:
Here is a little script illustrating the rough-order-of-magnitude of the problem.
See: http://www.laprbass.com/RAY_memory_get_usage.php

<?php // RAY_memory_get_usage.php
error_reporting(E_ALL);
echo '<pre>';


// SHOW HOW MUCH DATA STORAGE PHP USES FOR ARRAYS
// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28309789.html#a39695536


// GET THE INITIAL STATE OF THE MEMORY CONSUMPTION
$base = memory_get_usage();

// DEFINE A STRING OF DATA
$str = <<<EOD
Mary
Had
A
Little
Lamb
Its
Fleece
Was
White
As
Snow
EOD;

// GET THE MEMORY CONSUMPTION AFTER DEFINING THE STRING
$plus = memory_get_usage();

// CONVERT THE STRING TO AN ARRAY AND GET THE MEMORY CONSUMPTION
$arry = explode(PHP_EOL, $str);
$post = memory_get_usage();

// SHOW THE RAW DATA
echo PHP_EOL . 'STRING strlen(): ' . strlen($str) . ' BYTES';
echo PHP_EOL . 'ARRAY count(): ' . count($arry) . ' ELEMENTS';
echo PHP_EOL;
echo PHP_EOL . 'THE BASE MEMORY USAGE IS: ' . number_format($base) . ' BYTES';
echo PHP_EOL . 'AFTER SETTING THE STRING: ' . number_format($plus) . ' BYTES';
echo PHP_EOL . 'AFTER CREATING THE ARRAY: ' . number_format($post) . ' BYTES';
echo PHP_EOL;

// MAKE SOME CALCULATIONS
$str_increase = number_format($plus - $base);
$str_increase = str_pad($str_increase, 5, ' ', STR_PAD_LEFT);
$arr_increase = number_format($post - $plus);
$arr_ratio    = number_format( ($post - $plus) / ($plus - $base) , 2);

echo PHP_EOL . "MEMORY USAGE: $str_increase FOR THE STRING";
echo PHP_EOL . "MEMORY USAGE: $arr_increase FOR THE ARRAY";
echo PHP_EOL . "THE ARRAY USES <b>$arr_ratio</b> TIMES AS MUCH MEMORY AS THE STRING";
echo PHP_EOL;

// SHOW THE DATA SET
echo PHP_EOL . 'THE STRING:';
echo PHP_EOL . $str;
echo PHP_EOL;
echo PHP_EOL . 'THE ARRAY:';
echo PHP_EOL;
print_r($arry);

Open in new window

0
 
rgb192Author Commented:
Thanks for ini change and code explaining size.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now