Page 1 of 1

Memory limit

Posted: Tue Mar 18, 2008 7:10 am
by shiznatix
I am running a script that is eating up 200M of memory per load. This seams like a lot to me but I can't see how I can slim this code up. I was wondering if this 200M is too much or not? This kind of page is not hit by anyone than our employees (6 total) and its not hit very often. Basically, I am grabbing over 70 thousand rows from a table and fixing up the data. Basically this is my code thats crushing memory:

Code: Select all

 
$raketrackingSelect = $this->RAKETRACKING->getAdapter()->select()
->from(
    array(
        'rt' => 'rb_raketracking',
    )
)
->joinLeft(
    array(
        'um' => 'rb_usermap',
    ),
    'rt.fk_room_id = um.fk_room_id AND rt.room_username = um.room_username',
    array(
        'usermap_id' => 'id',
    )
)
->where('rt.fk_room_id = ?', $roomId)
->where('rt.data_date LIKE ?', new Zend_Db_Expr('"'.$selectedMonth.'%"'));
 
$raketracking = $this->RAKETRACKING->getAdapter()->query($raketrackingSelect)->fetchAll();
 
$usermapContent = array();
 
foreach ($raketracking as $val)
{
    if (empty($usermapContent[$val->room_username]))
    {
        $usermapContent[$val->room_username] = array(
            'username'        => $val->room_username,
            'grossRake'       => 0,
            'deductions'      => 0,
            'deductionsStyle' => '',
            'netRake'         => 0,
            'points'          => 0,
            'rakeback'        => 0,
            'rakebackPercent' => 0,
            'updated'         => false,
        );
    }
    
    $usermapContent[$val->room_username] = Functions::calculateRakebackData(Functions::getRoomNameBasedOnId($roomId), $selectedMonth, $val, $usermapContent[$val->room_username], $this->RAKETRACKING);
    $usermapContent[$val->room_username]['usernameStyle'] = ($val->usermap_id ? 'color: black;' : 'color: red;');
}
 
Is just increasing the amount of memory to be used a good idea or is there another way to do this?

Re: Memory limit

Posted: Tue Mar 18, 2008 7:23 am
by s.dot
Can't you just grab eh.. 1000/rows at a time, do your thang, unset() and move onto the next iteration of rows?

Re: Memory limit

Posted: Tue Mar 18, 2008 8:43 am
by Mordred
Use the & version of foreach for an instant /2 gain.
For best memory saving, do that even if you follow scottayy's advice, which in fact has much better gain, though it requires a bit of coding.

Re: Memory limit

Posted: Tue Mar 18, 2008 9:28 am
by shiznatix
Mordred wrote:Use the & version of foreach for an instant /2 gain.
the what version of foreach?

Re: Memory limit

Posted: Tue Mar 18, 2008 9:53 am
by Mordred
shiznatix wrote:
Mordred wrote:Use the & version of foreach for an instant /2 gain.
the what version of foreach?
The RTFM version of foreach ;)
Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself.
"copy" --> memory * 2, capisce?