Results 1 to 7 of 7

Thread: [RESOLVED] Working with massive strings - memory errors

  1. #1

    Thread Starter
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819

    Resolved [RESOLVED] Working with massive strings - memory errors

    I'm working on some VB.Net 2008 code that extracts a massive chunk of data from a database and then formats it and sticks it into a text file.

    Currently, it builds everything by spooling through a data table and putting the results into an IO.StringWriter, using the .WriteLine method for each record, and then at the end dumps it all out to a text file using the FileSystem.WriteAllText method. Unfortunately, some of the extracts are massive, and it's giving System.OutOfMemoryException errors part-way through the table.

    Does anyone know of a way to get around this, short of writing each line out to the text file independently?

    Any help would be gratefully received...
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Working with massive strings - memory errors

    Quote Originally Posted by InvisibleDuncan View Post
    Does anyone know of a way to get around this,
    Yes, you need to write each line out to the text file independently.
    Quote Originally Posted by InvisibleDuncan View Post
    short of writing each line out to the text file independently?
    Oh.

    Seriously, though, why would you try not to do that? The problem is with having objects that are too large (specifically, larger than 85,000 bytes). These objects go on the Large Object Heap and that's something you really want to avoid if you can.
    So if the problem is dealing with objects that are too large, why wouldn't you try the obvious thing and work with smaller objects?

  3. #3

    Thread Starter
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819

    Re: Working with massive strings - memory errors

    Quote Originally Posted by Evil_Giraffe View Post
    Seriously, though, why would you try not to do that?
    Simply because the writing of each line is a long way down the function chain from where the file name is determined (as it happens, the data is all collated before the file name is determined). I was hoping to avoid passing the file name as a parameter through each function call, and also avoid expanding the scope of the file name variable.

    I also (probably wrongly) assumed that it would operate faster if it was handled in memory rather than writing to the hard disk after every record.

    Thanks for the impressively fast reply!
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  4. #4
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Working with massive strings - memory errors

    Instead of writing to a StringWriter, then, perhaps add each line as its own string to a List(Of String), and then loop through that when you are writing the file. You needn't change the architecture, just don't mush all the strings into one!

  5. #5

    Thread Starter
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819

    Re: Working with massive strings - memory errors

    Hmmmm - that sounds like a very promising plan! I'll give that a whirl and see what happens.

    Incidentally, I'm just back from a couple of weeks in Kenya, and all the giraffes I encountered were quite friendly. Their coding wasn't up to much, though, so maybe they were just too nice.
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  6. #6
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Working with massive strings - memory errors

    Don't know whether coding comes into it, but I eat more leaves than I should, then other giraffes may die.

  7. #7

    Thread Starter
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819

    Re: Working with massive strings - memory errors

    That's quite an explanation!

    Your suggestion of a list successfully avoided the memory error, but as I feared it was hideously slow to save the file line by line. I've therefore come up with a compromise: when it comes to saving the file, I collate the list into text blocks of 3,000 records each, and save them like that.

    All in all, a successful result. Thanks for the advice.
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width