How do you debug php "Out of Memory" issues? - php

I've had some issues lately with PHP memory limits lately:
Out of memory (allocated 22544384) (tried to allocate 232 bytes)
These are quite the nuisance to debug since I'm not left with a lot of info about what caused the issue.
Adding a shutdown function has helped
register_shutdown_function('shutdown');
then, using error_get_last(); I can obtain information about the last error, in this case, the "Out of memory" fatal error, such as the line number, and the php file name.
This is nice and all, but my php program is heavily object oriented. An error deep in the stack doesn't tell me much about the control structure or the execution stack at the moment of the error. I've tried debug_backtrace(), but that just shows me the stack during shutdown, not the stack at the time of the error.
I know I can just raise the memory limit using ini_set or modifying php.ini, but that doesn't get me any closer to actually figuring out what is consuming so much memory or what my execution flow looks like during the error.
Anyone have a good methodology for debugging memory errors in advanced Object Oriented PHP programs?

echo '<pre>';
$vars = get_defined_vars();
foreach($vars as $name=>$var)
{
echo '<strong>' . $name . '</strong>: ' . strlen(serialize($var)) . '<br />';
}
exit();
/* ... Code that triggers memory error ... */
I use this to print out a list of currently assigned variables just before a problem section of my code, along with a (very) rough estimate of the size of the variable. I go back and unset anything that isn't needed at and beyond the point of interest.
It's useful when installing an extension isn't an option.
You could modify the above code to use memory_get_usage in a way that will give you a different estimate of the memory in a variable, not sure whether it'd be better or worse.

Memprof is a php extension that helps finding those memory-eaters snippets, specially in object-oriented codes.
This adapted tutorial is quite useful.
Note: I unsuccessfully tried to compile this extension for windows. If you try so, be sure your php is not thread safe. To avoid some headaches I suggest you to use it under *nix environments.
Another interesting link was a slideshare describing how php handles memory. It gives you some clues about your script's memory usage.

I wonder is perhaps your thinking regards methodology is flawed here.
The basic answer to your question - how do I find out where this error is occurring? - has already been answered; you know what's causing that.
However, this is one of those cases where the triggering error isn't really the problem - certainly, that 232 byte object isn't your problem at all. It is the 20+Megs that was allocated before it.
There have been some ideas posted which can help you track that down; you really need to look "higher level" here, at the application architecture, and not just at individual functions.
It may be that your application requires more memory to do what it does, with the user load you have. Or it may be that there are some real memory hogs that are unnecessary - but you have to know what is necessary or not to answer that question.
That basically means going line-by-line, object-by-object, profiling as needed, until you find what you seek; big memory users. Note that there might not be one or two big items... if only it were so easy! Once you find the memory-hogs, you then have to figure out if they can be optimized. If not, then you need more memory.

Check the documentation of the function memory_get_usage() to view the memory usage in run time.

Website "IF !1 0" provides a simple to use MemoryUsageInformation class. It is very useful for debugging memory leaks.
<?php
class MemoryUsageInformation
{
private $real_usage;
private $statistics = array();
// Memory Usage Information constructor
public function __construct($real_usage = false)
{
$this->real_usage = $real_usage;
}
// Returns current memory usage with or without styling
public function getCurrentMemoryUsage($with_style = true)
{
$mem = memory_get_usage($this->real_usage);
return ($with_style) ? $this->byteFormat($mem) : $mem;
}
// Returns peak of memory usage
public function getPeakMemoryUsage($with_style = true)
{
$mem = memory_get_peak_usage($this->real_usage);
return ($with_style) ? $this->byteFormat($mem) : $mem;
}
// Set memory usage with info
public function setMemoryUsage($info = '')
{
$this->statistics[] = array('time' => time(),
'info' => $info,
'memory_usage' => $this->getCurrentMemoryUsage());
}
// Print all memory usage info and memory limit and
public function printMemoryUsageInformation()
{
foreach ($this->statistics as $satistic)
{
echo "Time: " . $satistic['time'] .
" | Memory Usage: " . $satistic['memory_usage'] .
" | Info: " . $satistic['info'];
echo "\n";
}
echo "\n\n";
echo "Peak of memory usage: " . $this->getPeakMemoryUsage();
echo "\n\n";
}
// Set start with default info or some custom info
public function setStart($info = 'Initial Memory Usage')
{
$this->setMemoryUsage($info);
}
// Set end with default info or some custom info
public function setEnd($info = 'Memory Usage at the End')
{
$this->setMemoryUsage($info);
}
// Byte formatting
private function byteFormat($bytes, $unit = "", $decimals = 2)
{
$units = array('B' => 0, 'KB' => 1, 'MB' => 2, 'GB' => 3, 'TB' => 4,
'PB' => 5, 'EB' => 6, 'ZB' => 7, 'YB' => 8);
$value = 0;
if ($bytes > 0)
{
// Generate automatic prefix by bytes
// If wrong prefix given
if (!array_key_exists($unit, $units))
{
$pow = floor(log($bytes) / log(1024));
$unit = array_search($pow, $units);
}
// Calculate byte value by prefix
$value = ($bytes / pow(1024, floor($units[$unit])));
}
// If decimals is not numeric or decimals is less than 0
// then set default value
if (!is_numeric($decimals) || $decimals < 0)
{
$decimals = 2;
}
// Format output
return sprintf('%.' . $decimals . 'f ' . $unit, $value);
}
}

Use xdebug to profile memory usage.

Related

Why get_memory_peak_usage() is greater than get_memory_usage()

I'm trying to figure out this: if get_memory_usage() prints memory, used by script when this function was called, then every time this memory count should always increase or shouldn't change (actually, this is happening when I'm trying to get this usage multiple times after some lines of my code). And I've noticed that get_memory_peak_usage() is greater than the last result of get_memory_usage(), and how can it be if get_memory_usage() fixes already used memory, so this value can't decrease?
Script:
echo "\n" . 'Used: '.memory_get_usage().' bytes';
$array = array("1" => "value", "foo" => "bar",);
echo "\n" . 'Used: '.memory_get_usage().' bytes';
echo "\n" . 'Peak: '.memory_get_peak_usage().' bytes';
PHP has dynamic memory management that frees memory that is no longer needed.
The currently required memory (get_memory_usage()) changes during the execution of a PHP script. get_memory_peak_usage() always gets the maximum memory required.
Therefore get_memory_peak_usage() is always greater than get_memory_usage().
The memory is also used by PHP statements, functions, classes, etc.
Sufficiently large arrays must be used to demonstrate how memory management works.
Let's look at the following code:
function printMemUse(string $comment){
printf('%s : %dk/%dk<br>',
$comment,
memory_get_usage()/1024,
memory_get_peak_usage()/1024
);
}
function proc1(){
$arr = array_fill(0,6000,'a string');
return $arr[2000];
}
printMemUse('start');
$arr = array_fill(0,6000,'a string');
printMemUse('first array filled');
proc1();
printMemUse('after function called');
$arr2 = array_fill(0,6000,'a string');
printMemUse('secondarray2 filled');
$arr = null;
printMemUse('first arr set null');
$arr2 = null;
printMemUse('arr2 set null');
Output:
start : 376k/413k
first array filled : 637k/637k
after function called : 637k/897k
secondarray2 filled : 897k/897k
first arr set null : 637k/897k
arr2 set null : 377k/897k
The first value is memory_get_usage() as kByte, the second memory_get_peak_usage() kByte.
Variables in a function are released again after the end. We're just seeing a new maximum (after function called). This memory is then used by the 2nd array. The maximum value does not increase. With the assignment of NULL, the memory for the arrays is also no longer required.
Try a little yourself in the PHP sandbox.

pthreads stopping already running thread once a condition has been met

I'm still relatively new to PHP and trying to use pthreads to solve an issue. I have 20 threads running processes that end at varying times. Most finish around < 10 seconds or so. I don't need all 20, just 10 detected. Once I get to 10, I would like to kill the threads, or to continue on to the next step.
I have tried using set_time_limit to about 20 seconds for each of the threads, but they ignore it and keep running. I am looping through the jobs looking for the join because I didn't want the rest of the program to run but I'm stuck until the slowest one has finished. While pthreads has reduced the time from around a minute to about 30 seconds, I can shave even more time since the first 10 run in about 3 seconds.
Thanks for any help and here is my code:
$count = 0;
foreach ( $array as $i ) {
$imgName = $this->smsId."_$count.jpg";
$name = "LocalCDN/".$imgName;
$stack[] = new AsyncImageModify($i['largePic'], $name);
$count++;
}
// Run the threads
foreach ( $stack as $t ) {
$t->start();
}
// Check if the threads have finished; push the coordinates into an array
foreach ( $stack as $t ) {
if($t->join()){
array_push($this->imgArray, $t->data);
}
}
class class AsyncImageModify extends \Thread{
public $data;
public function __construct($arg, $name, $container) {
$this->arg = $arg;
$this->name = $name;
}
public function run() {
//tried putting the set_time_limit() here, didn't work
if ($this->arg) {
// Get the image
$didWeGetTheImage = Image::getImage($this->arg, $this->name);
if($didWeGetTheImage){
$timestamp1 = microtime(true);
print_r("Starting face detection $this->arg" . "\n");
print_r(" ");
$j = Image::process1($this->name);
if($j){
// lets go ahead and do our image manipulation at this point
$userPic = Image::process2($this->name, $this->name, 200, 200, false, $this->name, $j);
if($userPic){
$this->data = $userPic;
print_r("Back from process2; the image returned is $userPic");
}
}
$endTime = microtime(true);
$td = $endTime-$timestamp1;
print_r("Finished face detection $this->arg in $td seconds" . "\n");
print_r($j);
}
}
}
It is difficult to guess the functionality of Image::* methods, so I can't really answer in any detail.
What I can say, is that there are very few machines I can think of that are suitable to run 20 concurrent threads in any case. A more suitable setup would be the worker/stackable model. A Worker thread is a reuseable context, and can execute task after task, implemented as Stackables; execution in a multi-threaded environment should always use the least amount of threads to get the most work done possible.
Please see pooling example and other examples that are distributed with pthreads, available on github, additionally, much information regarding usage is contained in past bug reports, if you are still struggling after that ...

In PHP, how can I detect that input vars were truncated due to max_input_vars being exceeded?

I know that an E_WARNING is generated by PHP
PHP Warning: Unknown: Input variables exceeded 1000
But how can I detect this in my script?
A "close enough" method would be to check if( count($_POST, COUNT_RECURSIVE) == ini_get("max_input_vars"))
This will cause a false positive if the number of POST vars happens to be exactly on the limit, but considering the default limit is 1000 it's unlikely to ever be a concern.
count($_POST, COUNT_RECURSIVE) is not accurate because it counts all nodes in the array tree whereas input_vars are only the terminal nodes. For example, $_POST['a']['b'] = 'c' has 1 input_var but using COUNT_RECURSIVE will return 3.
php://input cannot be used with enctype="multipart/form-data". http://php.net/manual/en/wrappers.php.php
Since this issue only arises with PHP >= 5.3.9, we can use anonymous functions. The following recursively counts the terminals in an array.
function count_terminals($a) {
return is_array($a)
? array_reduce($a, function($carry, $item) {return $carry + count_terminals($item);}, 0)
: 1;
}
What works for me is this. Firstly, I put this at the top of my script/handler/front controller. This is where the error will be saved (or $e0 will be null, which is OK).
$e0 = error_get_last();
Then I run a bunch of other processing, bootstrapping my application, registering plugins, establishing sessions, checking database state - lots of things - that I can accomplish regardless of exceeding this condition.. Then I check this $e0 state. If it's not null, we have an error so I bail out (assume that App is a big class with lots of your magic in it)
if (null != $e0) {
ob_end_clean(); // Purge the outputted Warning
App::bail($e0); // Spew the warning in a friendly way
}
Tweak and tune error handlers for your own state.
Registering an error handler won't catch this condition because it exists before your error handler is registered.
Checking input var count to equal the maximum is not reliable.
The above $e0 will be an array, with type => 8, and line => 0; the message will explicitly mention input_vars so you could regex match to create a very narrow condition and ensure positive identification of the specific case.
Also note, according to the PHP specs this is a Warning not an Error.
function checkMaxInputVars()
{
$max_input_vars = ini_get('max_input_vars');
# Value of the configuration option as a string, or an empty string for null values, or FALSE if the configuration option doesn't exist
if($max_input_vars == FALSE)
return FALSE;
$php_input = substr_count(file_get_contents('php://input'), '&');
$post = count($_POST, COUNT_RECURSIVE);
echo $php_input, $post, $max_input_vars;
return $php_input > $post;
}
echo checkMaxInputVars() ? 'POST has been truncated.': 'POST is not truncated.';
Call error_get_last() as soon as possible in your script (before you have a chance to cause errors, as they will obscure this one.) In my testing, the max_input_vars warning will be there if applicable.
Here is my test script with max_input_vars set to 100:
<?php
if (($error = error_get_last()) !== null) {
echo 'got error:';
var_dump($error);
return;
}
unset($error);
if (isset($_POST['0'])) {
echo 'Got ',count($_POST),' vars';
return;
}
?>
<form method="post">
<?php
for ($i = 0; $i < 200; $i++) {
echo '<input name="',$i,'" value="foo" type="hidden">';
}
?>
<input type="submit">
</form>
Output when var limit is hit:
got error:
array
'type' => int 2
'message' => string 'Unknown: Input variables exceeded 100. To increase the limit change max_input_vars in php.ini.' (length=94)
'file' => string 'Unknown' (length=7)
'line' => int 0
Tested on Ubuntu with PHP 5.3.10 and Apache 2.2.22.
I would be hesitant to check explicitly for this error string, for stability (they could change it) and general PHP good practice. I prefer to turn all PHP errors into exceptions, like this (separate subclasses may be overkill, but I like this example because it allows # error suppression.) It would be a little different coming from error_get_last() but should be pretty easy to adapt.
I don't know if there are other pre-execution errors that could get caught by this method.
What about something like that:
$num_vars = count( explode( '###', http_build_query($array, '', '###') ) );
You can repeat it both for $_POST, $_GET, $_COOKIE, whatever.
Still cant be considered 100% accurate, but I guess it get pretty close to it.

How can I clear the memory while running a long PHP script? tried unset()

My script imports an excel file into a product database to update quantities new products etc....
I am having memory issue and I have tried raising the memory limit to the max(800MB+). I have tried unsetting the variables in order to release the memory between the loops but I still run out of memory. I have tried setting the timeout to infinite but its definitely a memory issue.
Error msg from log file:
Fatal error: Allowed memory size of 851443712 bytes exhausted (tried to allocate 71 bytes)
None of the script is contained in a function. If I create the main for loop inside a function and repeatedly call that function will that help garbage collection and clear up memory? Any help or guidance will be appreciated.
Import Script:
error_reporting( E_ALL & ~E_NOTICE );
ini_set('memory_limit', '812M');
set_time_limit(0);
/* Config Start */
define('BasePath', '/home/xxxxx/public_html');
define('CfgMagentoPath', BasePath);
define('CfgCategoryMapDBxls', BasePath."/xxxx/Shdddddd.xls");
define('CfgVenderDBxls', BasePath."/xxxx/xxxxxx.xls");
define('CfgReportEmail', "xxxxxx#gmail.com");
/* Config End */
require_once(CfgMagentoPath . '/app/Mage.php');
Mage::app();
//$app = Mage::app('default');
//Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
require_once(BasePath.'/xxxxx/xxxx/libs/mage.func-inc.php');
require_once(BasePath.'/xxxxx/xxxxx/libs/excel-read.class.php');
//Alert Arrays
$AAnotmapped = array();
$AAnewproducts = array();
$AApriceupdated = array();
$AAimgerror = array();
$PriceErrors = array();
$SkipCat = false;
//Create Mapped Cats - In Magento
$excel = new ExcelReader(CfgCategoryMapDBxls,"UTF-8");
$CM = $excel->getWorksheetData('Sheet1');
if(!$SkipCat){
echo "======== Generating Catagory Maps ===========\n\n";
CatMap_Create($CM);
echo "======== ============================== ===========\n\n";
}
//Start Item Read
$excel = new ExcelReader(CfgVenderDBxls,"UTF-8");
$IT = $excel->getWorksheetData('New_DATA');
$ITcnt = 0;
$ITtotal = count($IT);
foreach($IT as $ItemRow){
$ITcnt++;
$cSKU = $ItemRow['ITEM'];
$cProductName = Clean_Data($ItemRow['ALTSHORTDESC']);
$cCatName = Clean_Data($ItemRow['CATEGORY']);
$cManuf = Clean_Data($ItemRow['MANUFACTURER']);
$cShortDesc = Clean_Data($ItemRow['SHORTDESC']);
$cLongDesc = Clean_Data($ItemRow['LONGDESC']);
$cUPC = Prod_GetUPC($ItemRow['UPC'], $ItemRow['ALTUPC']);
$cStockQty = $ItemRow['QTY'];
$cWeight = Prod_GetWeight($ItemRow['WEIGHT'], $ItemRow['ALTWEIGHT']);
$cPrice = Prod_FigurePrice($ItemRow['COST'], $ItemRow['MSRP'], $ItemRow['MAP']);
$cCost = $ItemRow['COST'];
//Locate Catagory Map Magento ID
$mCatId = CatMap_Search($CM, $ItemRow['CATEGORY']);
//Now Create Product
if($mCatId > 0 && $cProductName != ""){
echo date("m.d.y g:i a")."\t($ITcnt / $ITtotal) Working On: " . $cProductName . " - SKU: $cSKU\n";
$ProdID = Prod_GetIDfromSKU($cSKU);
if($ProdID > 0){
if(Prod_Update($ProdID, $cCost, $cStockQty, $cWeight, $cUPC)){
echo "Updated: $cProductName\n";
$ITindex++;
}
}else{
Prod_Create($cSKU, $cProductName, $cManuf, $cPrice, $cCost, $cWeight, $cShortDesc, $cLongDesc, $cStockQty, $cUPC, $mCatId);
echo "Created: $cProductName to Catagory: $mCatId\n";
echo "$cShortDesc\n\n";
$ProdID = Prod_GetIDfromSKU($cSKU);
}
if($cPrice <= $cCost){
array_push($PriceErrors, "[$cSKU] $cProductName > Cost: $cCost | Price: $cPrice");
echo "Price Lower than Cost : Auto Inactive : Cost: $cCost | Price: $cPrice\n";
}
Prod_AddImg($ProdID, $cSKU);
}
unset($ItemRow, $ProdID, $cSKU, $cProductName, $cManuf, $cPrice, $cCost, $cWeight, $cShortDesc, $cLongDesc, $cStockQty, $cUPC, $mCatId);
echo "\n";
}
echo "======== Disabling 0 Product Catagories ===========\n\n";
Cat_Disable_Empty($CM);
echo "======== ============================== ===========\n\n";
unset($CM, $IT, $excel);
//array_push($AAnotmapped, 'Cat not Mapped');
//array_push($AApriceupdated, '### Price Updated');
//array_push($AAimgerror , 'Image Error');
Send_Status_Email();
Mage_Reindex();
echo date("m.d.y g:i a")."\tCompleted\n\n";
//print_r($AAnotmapped);
//print_r($AApriceupdated);
//print_r($AAimgerror);
Use functions.
Use $var = null; instead of unset($var);. Unset simply kills the variable reference.
As mentioned on this comment:
When you are using unset, the memory will only be freed whenever garbage collector decides, but when you are setting a variable to a different value (null in this case), then you might get some memory freed of course with the cost of CPU.
Even when you use functions, you would expect the garbage collector to clean up everything in the scope of the function when the function returns.. This is not a guarantee and using functions might even work against you if you're fighting memory usage. Because of the scope, php has to create copies of the variables passed as parameters, which will only add up to the memory usage. You could look into passing references.
The garbage collector will only free up memory when there are CPU cycles available. Usually in loops it wont get a chance, so it will try to do this after the loop, in which case it might already be too late.
However you can force the garbage collector to do his round by calling gc_collect_cycles.
Also you can try debugging your code using memory_get_usage()

PHP and Windows WMI, CPU Temperature and false readings?

I wrote this PHP function:
<?php
//windows cpu temperature
function win_cpu_temp(){
$wmi = new COM("winmgmts://./root\WMI");
$cpus = $wmi->execquery("SELECT * FROM MSAcpi_ThermalZoneTemperature");
foreach ($cpus as $cpu) {
$cpupre = $cpu->CurrentTemperature;
}
$cpu_temp = ($cpupre/10)-273.15 . ' C';
return $cpu_temp;
}
echo win_cpu_temp();
?>
My problem, is that the script displays 59.55 C which I had thought was correct. I checked this value several hours later, and it's exactly the same. I just put the CPU to work at 90% compressing video for ten minutes, and this value is the same still.
Can anyone help me find the "true" value for this function?
I've read (to no avail):
MSAcpi_ThermalZoneTemperature class not showing actual temperature
How is, say, "Core Temp" getting its values? Same computer, it reports between 49 and 53 Celsius.
With a little digging around I found the common issue with using MSAcpi_ThermalZoneTemperature was that it is dependent upon being implemented on your system.
You could try querying Win32_TemperatureProbe and see if you have any luck there.
Neither MSAcpi_ThermalZoneTemperature or Win32_TemperatureProbe worked on my system, although if you have admin access, you can use http://openhardwaremonitor.org/ which provides a WMI interface for all available sensor data.
This worked great for me and I was able to accurately report CPU Core temp from a PHP script:
function report_cpu_temp(){
$wmi = new COM('winmgmts://./root/OpenHardwareMonitor');
$result = $wmi->ExecQuery("SELECT * FROM Sensor");
foreach($result as $obj){
if($obj->SensorType == 'Temperature' && strpos($obj->Parent, 'cpu') > 0)
echo "$obj->Name ($obj->Value C)"; // output cpu core temp
else
echo 'skipping ' . $obj->Identifier ;
echo '<br />';
}
}
Hope this helps.

Categories