Memory limit

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Memory limit

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Memory limit

Post 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?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Memory limit

Post 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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: Memory limit

Post by shiznatix »

Mordred wrote:Use the & version of foreach for an instant /2 gain.
the what version of foreach?
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Memory limit

Post 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?
Post Reply