Solved

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

Posted on 2013-12-03
9
2,005 Views
Last Modified: 2013-12-07
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
Comment
Question by:rgb192
  • 3
  • 3
  • 3
9 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39694786
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
 

Author Comment

by:rgb192
ID: 39694802
46,000 kb

I am reading the skype sql file
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39694818
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 250 total points
ID: 39694822
Or you might be able to do:

ini_set('memory_limit','256M');
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39695357
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
 

Author Comment

by:rgb192
ID: 39695404
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39695536
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
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 39695670
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
 

Author Closing Comment

by:rgb192
ID: 39703212
Thanks for ini change and code explaining size.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question