By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,709 Members | 1,692 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,709 IT Pros & Developers. It's quick & easy.

Pear DB and memory usage, strange...

P: n/a
Hi

I have a problem with pear DB

my code :

for ($i=0;$i<100000;$i)
{
$sql = "select id from table where id=x";

$db->query($sql);
}

if i run this code memory usage grow up, during for loop

but if i use mysql_query($sql) instead of $db->query($sql), memory
usage stay the same during all for loop

Have you an idea to solve my problem ?

vincent
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
sa*******@gmail.com (Vincent) wrote in message
news:<77**************************@posting.google. com>...

I have a problem with pear DB

my code :

for ($i=0;$i<100000;$i) {
$sql = "select id from table where id=x";
$db->query($sql);
}

if i run this code memory usage grow up, during for loop

but if i use mysql_query($sql) instead of $db->query($sql), memory
usage stay the same during all for loop
This is a rather well-known deficiency in memory management in PHP 4.
Memory allocated to objects is not released until the end of script's
execution.
Have you an idea to solve my problem ?


Don't use OOP in general and database abstraction layers in particular.
Another option is to switch to PHP 5, where this problem has purportedly
been solved.

Cheers,
NC
Jul 17 '05 #2

P: n/a
Nikolai Chuvakhin wrote:
for ($i=0;$i<100000;$i) {
$sql = "select id from table where id=x";
$db->query($sql);
}

if i run this code memory usage grow up, during for loop

but if i use mysql_query($sql) instead of $db->query($sql), memory
usage stay the same during all for loop


This is a rather well-known deficiency in memory management in PHP 4.
Memory allocated to objects is not released until the end of script's
execution.


To be more accurate, PHP4 releases memory taken by objects created from
simple user classes before the script ends - i.e. when variable is no
longer used or it is being assigned a new value. But it doesn't work the
same way when you have an object from a more complex class structure.
Exactly what causes it - I don't know.
Have you an idea to solve my problem ?


Don't use OOP in general and database abstraction layers in particular.
Another option is to switch to PHP 5, where this problem has purportedly
been solved.


Yes, at least with what I have tested, memory usage still starts to grow,
but levels out at some point. In my testing, it's definitely not nearly as
efficient as using variables and functions.

Even if OP were to switch to and run the script in PHP5, it would be a good
idea to first try the script both ways (with and without objects in the for
loop) and track the memory usage during that for loop to help him decide if
using objects is right for him.
Jul 17 '05 #3

P: n/a
> if i run this code memory usage grow up, during for loop

but if i use mysql_query($sql) instead of $db->query($sql), memory
usage stay the same during all for loop

Have you an idea to solve my problem ?


How are the memory requirements on your system with the following alternative?

// http://pear.php.net/package/DB/docs/...#methodprepare
$statement = $db->prepare('select x from table where id = ?');

for ($i = 0; $i < 100000; ++$i)
{
$result = $db->execute($statement, $i);
}

$db->freePrepared($statement);
Regards,
Markus
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.