|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-08-12 23:53 UTC] James_Brigham at hotmail dot com
Description:
------------
I have not compiled any additional modules. The recommended default php.ini is being used. There is a memory leak when initiating a new COM object, then running a query against it. After the query even if you use the unset()function on all the variables, you cannot recover the full memory, it just keeps climbing. Do you have any ideas of how to clear the memory in this simple code I have below?
Thanks for taking the time to read this, it is appreciated.
JB
Reproduce code:
---------------
#!c:\php5\ -q
<?php
$aryHost = array("cp2kisu01","cp2kisu02","cp2kisu03"); //this is a list of servers running IIS 5 on a windows 2000 platform
$sProcName = "dllhost"; //give the name of the process to check
$wmi = ""; //our comm object
$threadRecords = ""; //comm object result set
$threadcount = ""; //temp variable for looping though each return record.
echo("\r\n\r\n" . str_repeat("*",79) . "\r\n");
echo("Program Start\r\n"); //this is written for command line...
echo("\r\n" . str_repeat("*",79) . "\r\n");
while(true) //loop until the end of time
{
for ($i = 0; $i < count($aryHost); $i++) //loop through each server in the array
{
$wmi = new COM("WinMgmts:{impersonationLevel=impersonate}//{$aryHost[$i]}/root/cimv2"); //initialize new COM object
$threadRecords = $wmi->ExecQuery("SELECT * FROM Win32_PerfRawData_PerfProc_Process WHERE Name = '" . $sProcName . "'"); //execute a wmi query against our com object
foreach ($threadRecords as $threadcount) //loop through the resules
{
echo("Server:[" . $aryHost[$i] . "] Name: " . $threadcount->Name . " Count: " . $threadcount->ThreadCount . "\r\n"); //for each result output basic info to the screen
}
echo("\r\nClearing all the used memory for server: " . $aryHost[$i] . "\r\n\r\n");
unset($wmi); //clear object memory
unset($threadRecords); //clear returned records memory
unset($threadcount); //clear our temp variable we use for our loop
}
echo("\r\n\r\n" . str_repeat("*",79) . "\r\n");
echo("Sleeping.. so we can check the amount of memory consumed now..\r\n"); //this is written for command line...
echo("\r\n" . str_repeat("*",79) . "\r\n");
sleep(15); //sleep to keep this from being a tight loop and give time to check the memory loss each loop
}
?>
Expected result:
----------------
D:\Appsupport\IIS_Monitor>php wmi.php
*******************************************************************************
Program Start
*******************************************************************************
Server:[cp2kisu01] Name: dllhost Count: 318
Server:[cp2kisu01] Name: dllhost Count: 10
Clearing all the used memory for server: cp2kisu01
Server:[cp2kisu02] Name: dllhost Count: 318
Server:[cp2kisu02] Name: dllhost Count: 10
Clearing all the used memory for server: cp2kisu02
Server:[cp2kisu03] Name: dllhost Count: 333
Server:[cp2kisu03] Name: dllhost Count: 10
Clearing all the used memory for server: cp2kisu03
*******************************************************************************
Sleeping.. so we can check the amount of memory consumed now..
*******************************************************************************
Server:[cp2kisu01] Name: dllhost Count: 317
Server:[cp2kisu01] Name: dllhost Count: 10
Clearing all the used memory for server: cp2kisu01
Server:[cp2kisu02] Name: dllhost Count: 310
Server:[cp2kisu02] Name: dllhost Count: 10
Clearing all the used memory for server: cp2kisu02
Server:[cp2kisu03] Name: dllhost Count: 328
Server:[cp2kisu03] Name: dllhost Count: 10
Clearing all the used memory for server: cp2kisu03
*******************************************************************************
Sleeping.. so we can check the amount of memory consumed now..
*******************************************************************************
^C
D:\Appsupport\IIS_Monitor>
Actual result:
--------------
we gets the counts expected.. but we have a memory loss every run from this very simple script. Basically I would like to be able to loop this across 20 servers every 45 seconds.. but with the memory consumption I cannot feasibly do that. Every added server causes loss of additional memory.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 06:00:01 2025 UTC |
if you comment out the query lines, then the memory leak seems to go away... the lines I commented out were: //$threadRecords = $wmi->ExecQuery("SELECT * FROM Win32_PerfRawData_PerfProc_Process WHERE Name = '" . $sProcName . "'"); //execute a wmi query against our com object //foreach ($threadRecords as $threadcount) //loop through the resules //{ //echo("Server:[" . $aryHost[$i] . "] Name: " . $threadcount->Name . " Count: " . $threadcount->ThreadCount . "\r\n"); //for each result output basic info to the screen //}