Home » Php » php – Why does calculating 1/(n * log(n) – n) break the computer?

php – Why does calculating 1/(n * log(n) – n) break the computer?

Posted by: admin July 12, 2020 Leave a comment

Questions:

Recently I stumbled on the problem Wolfram says sum diverges, but Mathematica gives a numerical value. It says that 1/(n * log(n) - n) is not summable (or does not converge mathematically to be correct). The interesting point is that we can still try to calculate the sum numerically, despite the fact that it is ‘not summable’. Mathematica gives ~ 6.1 as the numerical answer.

OK. I think, let’s try to reproduce that number (or something similar) in a PHP script of that series summation. My code was:

$formula = function ($n) {return 1/($n * log($n) - $n);};
$n=2;
$sum=0;

while(true) {
    $term_n = $formula($n);
    $sum += $term_n;
    if ($n++ % 100000 == 0) {
        if ($sum > 5.8)
            usleep(1000);
        echo "n=".number_format($n-1)."; sum={$sum}; error={$term_n}\n";
    }
}

My algorithm computed the answer till 5.866 and then one of two things was happening:

  1. either Ubuntu was crashing / freezing
  2. Or Linux killed my computation script process

This happened after approximately 34 million iterations.

Later I inspected how the CPU load was changing in relation to calculating more series terms.

Now, the interesting part:
at approximately 22 million iterations the cores showed difficulty switching tasks between themselves:
enter image description here

Later on, at approximately 33 million iterations, the cores reached a point of no return – they refused to work at all:
enter image description here

The question is – What’s so special about sum 5.866 that it crashes a computer? – given the fact that neither is the iteration number N very huge (just 34 million), nor the N-th term very small (just 1.7E-9) – so no reasons for a singularity.

How to&Answers:

Just a guess, but it sounds like the computer is running out of memory. Your script doesn’t explicitly allocate any memory, but perhaps your version of PHP has a bug and is leaking memory somehow.

If free memory were exhausted, that could indeed cause the running OS to break down, or prompt it to kill your script to protect itself.

If the system started heavily using swap space to compensate, that would stall your script momentarily, causing the fluctuations in the CPU graphs that you observe.

Answer:

I have narrowed down problem to it’s roots – the more function calls executed in the script – the more ram is leaked-out. And it does not matter which function(s) are called – user defined, sin(), log() or min(,) or whatever. So I suspect this is a bug in PHP core which happens at some specific conditions / OS Kernel.

Test code :

define('AMOUNT', 2000000);

if ($argc == 1)
    for ($i=AMOUNT; $i <= 3*AMOUNT; $i+=AMOUNT) 
        shell_exec('php ' . __FILE__ . " {$i}");
else 
    for ($i=0; $i < $argv[1]; $i++)
        sin(1);
    /*
    Change line above into something without a function call to stop memory leak :
    $x++; // for example
    */

produces such memory leak graph :

enter image description here