mysql_real_escape_string memory leak?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vivek@thoughtconvergence.com

    mysql_real_escape_string memory leak?

    Could someone please help me figure out why the memory usage
    fluctuates when I use mysql_real_esca pe_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("SE T NAMES 'utf8'");
    for ($i = 1; $i<=10; $i++) {
    $temp = 'mysql';
    echo "mem: ", memory_get_usag e(), "\n";
    echo mysql_real_esca pe_string($temp ), "\n";
    echo "mem: ", memory_get_usag e(), "\n\n";
    }
    mysql_close($co n);
    }
    }
    $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

  • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

    #2
    Re: mysql_real_esca pe_string memory leak?

    vivek@thoughtco nvergence.com wrote:
    Could someone please help me figure out why the memory usage
    fluctuates when I use mysql_real_esca pe_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.
    Sorry, not able to reproduce here:

    mem: 99552
    mysql
    mem: 99552

    mem: 99552
    mysql
    mem: 99552

    mem: 99552
    mysql
    mem: 99552

    mem: 99552
    mysql
    mem: 99552

    (Keep in mind that I use a amd64 processor, so the 64-bit addresses waste a
    lot of memory).


    Question: does the memory usage go up and up, or does the garbage collector
    come after some time (hint: go wild with the loop count)?? You may just be
    creating temporal variables to hold the result of mysql_real_esca pe_string,
    that aren't collected thereafter.


    --
    ----------------------------------
    Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-


    Proudly running Debian Linux with 2.6.20-1-amd64 kernel, KDE 3.5.7, and PHP
    5.2.3-1 generating this signature.
    Uptime: 02:43:00 up 18 days, 10:13, 5 users, load average: 1.72, 1.78,
    1.95

    Comment

    • Jerry Stuckle

      #3
      Re: mysql_real_esca pe_string memory leak?

      vivek@thoughtco nvergence.com wrote:
      Could someone please help me figure out why the memory usage
      fluctuates when I use mysql_real_esca pe_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("SE T NAMES 'utf8'");
      for ($i = 1; $i<=10; $i++) {
      $temp = 'mysql';
      echo "mem: ", memory_get_usag e(), "\n";
      echo mysql_real_esca pe_string($temp ), "\n";
      echo "mem: ", memory_get_usag e(), "\n\n";
      }
      mysql_close($co n);
      }
      }
      $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.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • vivek@thoughtconvergence.com

        #4
        Re: mysql_real_esca pe_string memory leak?

        On Jul 3, 6:42 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
        v...@thoughtcon vergence.com wrote:
        Could someone please help me figure out why the memory usage
        fluctuates when I use mysql_real_esca pe_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("SE T NAMES 'utf8'");
        for ($i = 1; $i<=10; $i++) {
        $temp = 'mysql';
        echo "mem: ", memory_get_usag e(), "\n";
        echo mysql_real_esca pe_string($temp ), "\n";
        echo "mem: ", memory_get_usag e(), "\n\n";
        }
        mysql_close($co n);
        }
        }
        $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...@attgl obal.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?

        Comment

        • Jerry Stuckle

          #5
          Re: mysql_real_esca pe_string memory leak?

          vivek@thoughtco nvergence.com wrote:
          On Jul 3, 6:42 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
          >v...@thoughtco nvergence.com wrote:
          >>Could someone please help me figure out why the memory usage
          >>fluctuates when I use mysql_real_esca pe_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("SE T NAMES 'utf8'");
          >> for ($i = 1; $i<=10; $i++) {
          >> $temp = 'mysql';
          >> echo "mem: ", memory_get_usag e(), "\n";
          >> echo mysql_real_esca pe_string($temp ), "\n";
          >> echo "mem: ", memory_get_usag e(), "\n\n";
          >> }
          >> mysql_close($co n);
          >> }
          >>}
          >>$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...@attg lobal.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@attgl obal.net
          =============== ===

          Comment

          • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

            #6
            Re: mysql_real_esca pe_string memory leak?

            Jerry Stuckle wrote:
            >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.
            And it would take even more time if you forced the garbage collector to run
            more frequently than needed...

            Instead of that, try experimenting with prepared statements, COPY
            statements and different DB interfaces; those decisions can lead to very
            different times when inserting that volume of information in the DB.

            --
            ----------------------------------
            Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-


            Proudly running Debian Linux with 2.6.20-1-amd64 kernel, KDE 3.5.7, and PHP
            5.2.3-1+b1 generating this signature.
            Uptime: 21:37:58 up 20 days, 5:08, 5 users, load average: 3.24, 3.37,
            1.52

            Comment

            Working...