-1

First of all, I am talking about a server that host my website.

So, I noticed on my cPanel specs some minutes ago that my CPU usage is nearly 100% (I have a range to 80-95% ). On the other hand, my memory usage is 5-10%.

I am wondering if this server will be crashed on the future. e.g. With CPU usage = 100% what will gonna happend? Will this server may crash or not?

I notice that this cpu usage is because I am running a ajax script that it is refresh a div every 5 seconds. The content of this div is a String from 3 random arrays that they are created every 5 sec.

Moreover, I use many variables - arrays on my code (php code) and I am wondering if I must flush these variables. eg

<html>
<?
$variable1 == something
$variable2 == something
$variable3 == something

?>
</html>
<? flush($variable1) ?>

Is nessacary to do this on php (like on C++ ) ? or php has any Auto Garbage collector like Java?

EDIT: Ajax Scritp that im using

index.html

<html>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
    <script type="text/javascript">
    var auto_refresh = setInterval(
    function ()
    {
    $('#autoRefreshDiv').load('autoRefreshDiv.php').fadeIn("slow");
    }, 5000); // refresh every 5000 milliseconds
    </script>
    <body>
    <div id="autoRefreshDiv"></div>
</body>
    </html>

autoRefreshDiv.php

<?
$array1 = array("something1", "something2");
$array2 = array('something3','something4');
$array3 = array('something5','something6');
shuffle($array1);
shuffle($array2);
shuffle($array3);
echo $array1[0].'.....'.$array3[0].'.....'.$array2[0] ;
?>
John
  • 31
  • 1
  • 3
  • 8
  • 1
    PHP does have GC. RTM: http://php.net/manual/en/features.gc.php – mauris Feb 05 '13 at 22:39
  • yes. but I think my variables are destroyed if a browser read the whole html code. Am I right? – John Feb 05 '13 at 22:41
  • It sounds like your code could be optimized. For an application like this, an opcode cache like APC is especially beneficial. – G-Nugget Feb 05 '13 at 22:41
  • 4
    "*I am running an AJAX script that refreshes a div every 5 seconds*". Do you have [long polling](http://stackoverflow.com/q/333664/1037210) implementation? – Lion Feb 05 '13 at 22:42
  • @Lion yes. this script is running all the time on html page till a user close this page. – John Feb 05 '13 at 22:46
  • we need some info: Is this the only website running on the server? How much visitors do you have? What does "htop" or "top" show as most intensive process? – Green Black Feb 05 '13 at 22:46
  • @G-Nugget look a little bit my code. Can you told me any tip for optimize this? :/ – John Feb 05 '13 at 23:06

2 Answers2

1

Your question is a bit vague. Some answers: Your server will not crash if the CPU load reaches 100%. It might respond slower but crashing is not an option. You might still want to do some optimizing to your application...

Flush has not much to do with garbage collection but with output buffering. It will send some data to the user before the script is completely done.

Having many variables is not such a big problem if your not talking about many thousand. As long as you have no mad recursion or such you should be safe. Since your memory is only at 10% max, there's no danger there. Of course php.ini limits the total amount of memory your script will use.

I hope this helps a bit.

Christoph Grimmer
  • 4,210
  • 4
  • 40
  • 64
  • `Your server will not crash if the CPU load reaches 100%. It might respond slower but crashing is not an option.` .. One might argue the semantics of the term `crash` as "my" definition is process wait times that have increased to the point of not being able to serve content properly -- With that definition, "crashing" the server is possible. – Zak Feb 05 '13 at 22:48
0

You dont have to flush the variables because PHP has a GC that is triggerd if the request is closed -> evertime the ajax-request finishes. Also using many variables will most likley only stress your RAM and not the CPU.

i think that generating big random arrays every-few seconds creates the problem with your cpu usage.

gries
  • 1,135
  • 6
  • 29