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 7822571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:57:22+00:00 2026-06-02T07:57:22+00:00

i have a VERY large text file to parse (~2GB). for various reasons i

  • 0

i have a VERY large text file to parse (~2GB). for various reasons i have to process the file line-wise. i do this by loading the text file to memory (the server I’m running the parser on has way enough memory) with var records = Regex.Split(File.ReadAllText(dumpPath, Encoding.Default), @"my regex here").Where(s => !string.IsNullOrEmpty(s));. this consumes RAM equivalent to the size of the text file plus a few MBs for the IEnumerable overhead. so far so good.
then i go over the collection with foreach (var recordsd in records) {...}

here comes the interesting part. i do a lot of string manipulation and regex-ing in the foreach loop. then the program quickly bombs with an System.OutOfMemoryException, even though i never use more than a few kB in the foreach loop.
i made a few memory snapshots using the profiler of my choice (ANTS memory profiler), seeing millions and millions of Generation 2 string objects on the heap, consuming all available memory.

seeing that, i – just as a test – included a GC.Collect(); at the end of each foreach iteration, and voila, problem solved and no more out of memory exceptions (sure enough because of the permanent garbage collections the program now runs painstakingly slow). The only memory consumed is the size of the actual file.

now i can’t explain why this happens and how to prevent it. to my understanding, the very moment a variable goes out of scope and has no more (active) references to it should be marked for garbage collection, right?

on another side note, i tried to run the program on a really massive machine (64GB RAM). the program finished successfully but never released a single byte of memory before it was closed. why? if there are no more references to an object plus if the object goes out of scope, why is the memory never released?

  • 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-06-02T07:57:24+00:00Added an answer on June 2, 2026 at 7:57 am

    now i can’t explain why this happens and how to prevent it. to my understanding, the very moment a variable goes out of scope and has no more (active) references to it should be marked for garbage collection, right?

    No. There’s no such thing as being "marked" for garbage collection, and variables aren’t garbage collected: objects are. And an object which is already in gen2 won’t be garbage collected until the next time the GC looks at gen2, which is relatively rare.

    for various reasons i have to process the file line-wise.

    Then there’s your answer: use File.ReadLines if you’re using .NET 4, and write the equivalent (it’s easy) if you’re not. Then you don’t need the whole file in memory at a time – just one line. Your memory usage should absolutely plummet. (Note that that’s ReadLines, not ReadAllLines – the latter will read the whole file into an array of strings, which isn’t what you want.)

    on another side note, i tried to run the program on a really massive machine (64GB RAM). the program finished successfully but never released a single byte of memory before it was closed. why?

    If you’re talking about memory that the process takes from the operating system, I don’t believe that the CLR ever releases memory. I assume it takes the approach that if you’ve used that much memory once, you’ll probably use that much again.

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

Sidebar

Related Questions

I have a very large comma-seperated text-file. I need to display this in a
I have a very large file that looks like this (see below). I have
I have a very large text file and I need to gather data from
I have a very very large text file (much larger than can fit in
I have a very large text file and all I need to do is
On a Linux system, I have a very large text file and I need
I have a tab-delimited text file that is very large. Many lines in the
I have a very large text file, and my iPad program needs to occasionally
I am using Microsoft SQL Server 2008. I have a very large text file
I have a very large (~8 gb) text file that has very long lines.

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.