PHP/MySQL seems to eat my system's memory

Codes here !

Moderators: egami, macek, gesf

Post Reply
defaria
New php-forum User
New php-forum User
Posts: 2
Joined: Tue Jul 21, 2020 8:18 pm

Tue Jul 21, 2020 8:24 pm

I have a PHP function which selects all rows of a table and all fields in the row for the purposes of totalling up all of their lengths. This is a web page and it spits out the total size of the table. The problem is if I refresh this page a couple of times while examining my systems memory usage it quickly increases until I'm out of memory.

I've ensured that I close the database after performing this totalling function and have even put in calls to mysqli_free_result but the behaviour persists - after about 5 refreshes and my system is out of memory. Why is this happening and how can I fix it?

Please let me know if you need any other info.

Thanks.
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 1190
Joined: Mon Feb 22, 2016 5:52 pm

Wed Jul 22, 2020 3:09 am

The database will close when you exit PHP sending the page to your browser, do you get the same result when you refresh?

If the function and SQL doesn't change between calls there must be something else going on. Without any code, there's no way of telling what's going on for certain.
defaria
New php-forum User
New php-forum User
Posts: 2
Joined: Tue Jul 21, 2020 8:18 pm

Wed Jul 22, 2020 5:11 am

The page does close and displays the results however memory continues to grow with every refresh.

Code:

Code: Select all

function OpenDB() {
  global $db;

  $db = mysqli_connect("localhost", "<username>", "<password>")
    or DBError("OpenDB: Unable to connect to database server", "Connect");

  mysqli_select_db($db, "MAPS")
    or DBError("OpenDB: Unable to select database", "adefaria_maps");
} // OpenDB

function CloseDB() {
  global $db;

  if (isset ($db)) {
    print "Closing db<br>";
    mysqli_close($db);
  } // if
} // CloseDB

function Space() {
  global $userid, $db;

  // Tally up space used by $userid
  $space = 0;

  $statement = "select * from email where userid = \"$userid\"";

  $result = mysqli_query($db, $statement)
    or DBError("Space: Unable to execute query: ", $statement);

  while ($row = mysqli_fetch_array ($result)) {
    $msg_space =
      strlen($row["userid"])    +
      strlen($row["sender"])    +
      strlen($row["subject"])   +
      strlen($row["timestamp"]) +
      strlen($row["data"]);
    $space += $msg_space;
  } // while

  mysqli_free_result($result);

  return $space;
} // Space
The function in question is called Space which is called from the main page with:

Code: Select all

OpenDB();
Space();
CloseDB();
BTW (Side note): This forum web page has that nice green image at the top with the moving graphics, some sort of .png thing. Anyway, if displayed in my browser (Chrome Beta 84.0.4147.89) hogs one of my CPUs at 100%. I say displayed because if I merely scroll down so that that image is not being shown the CPU usage drops back down. The image is great but hogging the CPU it bad. Perhaps if it was static it would be much better. (Not sure how to notify the webmaster here).
Post Reply