digital_paki
digital_paki

Reputation: 185

Array taking up waay too much memory

I am creating an array to hold values returned from a query - It seems adding to a multi dimensional array causes the script to take upto 125Mb of memory and considering the total amount of data that the array is supposed to hold is not more than 5Mb it's very weird:

Here is what I am doing:

try 
{
    if (!$link = mysql_connect(DB_Host, DB_User, DB_Password))
            Throw New Exception(mysql_error());
    if (!mysql_select_db("Spexplus_Demo"))
            throw New Exception(mysql_error());
} 
catch (Exception $e) 
{
    echo $e->getMessage().$e->getTraceAsString();
    exit(1);
}

$query = "select cda.categoryid as categoryId, 
                  cda.templatetype as templateType, 
                  hn.headerid as headerId, 
                  hn.name as headerName, 
                  an.attributeid as attributeId, 
                  an.name as attributeName
                  from categorydisplayattributes cda 
                  join categoryheader ch on cda.headerid = ch.headerid 
                  and cda.templatetype = ch.templatetype 
                  and cda.categoryid = ch.categoryid 
                  join headernames hn on cda.headerid = hn.headerid 
                  and hn.localeid = 1
                  join attributenames an on cda.attributeid = an.attributeid 
                  and an.localeid = 1";

$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))

        {
            $categorydisplayattributes[$row['categoryId']][$row['templateType']][$row['headerId']][$row['headerName']][$row['attributeId']][$row['attributeName']] =array() ;
        }


echo "Memory After CDA:".memory_get_usage(TRUE).Line_Terminator;
exit();

The query result itself doesn't go beyond 5Mb or so when I check, however, assigning the values to the array in this fashion causes the spike - Has anyone else encountered something like this?

Upvotes: 1

Views: 309

Answers (1)

Khurram Ijaz
Khurram Ijaz

Reputation: 1864

    while($row = mysql_fetch_assoc($result))


           {

//concatenate the values using '.' if string 

$index = $row['categoryId']][$row['templateType']][$row['headerId']][$row['headerName']][$row['attributeId']][$row['attributeName'];

                $categorydisplayattributes[$index] =array() ;
            }

try that.

Upvotes: 2

Related Questions