Forum Moderators: coopster

Message Too Old, No Replies

Memory Error while handling files in base class

         

lorum

10:27 pm on Apr 26, 2010 (gmt 0)

10+ Year Member



Hello,

I am currently trying to handle files through a base class, but whenever I try to do this, PHP throws a memory exception.

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 261904 bytes) in ****\index.php on line **


Even doing something as easy as this:

class MainClass{

protected function PrintFile($val){
print(highlight_file($val,true));
}
}

class InClass extends MainClass{

public function PrintFile($val){
$this->PrintFile($val);
}
}

$class = new InClass();

$class->PrintFile($_POST['file']);


Doesn't work. When I do it without the use of a base class it works like a charm.

Am I missing something here?

Readie

10:38 pm on Apr 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World lorum.

Really simple fix :)

Write the following line at the top of the document, right after the <?php opening tag.
ini_set('memory_limit', '12M');

You might need to increase that 12M to a higher number.

I do, however, suggest you take a look at the code. It may be that it's very inefficiently coded.

lorum

10:48 pm on Apr 26, 2010 (gmt 0)

10+ Year Member



Hello,

I have already tried that, and it didn't work.

If you look at the error too, you will see that the amount of memory is sufficient.
Allowed memory size of 134217728 bytes exhausted (tried to allocate 261904 bytes)


Thanks for your time, however.

eelixduppy

10:54 pm on Apr 26, 2010 (gmt 0)



First off, 128 MB (134217728 bytes) is a huge amount of data. I have doubts you want one instance of this script to be consuming so many resources.

Secondly, the memory is NOT sufficient, it has been exhausted before the script tried to allocate an additional 261904 Bytes.

You need to set your memory limit higher than 128 MB for this to work correctly, however, I think you should also consider a different approach to this unless you are sure this is the best solution to your problem.

astupidname

1:43 am on Apr 27, 2010 (gmt 0)

10+ Year Member



Firstly, is the file actually that large that the file it's self is beyond memory limit? (the file being printed from $_POST['file'])
Secondly, and probably the real problem, is you are attempting to override a protected method (PrintFile) from InClass by defining a public method of the same name, which ain't cool and crashes it. Change naming of the methods.

lorum

6:35 am on Apr 27, 2010 (gmt 0)

10+ Year Member



Hello,

The file that is being printed is 6 KB in size. You are right about the function naming, but this was just an example.

eelixduppy: I have gone so far and have set the memory limit to an obscene amount, namely 512MB and it still doesn't work.

Could you explain why I should consider a different approach to this? The files the base class has to handle are not larger than 512 KB, ever.

Thanks for your time.

eelixduppy

1:16 pm on Apr 27, 2010 (gmt 0)



>> a different approach to this?

Because whatever you are doing in the code is clearly not written correctly. Now that I know the files are no larger than 512 KB then there has got to be a leak somewhere in your code that is preventing correct execution. I can't see your code therefore I was vague about it. I would start looking at any loops if you have them; odds are there is some resource you are allocating on each iteration or something like that.

lorum

2:35 pm on Apr 27, 2010 (gmt 0)

10+ Year Member



Hello,

Yes, I indeed had an error in my code which was causing the same function to be called endlessly, causing memory corruption. This has been resolved and now the script's running like a charm. Thanks for your time.

eelixduppy

9:30 pm on Apr 27, 2010 (gmt 0)



Glad you figured it out. Make sure to set your memory_limit directive back to a more normal setting.