Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to efficiently write a large text file in C#?

Tags:

c#

io

text-files

I am creating a method in C# which generates a text file for a Google Product Feed. The feed will contain upwards of 30,000 records and the text file currently weighs in at ~7Mb.

Here's the code I am currently using (some lines removed for brevity's sake).

public static void GenerateTextFile(string filePath) {    var sb = new StringBuilder(1000);   sb.Append("availability").Append("\t");   sb.Append("condition").Append("\t");   sb.Append("description").Append("\t");   // repetitive code hidden for brevity ...   sb.Append(Environment.NewLine);    var items = inventoryRepo.GetItemsForSale();    foreach (var p in items) {     sb.Append("in stock").Append("\t");     sb.Append("used").Append("\t");     sb.Append(p.Description).Append("\t");     // repetitive code hidden for brevity ...     sb.AppendLine();   }    using (StreamWriter outfile = new StreamWriter(filePath)) {       result.Append("Writing text file to disk.").AppendLine();       outfile.Write(sb.ToString());   } } 

I am wondering if StringBuilder is the right tool for the job. Would there be performance gains if I used a TextWriter instead?

I don't know a ton about IO performance so any help or general improvements would be appreciated. Thanks.

like image 248
jessegavin Avatar asked Aug 04 '10 15:08

jessegavin


People also ask

How do I handle large text files?

To be able to open such large CSV files, you need to download and use a third-party application. If all you want is to view such files, then Large Text File Viewer is the best choice for you. For actually editing them, you can try a feature-rich text editor like Emacs, or go for a premium tool like CSV Explorer.

How can I open a text file larger than 1gb?

Solution 1: Download a Dedicated Large File Viewer If all you need to do is read the large file, you can download a dedicated large file viewer such as the Large Text File Viewer. Such tools will open large text files with ease.


2 Answers

File I/O operations are generally well optimized in modern operating systems. You shouldn't try to assemble the entire string for the file in memory ... just write it out piece by piece. The FileStream will take care of buffering and other performance considerations.

You can make this change easily by moving:

using (StreamWriter outfile = new StreamWriter(filePath)) { 

to the top of the function, and getting rid of the StringBuilder writing directly to the file instead.

There are several reasons why you should avoid building up large strings in memory:

  1. It can actually perform worse, because the StringBuilder has to increase its capacity as you write to it, resulting in reallocation and copying of memory.
  2. It may require more memory than you can physically allocate - which may result in the use of virtual memory (the swap file) which is much slower than RAM.
  3. For truly large files (> 2Gb) you will run out of address space (on 32-bit platforms) and will fail to ever complete.
  4. To write the StringBuilder contents to a file you have to use ToString() which effectively doubles the memory consumption of the process since both copies must be in memory for a period of time. This operation may also fail if your address space is sufficiently fragmented, such that a single contiguous block of memory cannot be allocated.
like image 64
LBushkin Avatar answered Sep 19 '22 14:09

LBushkin


Just move the using statement so it encompasses the whole of your code, and write directly to the file. I see no point in keeping it all in memory first.

like image 40
Jon Skeet Avatar answered Sep 16 '22 14:09

Jon Skeet