473,386 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Large TXT Files

Hello All
i am trying to read a large txt files -may 1 GB-, which coze my program to
hang, and i need to know if there are techniques that make this without hang
and do it faster
if you know something can help. please tell me.

that is my code , and i had run it for a txt file its size is 423 MB, and i
left my PC opened and in the next day i found that it records about 10 hours
and large number of lines about 500000 line and also i got an exception
"OutOfMemoryException"
and i dont know much about "backgroundworker", if you know a complete
example plz provid me by the link

StreamReader SR;
private void btnReadFile_Click(object sender, EventArgs e)
{
DateTime DT= DateTime.Now;
TimeSpan T = new TimeSpan(DT.Day, DT.Hour, DT.Minute, DT.Second);
if (SR != null)
{
while (!SR.EndOfStream)
{
try
{
string line = SR.ReadLine();
txtFileContent.AppendText(line);
txtFileContent.AppendText("\n");
txtFileContent.AppendText("\r");
int count = Convert.ToInt32(labNumOfLines.Text);
count++;
labNumOfLines.Text = count.ToString();
DateTime tempTime = DateTime.Now;
TimeSpan T2 = new TimeSpan(tempTime.Day, tempTime.Hour, tempTime.Minute,
tempTime.Second);
T2=T2.Subtract(T);
labTime.Text = T2.ToString();
this.Refresh();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

}
}
}
Note:txtFileContent is a TextBox

Mar 28 '07 #1
2 4284
On Mar 28, 12:48 pm, Just close your eyes and see i am trying to
read a large txt files -may 1 GB-, which coze my program to
hang, and i need to know if there are techniques that make this without hang
and do it faster
if you know something can help. please tell me.
1) Don't do the reading in the UI thread.
2) Don't try to display that much data in the UI - page it if
necessary
3) Don't constantly convert the count between string and int values.
(Just declare count with a wider scope and increment it for each
line.)
and i dont know much about "backgroundworker", if you know a complete
example plz provid me by the link
Have you looked on MSDN? There are usually examples there. Here's a
general page about WinForms threading:

http://pobox.com/~skeet/csharp/threads/winforms.shtml

Jon

Mar 28 '07 #2
On Wed, 28 Mar 2007 04:48:03 -0700, Just close your eyes and see
<Ju*********************@discussions.microsoft.com wrote:
[...]
that is my code , and i had run it for a txt file its size is 423 MB,
and i left my PC opened and in the next day i found that it records
about 10 hours and large number of lines about 500000 line and also
i got an exception "OutOfMemoryException"
The big problem is your expectation that you can read the entire file into
memory at once. The theoretical maximum for a single object in Win32 and
..NET is 2GB (the size of the virtual address space allocated to the
proces), but in reality you'll never get close to that because your
program has other stuff allocated in memory as well. Because of
fragmentation (especially a problem if you are continually reallocating an
object, as you are here with your "AppendText" calls), the maximum size
can be MUCH less than 2GB, or even much less than 1GB.

This is exactly why you have an "OutOfMemoryException". You have
literally run out of memory: there is not a large enough contiguous block
of virtual address space to contain the object txFileContent.

As Jon says, the only solution is to not try to contain the entire file's
data in memory at once. You'll have to process the file in smaller
parts. This will not only prevent the out-of-memory condition, the
processing of the file should be significantly improved (when you're
appending, every time the size of the object has to change, ALL of the
data has to be copied to a new block of memory...that copy gets slower and
slower as the object gets larger and larger).

In addition to that, and the other comments he provides, I'll point out a
very minor issue: there's no need for you to create a TimeSpan object at
the beginning. If you simply store the "Now" DateTime in DT at the
beginning, you can subtract that from DateTime.Now later, and that
subtraction will result in a TimeSpan object.

Pete
Mar 28 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Edvard Majakari | last post by:
Hi all ya unit-testing experts there :) Code I'm working on has to parse large and complex files and detect equally complex and large amount of errors before the contents of the file is fed to...
6
by: Greg | last post by:
I am working on a project that will have about 500,000 records in an XML document. This document will need to be queried with XPath, and records will need to be updated. I was thinking about...
3
by: Buddy Ackerman | last post by:
I'm trying to write files directly to the client so that it forces the client to open the Save As dialog box rather than display the file. On some occasions the files are very large (100MB+). On...
3
by: A.M-SG | last post by:
Hi, I have a ASP.NET aspx file that needs to pass large images from a network storage to client browser. The requirement is that users cannot have access to the network share. The aspx file...
2
by: jdev8080 | last post by:
We are looking at creating large XML files containing binary data (encoded as base64) and passing them to transformers that will parse and transform the data into different formats. Basically,...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
1
by: Lars B | last post by:
Hey guys, I have written a C++ program that passes data from a file to an FPGA board and back again using software and DMA buffers. In my program I need to compare the size of a given file against...
8
by: theCancerus | last post by:
Hi All, I am not sure if this is the right place to ask this question but i am very sure you may have faced this problem, i have already found some post related to this but not the answer i am...
1
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
Using .NET 2.0 is it more efficient to copy files to a single folder versus spreading them across multiple folders. For instance if we have 100,000 files to be copied, Do we copy all of them to...
17
by: byte8bits | last post by:
How does C++ safely open and read very large files? For example, say I have 1GB of physical memory and I open a 4GB file and attempt to read it like so: #include <iostream> #include <fstream>...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.