user4920811
user4920811

Reputation:

when I refresh the page, memory usage get more, why? and how can I fix it?

I have a file called memory_usage_test.php. something like this:

// memory_usage_test.php

 <?php
   echo memory_get_usage(); 
 ?>

when I run that, the output is 58480 , and when I refresh the page, the output will be 58704. why? why that get more ? and how can I fix it ?


Edit:

I do it via var_dump(): here is my new file:

<?php
echo memory_get_usage().'<br>'; 
echo '<pre>';
var_dump($_SERVER);
?>

here is my output:

// just different

// ---------------------------first time-------------------------

 array(31)
 {
      ["REMOTE_PORT"]=>
      string(5) "62909"

      ["REQUEST_TIME"]=>
      int(1435672670)
 }

// ---------------------------refreshed-------------------------

 array(32)
 {

     ["HTTP_CACHE_CONTROL"]=> // This line is added
     string(9) "max-age=0"

      ["REMOTE_PORT"]=>
      string(5) "63644"

      ["REQUEST_TIME"]=>
      int(1435673309)
 }

Upvotes: 2

Views: 313

Answers (2)

kanchan
kanchan

Reputation: 359

Even I got the same issue. This is due to browser caching. You will see different values on each browser. As each browser has its own caching techniques. When you open a new page, Request headers don't force cache-control parameter. when you refresh, Following parameter is passed in request Headers by your browser.

Cache-Control - max-age=0

Even if you try with incognito mode, you ll see different result in opening link 1st time and refreshing it. I am trying to fix this by disabling cache. Will let you know once done.

You can limit the memory usage by ini_set('memory_limit',);

Upvotes: 3

mGuv
mGuv

Reputation: 631

As prompted by 'deceze' in the comments:

It appears to be the difference between a new/current request. If you go to the page in a NEW private browser window, you'll notice it goes down again. If you refresh , it goes up once and stays there.

If you compare $_SERVER, you'll notice things change between the two types of request.

The second request has an extra variable (at least on my machine):

'HTTP_CACHE_CONTROL' => "max-age=0" 

This might explain the tiny bit of extra memory.

Honestly the real answer here is: It doesn't matter. It's such a tiny difference and not like it constantly goes up. It's just the server handling subsequent requests differently to fresh ones (as expected), as your browser will have requested the page differently (due to caching control headers). This is normal and expected.

Upvotes: 3

Related Questions