❔ Best way to you Read lot of files?
I need something like a Filestream but where I can change the file thats read but each time I want to read another file I need to create a new FileStream which does extra allocation. See here:
Is there a better way which dont generate as much or even better, no(extra) garbage?
8 Replies
I guess you might try File.ReadAllText() method.
Seems like this allocates even more per use than a FileStream
did you try to set the filestreamoption buffersize to 0, according to docs this would disable the buffer and hopefully the allocation for it..
never used it tho so take it with a grain of salt
what im more interested in tho is, what is it for? are you trying read thousands of files at once?
btw it is recommended to use the usingpattern since filestream implements idisposable, even the docs say to better use dispose
btw you are always creating new filestreams but never close them, so potential memoryleak
setting it to 0 or 1 does disable it but there are still other allocations, not that big anymore (without the buffer Filestream does allocate ~200b instead of 4kb)
Well I dont know how many Files need to be read(also because of user input) but I wanted to make it flexible so if I need to read thousands of files it should do so and possible without much or any extra allocations so it wont run the Collector 2 much
I Could make a static Filestream , but that does not change the problem with new Files
i dont think you can completely get rid of all allocations.. to my understanding, to be able to read files the OS has to provide handles which would have to be managed, therefore at least some allocations per file
to be honest, did you do any benchmarking/testing? is it even a problem currently? if not use your time more productively instead of saving couple megs of ram
Yea I did Benchmark with Benchmarkdotnet and at this point it is more in my own interest rather than an actual real scenario problem. But is there maybe some unsafe way can solve this? Thanks for any info so far!
my personal opinion is that as long you arent running into any problems you should let the garbagecollector do its job. just properly dispose the streams and the collector will most likely free the memory in batches when necessary.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.