0

I am creating an STA thread as I am basically using a UI component within a library that has functionality I want without actually having any UI (exporting tables to Excel).

System.Threading.Thread thread = new System.Threading.Thread(() =>
            {
                _reportRunner.RunReport(report);
            });

thread.SetApartmentState(System.Threading.ApartmentState.STA);
thread.Start();
thread.Join();

Above is where the magic happens, it runs fine however it does not dispose of the objects properly and I am getting huge memory leaks as some of these data tables are very large.

I can find various information on STA threads and memory leaks but not much on solving them, is there anyway to free up the memory, I've tried manually forcing the garbage collector but a) it doesn't work and b) I really don't want to do that.

My application is effectively a console app, no WPF in sight.

I have used dotMemory memory profiler to understand this, my app is using 8gb of memory which is meant to be released once a report is produced and exported but it is not.

I am using Dev express WPF pivotgridcontrol which has an export function

Simon Nicholls
  • 635
  • 1
  • 9
  • 31
  • Which tool you are using for memory profiling? – Sham Oct 10 '18 at 08:52
  • It's far more likely to be due to RCWs for Excel COM objects than the threading. See here for more info on how to properly clean up Excel objects after use: https://stackoverflow.com/questions/158706/how-do-i-properly-clean-up-excel-interop-objects – Bradley Smith Oct 10 '18 at 08:54
  • 2
    Every time someone mentions a memory leak my eyes glaze over.. How did you prove this to your self, what tools did you use, why do you think its a problem, did you research garbage collection and how .net manages memory before asking your question – TheGeneral Oct 10 '18 at 09:01
  • 1
    Without knowing this information its practically impossible to answer your question, and any answer would be an unsubstantiated wild guess – TheGeneral Oct 10 '18 at 09:05
  • @Saruman and every time someone comes on stack overflow and is incredibly patronising my eyes glaze over also, stack overflow is meant to be a friendly place not everyone is asking moronic questions...I have used dotMemory for memory profiling, it is not difficult to see there is a memory leak even before I profiled. My app is stuck at around 7GB of memory, it is a batch app that should produce reports, export them and then release said memory but it is not releasing that memory. – Simon Nicholls Oct 10 '18 at 09:19
  • 1
    There was no offense intended, however this is pertinent information to the question, you need to state your reasoning, your evidence, your train of thoughts (which you have), what you expect, and why you come to the conclusion. – TheGeneral Oct 10 '18 at 09:22
  • 1
    I am having a very similar problem. We are using some GUI controls in a windows service to render charts, which require STA thread exactly like you have here. GC is not cleaning up anything. Have you found anything yet? – grinder22 Jan 31 '19 at 20:11

0 Answers0