Let's say that I have a long-running thread (A daemon or a service) and I don't want to unnecessarily accumulate memory, and I want to use the EventLog.
I have code similar to:
EventLog myEventLog = new EventLog;
myEventLog.Log = "Application";
IEnumerator eventEnumerator = myEventLog.Entries.GetEnumerator();
int eventLogIndex = 0;
Then I set up a method to go off at intervals to read from the EventLog:
while(myEventLog.Entries.Count > eventLogIndex)
{
eventEnumerator.MoveNext();
eventLogIndex ++;
// do something with eventEnumerator.Current)
}
So the enumerator keeps moving up the eventlog if more stuff is added to it. I did it this way because I read here that using the EventWritten handler can miss events if the events come in at the same time.
My question is: Would this cause memory usage to accumulate? Would used values of the eventlog be dropped from memory, or would the entire list of events stay in memory until the thread closes? And, if so, is there a way around this that would be thread-safe regarding incoming EventLogEntries?
By looking at the code you can verify that yes, there is memory usage accumulation. And the usage will grow so long as you're holding a reference to EventLog instance.
However, what is accumulated is not instances of EventLogEntry class, but a raw log data. So the memory consumption would be relatively low.
You should check it in a real life scenarios, by simulating a typical behavior of your production system and tracking the memory usage.