|
Posted by Jerry Stuckle on 07/05/07 18:13
vivek@thoughtconvergence.com wrote:
> On Jul 3, 6:42 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>> v...@thoughtconvergence.com wrote:
>>> Could someone please help me figure out why the memory usage
>>> fluctuates when I use mysql_real_escape_string? I'm finding (what I
>>> think are) memory leaks with a few mysql functions in php and I'm
>>> trying to figure them all out. This one is pretty vexing. Thanks in
>>> advance.
>>> Here is example code:
>>> class memTest {
>>> function __construct() {
>>> $con = mysql_connect("***************","*******","****");
>>> if (!$con) {
>>> die('Could not connect: ' . mysql_error());
>>> }
>>> mysql_select_db("*******", $con);
>>> mysql_query("SET NAMES 'utf8'");
>>> for ($i = 1; $i<=10; $i++) {
>>> $temp = 'mysql';
>>> echo "mem: ", memory_get_usage(), "\n";
>>> echo mysql_real_escape_string($temp), "\n";
>>> echo "mem: ", memory_get_usage(), "\n\n";
>>> }
>>> mysql_close($con);
>>> }
>>> }
>>> $a = new memTest();
>>> Here is what is output:
>>> mem: 43216
>>> mysql
>>> mem: 43208
>>> mem: 43208
>>> mysql
>>> mem: 43216
>>> mem: 43216
>>> mysql
>>> mem: 43224
>>> mem: 43224
>>> mysql
>>> mem: 43232
>>> mem: 43232
>>> mysql
>>> mem: 43240
>>> mem: 43240
>>> mysql
>>> mem: 43248
>>> mem: 43248
>>> mysql
>>> mem: 43256
>>> mem: 43256
>>> mysql
>>> mem: 43264
>>> mem: 43264
>>> mysql
>>> mem: 43272
>>> mem: 43272
>>> mysql
>>> mem: 43280
>> And you're worried about *164 bytes* of memory?
>>
>> Do you actually have a problem? Or are you doing "premature optimization"?
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================
>
> I am doing premature optimization. Eventually I'm going to be looping
> ~3.5 million times and inserting data into a table, so any memory leak
> will be a problem.
>
> Interestingly, when I increase the loop count to 1000, the memory
> seems to stop increasing and stabilizes at iteration 256 (2^8). Prior
> to that, the memory usage increases by 8 bytes each iteration. I'd
> like to know why it increases in the first place, and how to prevent
> it from increasing. Is there a way to force garbage collection in php?
>
Nope, no way to force garbage collection. It's not a separate thread
like in Java; the interpreter will clean things up when it gets around
to it.
But again, I wouldn't worry about it until you have a problem. With
3.5M iterations, you'll have other problems than memory leaks - for
instance, it's probably going to take a lot of time to do that many
inserts into a database.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|