php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80117 Apache and PHP leaking memory and also does not releasing it
Submitted: 2020-09-17 16:49 UTC Modified: 2020-09-17 17:12 UTC
From: gilperon at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS: WINDOWS 10 AND CENTOS 8
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: gilperon at gmail dot com
New email:
PHP Version: OS:

 

 [2020-09-17 16:49 UTC] gilperon at gmail dot com
Description:
------------
This case is similar to a previous one I reported in this bug tracker, but that other case was solved by simply using "gc_mem_caches". HOWEVER this new case DOES NOT work by using "gc_mem_caches".

I am running latest APACHE (2.5.x) + PHP (7.x) on my Win10 computer which has 8GB of RAM. All configurations are default.

When I start my Apache and check Windows Task Manager it shows the two Apache processes are using about 5 to 6 MB of RAM. Great. 

Now I saved the code below on a test.php file and execute it using my browser like this http://localhost/test.php 

The contents of that file is pretty simple, just this:

<?php

set_time_limit(0); 
ini_set("memory_limit","-1");

for ($i=0;$i<20000000;$i++) {
	
	$a[] = md5($i);
	
}

unset($a);

gc_mem_caches();

?>

After executing the code above, one of Apache process RAM went up to close to 1GB and after a few seconds it went down to the original 5-6MB (same as when I started Apache). However if I execute that exactly same code again (without restarting/reloading Apache), after the end I will be left with Apache using 400MB. 

And that's not all! If I keep executing that same code again, and again, and again... Apache RAM usage increases every single time the script finishes its execution by about 180MB. So Apache is probably leaking memory somewhere and is never releasing it. I already tried "gc_enable(); gc_collect_cycles();" but it does not work (of course, I am not using references).

Test script:
---------------
<?php

set_time_limit(0); 
ini_set("memory_limit","-1");

for ($i=0;$i<20000000;$i++) {
	
	$a[] = md5($i);
	
}

unset($a);

gc_mem_caches();

?>

Expected result:
----------------
Memory should go back to 5-6MB.

Actual result:
--------------
Memory leak or increase memory usage.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-09-17 17:12 UTC] danack@php.net
-Status: Open +Status: Not a bug
 [2020-09-17 17:12 UTC] danack@php.net
Hi,

It turns out that computers are complicated.

Exactly what is happening on your system isn't obvious, but trying to figure out of something has a memory leak or not, just by looking at Windows Task Manager isn't the way to go about it.

It could just be that "that's how Apache delays releasing memory", or "that's just how windows task manager is".

If there was a real memory leak, you'd be able to make apache crash (or at the very least slow down drastically). But seeing high memory usage numbers in Task Manager doesn't actually indicate a fault.

Until you actually see some result that causes an actual problem (like a reproducible crash, or unable to serve requests), please refrain from opening bug tickets about system metrics you are seeing, as they aren't a useful thing for people to be looking at.
 [2020-09-17 17:23 UTC] gilperon at gmail dot com
danack@php.net did you read everyhing I told you? As you execute that same script a few dozen times, it will end up consuming all system RAM memory and `error_log` file will say that apache is "Out of memory".

You said "Until you actually see some result that causes an actual problem".

So, isnt this a problem? Apache is yelling at me, saying I have no more memory after my server receives only a few requests (not at the same time, the requests are mostly one after another). If you think this is not a problem, I have no idea what you consider being a problem.
 [2020-09-17 17:29 UTC] danack@php.net
> danack@php.net did you read everyhing I told you?

Yes.


> and `error_log` file will say that apache is "Out of memory".

I'm being blind. I can't see anything about a log file. Where is this in your bug report? 

Okay, this is a wild guess; are you having a problem with a script using all the memory, and are trying to make a reproduce case?
 [2020-09-17 18:07 UTC] gilperon at gmail dot com
My friend, I will copy the phrase where I told you my Apache is crashing on RAM:

   "If I keep executing that same code again, and again, and again... Apache RAM usage increases every single time the script finishes its execution by about 180MB."

So, if you have a VPS with 2GB of RAM (which is already PRETTY LOW) and you run my sample code, you will probably crash your Apache with less than 15 requests.

I also have to clear one thing you said "are you having a problem with a script using all the memory".

NO! I am not having problems with my script using all my RAM. No! My script runs fine, it starts fine, finishes fine with lots of RAM to spare. HOWEVER, if I execute that same exactly script over, and over, and over... in less than 15 requests I will end up consuming my entire VPS RAM memory (2GB) with TRASH/CACHE.

I am not complaning about my script using lots of memory, NO! I am complaining about PHP leaving lots of trash behind AFTER executing my code, so this trash adds up in a short time and crashes apache.
 [2020-09-17 18:19 UTC] danack@php.net
> So, if you have a VPS with 2GB of RAM (which is already PRETTY LOW) and you run my sample code, you will probably crash your Apache with less than 15 requests.

"probably"

Or maybe, the Apache memory manager will kick in and free the memory.

As I said, if you are seeing it crash that would be one thing. But you're saying:

> HOWEVER, if I execute that same exactly script over, and over, and over... in less than 15 requests I will end up consuming my entire VPS RAM memory (2GB) with TRASH/CACHE.

Which reads to me as if you think it will crash....but it probably won't.
 [2020-09-17 23:20 UTC] gilperon at gmail dot com
First of all, it's not a matter if apache will crash or not. It's about `gc_mem_caches()` and `gc_collect_cycles()` not working as they should. Because in some circunstances those 2 functions look to work fine (as the `explode` example I gave you in another bug report - on that thread the function `gc_mem_caches()` worked perfectly, completely reseting memory usage to initial values when apache was restarted).

HOWEVER now, with the new code I provided you, those 2 functions are not working as expected.

So I think the main issue here is not wheter apache will crash or not, but WHY those 2 functions are not doing what they are supposed to? With the code I provided you on this thread I am sure `gc_collect_cycles()` should not help anything, BUT `gc_mem_caches()` should help clearing the entire cache.
 [2020-09-18 13:08 UTC] gilperon at gmail dot com
No I am getting this error in my apache:

VirtualAlloc() failed: [0x000005af] The paging file is too small for this operation to complete.

Clearly memory is not being cleaned with the 2 functions I told you. So yeah, now Apache is crashing.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 10:01:28 2024 UTC