I hope that this the appropriate newsgroup for this
posting.
I have a problem with a C# program that accesses eventlogs
on various servers (read only) at our site. The program
is in a continuous loop, so eventually will use up all
available memory before crashing.
I have condensed the code down to a snippet that I have
included below.
While running, the CLR allocates memory for the
eventlogentries, but never seems to release the memory.
GC never collects the memory, even if I call it
explicitly. I even tried calling the close and dispose
methods of the eventlog class, and setting the pointer to
null.
Using a memory profiler, I can see the following:
- The instances are from System.Diagnostics
- The instance names are EventLogEntry
- The allocation stack is:
- Class1.ProcessServerLog()
- EventLogCollection.get_Item()
- EventLog.GetEntryAt()
- EventLog.GetEntryWithOldest()
Am I programmatically forgetting to do something to
release these entries? Or is there some sort of memory
leak in the EventLog class that is not releasing memory
allocated for the entry records?
Thanks for any and all help.
Bryan Hunt
=========================================================
using System;
using System.Diagnostics;
using System.Threading;
namespace Test_Event_Log_Read___Console
{
// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
Class1 myClass1 = new Class1();
while (true)
{
myClass1.ProcessServerLog
("Development","Application");
myClass1.ProcessServerLog("Development", "DNS
Server");
myClass1.ProcessServerLog
("Development", "Security");
myClass1.ProcessServerLog
("Development", "System");
myClass1.ProcessServerLog
("ntwks", "Application");
myClass1.ProcessServerLog
("ntwks", "security");
myClass1.ProcessServerLog
("ntwks", "system");
myClass1.ProcessServerLog
("w2kserver", "Application");
myClass1.ProcessServerLog("w2kserver", "directory
service");
myClass1.ProcessServerLog("w2kserver", "DNS
Server");
myClass1.ProcessServerLog("w2kserver", "file
replication service");
myClass1.ProcessServerLog
("w2kserver", "Security");
myClass1.ProcessServerLog
("w2kserver", "System");
Thread.Sleep(90000);
}
}
public void ProcessServerLog(string server, string log)
{
EventLog svrLog = new EventLog();
svrLog.MachineName = server;
svrLog.Log = log;
try
{
// Read entries from most-recent to oldest
for (int i = svrLog.Entries.Count - 1; i >= 0; i--)
{
Console.WriteLine("Reading: {0}/{1} - entry
number: {2}", server, log, i);
}
}
catch (System.InvalidOperationException e)
{
Console.WriteLine(">>>>ERROR processing event log
record: " + e.Message);
}
// Cleanup some objects
svrLog.Close();
svrLog = null;
}
}
}