Solved

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

Posted on 2013-12-03
9
2,034 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 110

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 110

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 110

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 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