skip to Main Content

I have a LAMP server on which I run a PHP script that makes a SELECT query on a table containing about 1 million rows.

Here is my script (PHP 8.2 and mariaDB 10.5.18) :

$db = new PDO("mysql:host=$dbhost;dbname=$dbname;", $dbuser, $dbpass); 
  
$req = $db->prepare('SELECT * FROM '.$f_dataset);
$req->execute();
$fetch = $req->fetchAll(PDO::FETCH_ASSOC);
$req->closeCursor();

My problem is that each execution of this script seems to consume about 500MB of RAM on my server, and this memory is not released at the end of the execution, so having only 2GB of RAM, after 3 executions, the server kills the Apache2 task, which forces me to restart the Apache server each time.

Is there a solution to this? A piece of code that allows to free the used memory?

I tried to use unset($fetch) and gc_collect_cycles() but nothing works and I haven’t found anyone who had the same problem as me.


EDIT

After the more skeptical among you about my problem posted several responses asking for evidence as well as additional information, here is what else I can tell you:

I am currently developing a trading strategy testing tool where I set the parameters manually via an HTML form. This one is then processed by a PHP script that will first perform calculations in order to reproduce technical indicators (using the Trader library for some of them, and reprogrammed for others) from the parameters returned by the form.

In a second step, after having reproduced the technical indicators and having stored their values in my database, the PHP script will simulate a buy or sell order according to the values of the stock market price I am interested in, and according to the values of the technical indicators calculated just before.

To do this, I have in my database for example 2 tables, the first one stores the information of the candles of size 1 minute (opening price, closing price, max price, min price, volume …), that is to say 1 candle per line, the second table stores the value of a technical indicator, corresponding to a candle, thus to a line of my 1st table.
The reason why I need to make calculations, and therefore to get my 1 million candles, is that my table contains 1 million candles of 1 minute on which I want to test my strategy. I could do this with 500 candles as well as with 10 million candles.

My problem now, is only with the candle retrieval, there are not even any calculations yet. I shared my script above which is very short and there is absolutely nothing else in it except the definitions of my variables $dbname, $dbhost etc. So look no further, you have absolutely everything here.

When I run this script on my browser, and I look at my RAM load during execution, I see that an apache process consumes up to 697 MB of RAM. I’d like to say that so far, nothing abnormal, the table I’m retrieving candles from is a little over 100 MB. The real problem is that once the script is executed, the RAM load remains the same. If I run my script a second time, the RAM load is 1400 MB. And this continues until I have used up all the RAM, and my Apache server crashes.

So my question is simple, do you know a way to clear this RAM after my script is executed?

2

Answers


  1. Chosen as BEST ANSWER

    After several hours of research and discussion, it seems that this problem of unreleased memory has no solution. It is simply the current technical limitations of Apache compared to my case, which is not able to free the memory it uses unless it is restarted every time.

    I have however found a workaround in the Apache configuration, by only allowing one maximum request per server process instead of the default 5.

    This way, the process my script is running on gets killed at the end of the run and is replaced by another one that starts automatically.


  2. What you describe is improbable and you don’t say how you made these measurements. If your assertions are valid then there are a couple of ways to solve the memory issue, however this is the xy problem. There is no good reason to read a million rows into a web page script.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search