?
Solved

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

Posted on 2013-12-03
9
Medium Priority
?
2,100 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 84

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 84

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
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
LVL 84

Accepted Solution

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

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

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 111

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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

762 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