Freeing Memory
am 30.07.2009 14:25:25 von Anton Heuschen
--001485f197c2fd511a046feb65f4
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
How would you go about ensuring the memory is not exhausted when running a
script ?
I have a script, which to make it basic ... reads values from files, I
create an array of values per file then with a foreach insert values into a
table, I have added a line to echo the memory use after each array is done
(after insert is done and the foreach is complete for the file) with: ->
echo "Mem SQL: ".memory_get_usage() . "\n";
It gave me this result below:
Mem SQL: 8341312
Mem SQL: 8461856
Mem SQL: 8693440
Mem SQL: 9327008
Mem SQL: 9798952
Mem SQL: 10238392
Mem SQL: 10604776
As can be seen the mem usage simply grows,
I have added a line after each iteration of the foreach is complete to unset
the vars and array ... thinking this would basically clear up the allocated
memmory used by the array ... and it would start at 0 again for the next
array looped, but obviously this is not quite the answer.
The question is then how do you "clear" memmory then ?
--001485f197c2fd511a046feb65f4--
Re: Freeing Memory
am 30.07.2009 16:42:26 von Dan Shirah
--00151750db0ef364ed046fed4fb7
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
>
> How would you go about ensuring the memory is not exhausted when running a
> script ?
>
> I have a script, which to make it basic ... reads values from files, I
> create an array of values per file then with a foreach insert values into a
> table, I have added a line to echo the memory use after each array is done
> (after insert is done and the foreach is complete for the file) with: ->
> echo "Mem SQL: ".memory_get_usage() . "\n";
>
> It gave me this result below:
>
> Mem SQL: 8341312
> Mem SQL: 8461856
> Mem SQL: 8693440
> Mem SQL: 9327008
> Mem SQL: 9798952
> Mem SQL: 10238392
> Mem SQL: 10604776
>
> As can be seen the mem usage simply grows,
>
> I have added a line after each iteration of the foreach is complete to
> unset
> the vars and array ... thinking this would basically clear up the allocated
> memmory used by the array ... and it would start at 0 again for the next
> array looped, but obviously this is not quite the answer.
>
> The question is then how do you "clear" memmory then ?
>
I don't know what version of SQL you are using, but I have found that using:
mysql_free_result($result);
mssql_free_result($result);
ifx_free_result($result);
Helped my queries run much faster and use less resources. I had something
similar to your script where I would read lines from a huge file and then
insert the contents into my database. Before adding the above the process
would take 20-30 minutes. After freeing the results after each insert my
script completed in about 5-8 minutes.
Just add that within your foreach loop after you execute your query that
inserts the info.
Hope that helps.
Dan
--00151750db0ef364ed046fed4fb7--
Re: Freeing Memory
am 31.07.2009 12:43:49 von news.NOSPAM.0ixbtqKe
On Thu, 30 Jul 2009 10:42:26 -0400, Dan Shirah wrote:
> I don't know what version of SQL you are using, but I have found that using:
>
> mysql_free_result($result);
> mssql_free_result($result);
> ifx_free_result($result);
>
> Helped my queries run much faster and use less resources. I had something
> similar to your script where I would read lines from a huge file and then
> insert the contents into my database. Before adding the above the process
> would take 20-30 minutes. After freeing the results after each insert my
> script completed in about 5-8 minutes.
How does that work considering that mysql_query() only
returns true or false on INSERT? I'd expect the script
to fail on $result not being a valid resource.
/Nisse
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Freeing Memory
am 31.07.2009 13:54:13 von Dan Shirah
--00151750dafc389c71046fff14d1
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
>
> How does that work considering that mysql_query() only
> returns true or false on INSERT? I'd expect the script
> to fail on $result not being a valid resource.
>
I don't know about mysql as I work with MSSQL Server and Informix, but for
me it works like this:
$insert = ifx_prepare("INSERT INTO my_table
VALUES ('0')", $connect_id);
ifx_do($insert) or die ("Query failed");
ifx_free_result($insert);
By using PREPARE and DO to execute the queries it allows ifx_free_result to
release those resources.
--00151750dafc389c71046fff14d1--
Re: Freeing Memory
am 31.07.2009 16:27:54 von news.NOSPAM.0ixbtqKe
On Fri, 31 Jul 2009 07:54:13 -0400, Dan Shirah wrote:
>>
>> How does that work considering that mysql_query() only
>> returns true or false on INSERT? I'd expect the script
>> to fail on $result not being a valid resource.
>>
>
> I don't know about mysql as I work with MSSQL Server and Informix, but for
> me it works like this:
>
> $insert = ifx_prepare("INSERT INTO my_table
> VALUES ('0')", $connect_id);
> ifx_do($insert) or die ("Query failed");
> ifx_free_result($insert);
>
> By using PREPARE and DO to execute the queries it allows ifx_free_result to
> release those resources.
That explains it. (You'd have to use the MySQLi extension
to use prepared statements with MySQL, unless I'm confused).
Personally, I try to make it a point to free resources as
soon as possible.
/Nisse
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php