streamwriter consumes memory??

J

Jon

I'm puzzled..
let's say I have an array of streamwriters. a lot..

'the code is something like
dim sw() as streamwriter
redim sw(10000)

'instantiate
for i as int16 = 0 to 9999
sw(i) = new streamwriter(arrayOfFileName(i))
next
'write to the files
for i as int16 = 0 to 9999
sw(i).write("123 ")
next

I monitor the memory from task manager. I can understand
that memory level drops when I instantiate the
streamwriters.
However, I cannot understand, during the streamwriter
writing to the files, the memory drops with each write.

I tried closing all the streamwriters after the
writing... but the memory is not released. is it waiting
for GC?

Anyone knows how I can free the memory so I don't run out
of memory half-way thru writing the files?
 
B

Bela Istok

Why you need a lot of StreamWriters, it's a waste of memory, in the example
you show us, you only write one file at time, why not use only 1 stream
writer?
 
G

Guest

hi Bela,

everytime some data comes in, I want to write(and append)
these data to the many files. Also, I don't want to close
these files. I want to append the data. Finally when the
data stops coming in, I can close these files, and will
get the final files.

if I write to these files one by one, each time the data
comes in,
sw.open
sw.write
sw.close,
it will take forever.
 
J

Jarod_24

As far as i know, .Close() isn't the same as calling the Garbage Collection.
if you set the Variable to nothing the memory will release the next time
around the GC runs
If you call the .Dispose() method, then GC will immediatly clean up and
release memory. (i don't remember is StramWriter implements iDispose)

Read about Dispose and Nothing in the MSDN documentation.
 
P

Philip Rieck

Actually, calling .Dispose() will release any resources the object is
holding, but will not cause the GC to clean up immediately.

To clean up after these, just make sure you call .dispose() as soon as you
can, and do not hold references to them. the garbage collector will
automatically reclaim the memory when the "pressure" is high enough (the
system is running low). You can also force a collection by calling
GC.Collect()
 
B

Bela Istok

If you have trouble with the memory used you need to limit the number or classes that you instance, because each class use some memory and resources from the machine.



I think that you can implement something like a queue, were all the info from the files get in a temporal buffer (0nly 1 queue with all the pending info), and later you write the info in the files, 10 at time, or something like that. I guest that it's a better approach that have 1000 Streams Writers opened all the time because you don't write all the time to the Streams Writers.


--
Bela Istok
MVP C#
Caracas, Venezuela
hi Bela,

everytime some data comes in, I want to write(and append)
these data to the many files. Also, I don't want to close
these files. I want to append the data. Finally when the
data stops coming in, I can close these files, and will
get the final files.

if I write to these files one by one, each time the data
comes in,
sw.open
sw.write
sw.close,
it will take forever.
 

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