On Oct 15, 3:58 pm, AD7six <[email protected]> wrote:
> On 15 oct, 00:01, Sho Shimauchi <[email protected]> wrote:
>
>
>
> > My Program's memory usage increses when Model->query() is called.
> > I found out that a cacheDboSource->_queryCache is not flushed in
> > source code.
>
> >https://trac.cakephp.org/browser/trunk/cake/1.2.x.x/cake/libs/model/d...
> > InDboSource->query(),DboSource->fetchAll() is called when number of
> > arguments is 
> > 1.https://trac.cakephp.org/browser/trunk/cake/1.2.x.x/cake/libs/model/d...
> >DboSource->fetchAll() has second argument $cache, which default is
> > true.
> > So, wheneverDboSource->query() is called with only one argument,
> > caching is always used.
> > And I could not found any code to flush this cache.
>
> > How do I flush the cache?
>
> > If there are no method for flushing the cache, I put two proposal:
> > (1) make available to choose whether caching is used or not when
> >DboSource->query() is called.
> > (2) introduce some caching algolithm like LRU to avoid increasing
> > memory infinitely.
>
> At a guess, whatever code you've got that prompted you to write this
> message (show it) is wrong.
>
> AD

My previous post was not based on some message which was printed on my
screen.

I analyzed my CakePHP application about memory usage with some function
(var_dump(),memory_get_usage(),etc)
and tools(Xdebug,etc).
On my system, I needed to use Model->query() method many times when
some Controller method was called.
But I found some facts listed below:

(1) Memory usage increases without end whenever query() is called.
(2) In Model->query(), query() method of some data source class which
defined in database.php.
For example, if you choose MySQL for data source, DboMysql->query() is
called in Model->query().
(3) This query() method belongs to DboSource class, which is parent of
the data source class.
(4) In DboSource->query(), DboSource->fetchAll() method is called.
This method caches all queries and results
which is given to Model->query() as argument. The cache is stored in
DboSource->_queryCache.
(5) There is no method to control size of the cache.

And so, I found that memory usage increases when Model->query() is
called many times, with different query each time.

For example, run this simple controller:
(prepare a table 'tbl_1' and over 100 data and Model class before)


class Tbl1Controller extends AppController
{
  var $uses = array("Tbl1");

  function index(){
    for($i=1; $i<=100;$i++){
        $sql = 'SELECT * FROM `tbl_1` AS `Tbl1` WHERE `Tbl1`.`id` =
' . $i . ';';
        $this->Tbl1->query($sql);
    }
    $db =& ConnectionManager::getDataSource($this->Tbl1->useDbConfig);
    var_dump($db->_queryCache);
  }
}


When you run this, your browser will print some output as below:


array
  'SELECT * FROM `tbl_1` AS `Tbl1` WHERE `Tbl1`.`id` = 1;' =>
    array
      0 =>
        array
          'Tbl1' =>
            array
              ...
  'SELECT * FROM `tbl_1` AS `Tbl1` WHERE `Tbl1`.`id` = 2;' =>
    array
      0 =>
        array
          'Tbl1' =>
            array
              ...


Another example, you run this script to look at increasing memory
usage:
(don't forget add more data(at least 10000) to 'tbl_1')


class Tbl1Controller extends AppController
{
  var $uses = array("Tbl1");

  function index(){
    for($i=1; $i<=10000;$i++){
        $sql = 'SELECT * FROM `tbl_1` AS `Tbl1` WHERE `Tbl1`.`id` =
' . $i . ';';
        $this->Tbl1->query($sql);
        if($i % 1000 == 0){
           var_dump(memory_get_usage());
        }
    }
  }
}


Here is a sample output of this script.
You can look memory usage is increasing.


int 6698552

int 7702684

int 8710900

int 9710952

int 10727416

int 11727452

int 12727492

int 13727548

int 14760388

int 15760464


I think this is a problem if I cannot avoid memory usage increase.
If I can, please tell me how to avoid this.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to