I have a complex database conversion console app that reads from an old database, does a bunch of things, and puts into the new database.
I’m having an escalating memory problem where my mem usage (as monitored in task manager) constantly climbs and eventually slows down the process to a halt.
I’ve boiled it down to the simplest possible test POC to try and understand what’s going on.
for (int i = 0; i < 100000; i++)
{
TestObj testc = new TestObj
{
myTest = "testing asdf"
};
}
public class TestObj
{
public string myTest;
}
My thought was that each testc that is created in the loop wouldn’t survive past the end of the iteration, but the way the memory is tracking it seems like the application is holding on to every instance of testc.
I’ve done a good amount of research and experimentation but I feel like there is something I’m missing here. Shouldn’t I be able to run this and have memory utilization stay rather constant?
You’re missing one key thing about garbage collection: The GC won’t run until it needs to. So yes, even though it’s nice enough to clean up after you, it still won’t do it until more memory is needed.