Reputation: 23
I have a list where I read and store a large amount of text. This list continually adds more strings to it. This list can cost up to a few GB of RAM after some time.
At the same time I have a thread that reads the list or array starting from the first index and then processes it.
for(i=0;;i++)
{
string a = array[i]
Process(a)
}
And I wonder if it is possible for example to clear the first 1000 strings without losing the count or index. That way the above loop would still work without losing the index, but free up memory at the same time. Is it possible to delete only parts of an array?
Upvotes: 1
Views: 96
Reputation: 29083
The ideal approach here would be to not use an array but instead an 'iterator' that you can foreach over, collecting items as-your-go instead of collecting all the array items at once and then processing them. See here as a good starting reference. Another benefit of this approach is that it puts you in a better position to make your code multi-threaded by using Parallel.ForEach
Upvotes: 1