Memory leak and .NET -- like oil and water :-
I've written a distributed system for parallel processing of large tasks. Each machine has an agent on it that sends processor usage information back to the controlling apps so that tasks can be distributed based on current system load. Obviously I need to do this on a timer, so that I always have current CPU utilization info ... The problem is that I can't seem to stop my agent's memory usage from getting out of hand, and am beginning to think this is a bug in the implementation of the ManagementObject class
a code example is below ... I obviously cut out all of the boilerplate windows form initialization code here, but I think you can get the point... The memory leak occurs in the call to ManagementObject.get(). I'd be more than happy to explicitly clean up the memory, but can't seem to find any methods that accomplish that
Any advice would be appreciated, or a link to some really cheap memory !! :-
public class MyForm : System.Windows.Forms.For
private System.Windows.Forms.Timer m_timerGetCPUInfo
private ManagementObject[] arrCPUObjects
private int nCPUs = 2
public MyForm(
ManagementScope managementScope = new ManagementScope()
arrCPUObjects = new ManagementObject[nCPUs]
ManagementPath mPath_CPU = new ManagementPath()
for(int i=0;i<nCPUs;i++
mPath_CPU.RelativePath = "Win32_PerfRawData_PerfOS_Processor.Name='" + i + "'"
arrCPUObjects[i] = new ManagementObject(managementScope,mPath_CPU,null)
m_timerGetCPUInfo = new System.Windows.Forms.Timer()
m_timerGetCPUInfo.Tick += new EventHandler(CheckStatusTimer)
m_timerGetCPUInfo.Interval = 500
m_timerGetCPUInfo.Start()
private void CheckStatusTimer(object sender, System.EventArgs e
//------------------------------------------------------/
// Start memory eating loop !! :-
//------------------------------------------------------/
for(int i=0;i<nCPUs;i++
arrCPUObjects[i].Get()
ulong temp1 = (ulong)arrCPUObjects[i].Properties["PercentProcessorTime"].Value
ulong temp2 = (ulong)arrCPUObjects[i].Properties["TimeStamp_Sys100NS"].Value
}