Out of memory

C

CSharper

I have a resource file that i open to write.After writing almost 300MB
I call the close method and It failed with out of memory. One thing I
learned that, in C#, resource writers doesn't write data directly to
disk instead it writes to memory and then at the end on either close/
dispose, it will write the data to disk. I am thinking of compressing
the data still, I will grow into the size very fast.
Is there an alternate solution to this? Other than compressing the
data file?
Thanks.
 
D

David Fišer

Well if is it dedicated from Stream there could be a Flush() method. It
flushes the buffer and write data directly..
 
I

Ignacio Machin ( .NET/ C# MVP )

I have a resource file that i open to write.After writing almost 300MB
I call the close method and It failed with out of memory. One thing I
learned that, in C#, resource writers doesn't write data directly to
disk instead it writes to memory and then at the end on either close/
dispose, it will write the data to disk.  I am thinking of compressing
the data still, I will grow into the size very fast.
Is there an alternate solution to this? Other than compressing the
data file?
Thanks.

Hi,

How are you writing it?
You can always call flush, but I doubt that the buffer is that big,
post some code please
 
C

CSharper

Hi,

How are you writing it?
You can always call flush, but I doubt that the buffer is that big,
post some code please

Here is the simple code I have used to resolve the out of memory
issue. I thought I will close and reopen the resource to append. But
it seems, the append part doesn't work. It doesn't throw any exception
but it does not write the file. What could be the problem?
static void Main(string[] args)
{
FileStream fs = new FileStream("items.resources",
FileMode.OpenOrCreate, FileAccess.Write);

// Open a resource writer to write from the stream.
IResourceWriter writer = new ResourceWriter(fs);

// Add resources to the resource writer.
writer.AddResource("String 1", "First String");
writer.AddResource("String 2", "Second String");
writer.AddResource("String 3", "Third String");

// Write the resources to the stream, and close it.
writer.Close();
fs = new FileStream("items.resources",
FileMode.Append, FileAccess.Write);

// Open a resource writer to write from the stream.
writer = new ResourceWriter(fs);

// Add resources to the resource writer.
writer.AddResource("String 4", "Fourth String");
writer.AddResource("String 5", "Fifth String");
writer.AddResource("String 6", "Sixth String");

// Write the resources to the stream, and close it.
writer.Close();
}
 
C

CSharper

How are you writing it?
You can always call flush, but I doubt that the buffer is that big,
post some code please

Here is the simple code I have used to resolve the out of memory
issue. I thought I will close and reopen the resource to append. But
it seems, the append part doesn't work. It doesn't throw any exception
but it does not write the file. What could be the problem?
static void Main(string[] args)
        {
            FileStream fs = new FileStream("items.resources",
       FileMode.OpenOrCreate, FileAccess.Write);

            // Open a resource writer to write from the stream.
            IResourceWriter writer = new ResourceWriter(fs);

            // Add resources to the resource writer.
            writer.AddResource("String 1", "First String");
            writer.AddResource("String 2", "Second String");
            writer.AddResource("String 3", "Third String");

            // Write the resources to the stream, and close it.
            writer.Close();
            fs = new FileStream("items.resources",
       FileMode.Append, FileAccess.Write);

            // Open a resource writer to write from the stream.
            writer = new ResourceWriter(fs);

            // Add resources to the resource writer.
            writer.AddResource("String 4", "Fourth String");
            writer.AddResource("String 5", "Fifth String");
            writer.AddResource("String 6", "Sixth String");

            // Write the resources to the stream, and close it.
            writer.Close();
        }- Hide quoted text -

- Show quoted text -

Here is the correct code, sorry

FileStream fs = new FileStream("items.resources",
FileMode.OpenOrCreate, FileAccess.Write);

// Open a resource writer to write from the stream.
IResourceWriter writer = new ResourceWriter(fs);

// Add resources to the resource writer.
writer.AddResource("String 1", "First String");
writer.AddResource("String 2", "Second String");
writer.AddResource("String 3", "Third String");

// Write the resources to the stream, and close it.
writer.Generate();
writer.Close();
fs.Dispose();
fs.Close();
fs = new FileStream("items.resources",
FileMode.Append, FileAccess.Write);

// Open a resource writer to write from the stream.
writer = new ResourceWriter(fs);

// Add resources to the resource writer.
writer.AddResource("String 4", "Fourth String");
writer.AddResource("String 5", "Fifth String");
writer.AddResource("String 6", "Sixth String");

// Write the resources to the stream, and close it.
writer.Generate();
writer.Close();
fs.Dispose();
fs.Close();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top