Using waaay too much memory, on a very small script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Pyro In A Cage
Forum Newbie
Posts: 10
Joined: Mon Jul 17, 2006 4:27 pm

Using waaay too much memory, on a very small script

Post 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]
Last edited by Pyro In A Cage on Mon Jul 17, 2006 6:45 pm, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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?
(#10850)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Pyro In A Cage
Forum Newbie
Posts: 10
Joined: Mon Jul 17, 2006 4:27 pm

Post 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.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

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

Post 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
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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']))
Post Reply