Re: Where's my memory going?!

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sep 29, 2009, at 4:38 PM, Jim Lucas wrote:

Philip Thompson wrote:
On Sep 28, 2009, at 4:40 PM, jeff brown wrote:

Yes, that's the best way to clean up after yourself.  And you really
should use that on anything you have sitting around daemon like.

Jeff

Philip Thompson wrote:
On Sep 28, 2009, at 4:27 PM, Ralph Deffke wrote:
well this sound clearly to me like you are not freeing resultsets
you are not going to use anymore. In long scripts you have to take
care of this. on short scripts you can be a bit weak on that,
because the resultsets are closed and freed on script ending.

assumed u r using MySQL are u using mysql_free_result($result)

goog luck

ralph_deffke@xxxxxxxx


"Philip Thompson" <philthathril@xxxxxxxxx> wrote in message
news:9C0B9C4C-5E64-4519-862B-8A3E1DA4DE4C@xxxxxxxxxxxx
Hi all.

I have a script that opens a socket, creates a persistent mysql
connection, and loops to receive data. When the amount of specified data has been received, it calls a class which processes the data and inserts it into the database. Each iteration, I unset/destruct that
class I call. However, the script keeps going up in memory and
eventually runs out, causing a fatal error. Any thoughts on where to
start to see where I'm losing my memory?

Thanks in advance,
~Philip
I am not using mysql_free_result(). Is that highly recommended by all?
Thanks,
~Philip

I took your suggestions and made sure to clean up after myself. I'm
running into something that *appears* to be a bug with
mysql_free_result(). Here's a snippet of my db class.

<?php
class db {
   function fetch ($sql, $assoc=false)
   {
       echo "\nMemory usage before query: " . number_format
(memory_get_usage ()) . "\n";
       $resultSet = $this->query($sql);
       echo "Memory usage after  query: " . number_format
(memory_get_usage ()) . "\n";

       if (!$assoc) { $result = $this->fetch_row($resultSet); }
       else {
           $result = $this->fetch_array($resultSet);
           echo "Memory usage after  fetch: " . number_format
(memory_get_usage ()) . "\n";
       }

       $this->freeResult($resultSet);
       echo "Memory usage after   free: " . number_format
(memory_get_usage ()) . "\n";

       return $result;
   }

   function freeResult ($result)
   {
       if (is_resource ($result)) {
           if (!mysql_free_result ($result)) { echo "Memory could not
be freed\n"; }
       }
       unset ($result); // For good measure
   }

   function fetch_row ($set) {
       return mysql_fetch_row ($set);
   }

   function fetch_array ($set) {
       return mysql_fetch_array ($set, MYSQL_ASSOC);
   }
}

// I seem to be losing memory when I call this
$db->fetch($sql);
?>

The result I get with this is...

Memory usage before query: 6,406,548
Memory usage after  query: 6,406,548
Memory usage after  fetch: 6,406,548
Memory usage after   free: 6,406,572

As you may notice, the memory actually goes UP after the *freeing* of
memory. Why is this happening?! What have I done wrong? Is this a bug?
Any thoughts would be appreciated.


First off, my question would be, is your query actually working? Because I would imagine that if you were getting results back from the DB, that the amount
of memory being used would increase between step 1 & 2.

Check to make sure that you are getting results.

I'm confident the queries are working (there's many of them and I know the data they're returning is correct), but they may not always be returning results. The memory value does change in some instances...

Memory usage before query: 5,138,372
Memory usage after  query: 5,138,396
Memory usage after   free: 5,138,556

This was one that use fetch_row() instead of fetch_array(), but the same difference. I did some searching around and I think it's a bug in PHP and/or Zend Memory Management engine. As I mentioned in a previous post about mysql_query() not allocating memory appropriately, I believe this could quite possibly be the case. Several people have reported bugs similar to this... unfortunately, they are marked as (erroneously?) bogus or said it has been fixed (when, IMO, it has not).

http://bugs.php.net/bug.php?id=40883
http://bugs.php.net/bug.php?id=28424
http://bugs.php.net/bug.php?id=41871

I'm using version 5.2.6 on Fedora 8 and the bug still appears to be there. I think I'm going to take a different approach to fix this. I'll create a shell script to loop and invoke the socket listener script, and when it gathers data, call the import script. This way, the php script(s) will end execution after each iteration and release the memory. Is this reasonable?

This has been a long day. Thanks for your input. Any more thoughts are welcome.

~Philip

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux