Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7029049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:29:14+00:00 2026-05-28T00:29:14+00:00

I have written a c# win forms application that allows the user to open

  • 0

I have written a c# win forms application that allows the user to open a log (text) file and view the log lines in a data grid. The application formats that log data so that the user can filter, search etc.

The problem I have is that when the user opens a log file > 300mb the application throws an out of memory exception.

The app first loads all of the log lines into a string array, then it loops through the log lines, adding log entry objects to a list.

var allLogLines = File.ReadAllLines(logPath).ToList();
var nonNullLogLines = allLogLines.Where(l => !string.IsNullOrEmpty(l));

this.ParseLogEntries(nonNullLogLines.ToArray());

This initial step (loading the log data into a string array) uses up about 1gb of memory in task manager.

internal override void ParseLogEntries(string[] logLines)
{
    this.LogEntries = new List<LogEntry>();
    this.LogLinesCount = logLines.Count();

    for (int i = 0; i < this.LogLinesCount; i++)
    {
        int entryStart = this.FindMessageCompartment(logLines, i);
        int entryEnd = this.FindMessageCompartment(logLines, entryStart + 1);
        int entryLength = (entryEnd - entryStart) + 1;

        if (entryStart + entryLength > this.LogLinesCount)
        {
            entryLength = this.LogLinesCount - entryStart;
        }

        var logSection = new string[entryLength];

        Array.Copy(logLines, entryStart, logSection, 0, entryLength);
        Array.Clear(logLines, i, entryLength - 1);

        this.AddLogEntry(logSection);

        i = (entryEnd - 1);
    }
}

The AddLogEntry method addes a log entry to the list (LogEntries). The for loop manages to parse about 50% of the log file, then the out of memory exception occurs. At this point task manager reports that the application is using about 1.3gb of memory.

As you can see above I have added Array.Clear to null out the portion of the log data that has been successfully parsed, as a result I would expect that as objects are being added to the collection, the amount of memory (1gb to begin with) used by the large log data array would steadily reduce, but it does not. in fact this line makes no difference to the memory usage, even if I add a GC collect periodically.

Having read about LOH, I am assuming this is because the heap is not being compressed as portions of the large array are being nulled, so it always uses the same 1gb of memory despite its contents.

Is there any way I can reduce the amount of memory being held while the data is being parsed, or a possible rework that may make better use of memory? It seems strange to me that a 300mb text file, when put into a string array, consumes 1gb of memory?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:29:15+00:00Added an answer on May 28, 2026 at 12:29 am

    Instead of your method ParseLogEntries(string[] logLines) that parses all the log lines in one go, you could instead have a ParseLogEntry(string logLine) method that parses a single line.

    If you combine this with iterating over the lines in your log file one at a time (for instance by creating yourself an enumerator), this would avoid creating the big array string[] logLines in the first place.

    One way could be like this:

    static IEnumerable<string> ReadLines(string filename)
    {
        using (TextReader reader = File.OpenText(filename))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                yield return line;
            }
        }
    }
    
    // And use the function somewhere to parse the log
    
    var logEntries = new List<LogEntry>()
    foreach (string line in ReadLines("log.txt"))
    {
        logEntries.Add(ParseLogEntry(line));
    }
    

    If you’re using .NET 4.0 or greater, you could of course just use the File.ReadLines Method as pointed out by sll in another answer, instead of creating your own method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a win forms application written in C# which has a resource file,
I have written a WinForms application (C# 2.0, VS2008) that registers several file types
I have legacy win.forms application written in pretty straightforward approach where forms communicate with
We have a WinForms application written in C# that uses the AxAcroPDFLib.AxAcroPDF component to
I have a WinForms application written in C# for .NET 3.5 that needs to
I have created an application that runs in the taskbar. When a user clicks
I have a WinForms application that was written in C# .NET 3.5. This application
I have written a .net winforms application that does some heavy processing and slows
I have written a wrapper application in .Net that starts another WinForms application with
I have Winforms application that must create write to certain configuration file during usage.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.