Alexandru Dicu
Alexandru Dicu

Reputation: 1225

Clean memory after using a large list of data in WCF Service

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;
}

Upvotes: 1

Views: 1945

Answers (2)

abatishchev
abatishchev

Reputation: 100248

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);

Upvotes: 2

John Saunders
John Saunders

Reputation: 161773

Does your memory consumption keep going up with every call to the service? If you call the service once a minute, do you have 60*70MB of memory use after an hour? If not, then you may not have a memory leak. You shouldn't need to take any action.

Upvotes: 1

Related Questions