1

I am trying to debug strange memory leak in C# application (uses c++/cli and c++) using Diagnostic tool and Memory usage snapshots. But i have discovered one strange problem.

When I run debug in VS2017 with Heap Profiling turned on memory consumption is constant and program runs as expected. When Heap Profiling is turned off program leaks memory which has linear increase. Work completed is same, i have progress of work printed in console and I am sure both programs have made the same work, but one uses constant memory and other has linearly increasing memory (when same work done 2x memory used). Visually it looks like when GC is fired with Heap Profiling some memory gets released, and no memory is released when Heap Profiling is not used.

Does anyone have idea how could Heap Profiling affect this? Native memory is leaked.

[EDIT1] Data from Performance Profiler -> Memory usage

Object Type Reference Count Module  
shared_ptr_cli<GeoAtomAttributes>       TestBackEnd64.dll
shared_ptr_cli<GeoAtomAttributes> [Finalization Handle] 856,275 TestBackEnd64.dll
shared_ptr_cli<GeoAtomAttributes> [Local Variable]  1   TestBackEnd64.dll
GeoAtomAttributesCli [Local Variable]   1   TestBackEnd64.dll
watbywbarif
  • 6,487
  • 8
  • 50
  • 64

1 Answers1

1

Memory that can be relased with gc should not be considered as leaked memory, it should be considered as memory that is eligible for garbage collection. Because the next time gc is performed this memory will be collected and available for new object allocations.

Other thoughts;

Gc runs on managed heap, native libraries allocates memory on the native heap. So It cannot effect the memory management of native libraries. But you should be aware of the following cases.(this might not be your case though)

If you pass pinned data structures to native code and free these handles on your Object.Finalize method (of wrapper class); in this case the pinned memory can only be collected when wrapper class is queued for finalization.Calling cleanup functions(*) of native code in the finalize method of managed class can also cause similar cases. I think these are bad practices and should not be used, instead these cleanups should be done as soon as possible.

(*) This case might cause your total process memory consumption to bloat even when there is no need for gc in the managed heap.

miskender
  • 7,460
  • 1
  • 19
  • 23
  • 1
    I was aware of potential problems, but unable to find which class causes problems because Diagnostic tool strange behavior. Real profiler discovered which class is problematic (EDIT1), and I guess that when Heap Profiling is turned on in Diagnostic tool GC is forced or something like this. This post was more about Diagnostic tool debugging than finding which class causes leak or bloat. – watbywbarif Dec 12 '18 at 09:47
  • Oh yeah, best detail about this bloat is that it started on my PC after update, so same program ran on on my PC before without problems, and still runs on other PCs without problems. So it looks like that something on my pc is broken, or windows/visual studio update made improvement to .NET which now manifests as this bloat issue. – watbywbarif Dec 13 '18 at 13:08
  • @watbywbarif It is really a strange case. Does this issue cause your app to crash because of OutOfMemory errors? AFAIK .net heap grows until it cannot grow anymore, If your pc has more memory available than other pcs, It might use more memory then other pcs. If it is related to an update, that update might be about gc thresholds.( But both of these should not cause a crash on the app) You might also want to check this https://stackoverflow.com/questions/301393/can-i-and-do-i-ever-want-to-set-the-maximum-heap-size-in-net ( especially the links shared on that topic) – miskender Dec 13 '18 at 16:28
  • I have 32GB, but other PCs have also a lot and this does not happen. I will monitor if this changes as they also get latest update. Program did never crash, but it works slower and interface gets very unresponsive until job is finished (although work is done in other thread), and then all memory get released at once. – watbywbarif Dec 14 '18 at 09:27