473,959 Members | 51,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
"OutOfMemoryExc eption"
and i dont know much about "backgroundwork er", if you know a complete
example plz provid me by the link

StreamReader SR;
private void btnReadFile_Cli ck(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.EndOfStrea m)
{
try
{
string line = SR.ReadLine();
txtFileContent. AppendText(line );
txtFileContent. AppendText("\n" );
txtFileContent. AppendText("\r" );
int count = Convert.ToInt32 (labNumOfLines. Text);
count++;
labNumOfLines.T ext = count.ToString( );
DateTime tempTime = DateTime.Now;
TimeSpan T2 = new TimeSpan(tempTi me.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:txtFileCon tent is a TextBox

Mar 28 '07 #1
2 4321
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 "backgroundwork er", 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************ *********@discu ssions.microsof t.comwrote:
[...]
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 "OutOfMemoryExc eption"
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 "OutOfMemoryExc eption". 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
2114
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 module interpreting it. First I created a unit-test class named TestLoad which loaded, say, 40 files of which about 10 are correct and other 30 files contained over 20 different types of errors. Different methods on the TestLoad class were coded so...
6
2665
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 splitting up the XML into several XML documents (perhaps 50,000 per document) to be more efficient but this will make things a lot more complex because the searching needs to go accross all 500,000 records. Can anyone point me to some best practices...
3
6385
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 these files teh time that it takes until the client displays the Save As dialog can be extrordinarily long (3+ minutes). I don't understand why. I was initiall using the format: Respnse.writefile("filepath", offset, length) but that simply...
3
2349
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 must be the only method that users receive image files.
2
1978
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, we have images that have associated metadata and we are trying to develop a unified delivery mechanism. Our XML documents may be as large as 1GB and contain up to 100,000 images. My question is, has anyone done anything like this before?
20
4352
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 is structured as an upside-down tree, and (if I remember correctly) never more than 4 levels. The site basically grew (like the creeping black blob) ... all the pages were created in Notepad over the last
1
6337
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 a software buffer of size 3MB. This is needed so as to see which function to use to read from the file. As the files used range from very large (>30GB) to very small (<3MB), I have enabled large file support and I obtain the file size by using the...
8
6404
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 looking for. My problem is that i have to upload images and store them. I am using filesystem for that. setup is something like this, their will be items/groups/user each can
1
3905
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 a single folder called 'All Files' Do we spread them out and copy them to multiple folders like Folder 000 - Copy files from 0 to 1000 Folder 001 - Copy files from 1000 to 2000 Folder 002 - Copy files from 2000 to 2999
17
10018
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> #include <string> using namespace std; int main () {
0
10264
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10074
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11325
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10793
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9998
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7529
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6305
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
5061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3656
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.