Solved

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

Posted on 2013-12-03
9
1,968 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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
46,000 kb

I am reading the skype sql file
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 250 total points
Comment Utility
Or you might be able to do:

ini_set('memory_limit','256M');
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
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
Comment Utility
Thanks for ini change and code explaining size.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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 …

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now