I have a WCF client and I connect to a WCF server hosted in a Windows service. In the service I read the last day entries from the Security event log. Then I parse the entries and create my own List<Data>
which I return to my WCF client to display it in a DataGrid. The problem is that in the Security event log I have 30000 entries and after I parse every entry I create 30000 of new objects of type Data
. This type is a class with 15 string properties which contains the details from the messages from the event log. After the whole process, the memory usage of the Windows service goes up with 60-70MB. Once I send this large set of data to the client, how can I lower the memory used by Windows service from 70-80MB to the default 10MB ?
Here is my code:
public List<Data> GetConnections()
{
var eventLog = new EventLog("Security");
var fromDate = DateTime.Now.AddDays(-1);
var entries = (from EventLogEntry e in eventLog.Entries
where (e.InstanceId == m_EventNumber) && e.TimeGenerated >= fromDate orderby e.TimeGenerated
select e).ToList()
.OrderByDescending(x => x.TimeGenerated);
var items = new List<Data>();
foreach(var item in entries)
{
var nData = ParseMessage(item.Message);
if (nData != null)
items.Add(ruleData);
}
return items;
}
A bit more efficient code: only one loop instead of two, less objects to collect by GC.
var q = from EventLogEntry e in eventLog.Entries
where (e.InstanceId == m_EventNumber) && e.TimeGenerated >= fromDate orderby e.TimeGenerated
order by e.TimeGenerated desc
let r = ParseMessage(e.Message)
where r != null
select r;
return new List<Data>(q);