Page 1 of 1

Using waaay too much memory, on a very small script

Posted: Mon Jul 17, 2006 4:33 pm
by Pyro In A Cage
I am working on an authorization script for use with one of my programs (In short, product keys). The page that verifies the code is erroring saying that it has exhausted all of the memory. It makes no sense because the amount of memory it's trying to use is smaller than the maxium.
Fatal error: Allowed memory size of 20971520 bytes exhausted (tried to allocate 3840 bytes) in /home/elitenet/public_html/echo/admin/functions.php on line 18

3840 < 20971520, so why is the memory exhausted?

The entire functions.php script is:

Code: Select all

<?php

session_start();
ini_set("memory_limit", "20M");
include('variables.php');
function logged_in() { //Check to see if the person is logged in
	if (($session['user'] == ('' || null)) || ($session['pass'] == ('' || null))) {
		$temp_var=false;
	}
	else {
		$temp_var=true;
	}
	return $temp_var;
};

function generate_code($seed) {
	$temp_var=ord($seed) * 1337;
	$temp_var=$temp_var + strlen($seed);
	$temp_var=$temp_var / (1337 / strlen($seed));
	
}

?>
Line 18 is:

Code: Select all

$temp_var=$temp_var + strlen($seed);
It doesn't matter what value is passed to the $seed variable (Even if no value is passed), it still does it.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Jul 17, 2006 5:04 pm
by Weirdan
3840 < 20971520, so why is the memory exhausted?
Script tried to allocate 3840 bytes. Because there were not enough memory, it died. You have exhausted almost all the memory before that.

Posted: Mon Jul 17, 2006 5:10 pm
by Chris Corbyn
If you're running a dedicated server or VPS/VDS I've hit a similar issue before through my own idleness. I forgot to create some swap space and memory was running out under higher loads so things started getting messy. Make sure you have enough swap space:

Code: Select all

-bash $  free
             total       used       free     shared    buffers     cached
Mem:        515704     506976       8728          0       5912     115616
-/+ buffers/cache:     385448     130256
Swap:       977248     118636     858612  <--- Here

Posted: Mon Jul 17, 2006 5:23 pm
by Christopher
I'd suggest looking at the logic for the rest of the script. Often the line where the out of memory error occurs is merely the straw that breaks the camel's back. Is there some recursion going on that you don't know about?

Posted: Mon Jul 17, 2006 5:53 pm
by RobertGonzalez
I ran into something like this for a client. He had a 47000 line array and a 38000 line array that were being included in every page. First page load tanked the server.

Posted: Mon Jul 17, 2006 6:44 pm
by Pyro In A Cage
I found the problem.

For future reference:

code_verify.php was including functions.php and variables.php.
fariables.php included functions.php
functions.php included variables.php

This caused a never-ending loop of inclusions, as shown in the diagram below.

Image


The problem was fixed by changing the affected includes to include_once's.


Thank you all for your help, and fast responses. This site is extremely active and helpful, and I will definately be coming back.

Re: Using waaay too much memory, on a very small script

Posted: Mon Jul 17, 2006 6:51 pm
by bokehman
Pyro In A Cage wrote:

Code: Select all

('' || null))
Off topic: That really sucks. You are evaluating the value in brackets to a boolean before making the comparison. The only reason this seems to work is because you are using negative values but something like the following will always evaluate to true.

Code: Select all

if(('blonde' || 'redhead') == 'sexy'){  // true

Posted: Mon Jul 17, 2006 7:52 pm
by Ollie Saunders
Off topic: That really sucks.
Your right.
I think he means to do this:

Code: Select all

if (($session['user'] == '' || $session['user'] == null) or ($session['pass'] == '' || $session['pass'] == null))
Which is effectively:

Code: Select all

if (empty($session['user']) or empty($session['pass']))