0

Context:

A GUI Tool built using Windows form is basically doing the following 4 things

  1. Fetch logs from a biometric device (by CZKEM interop service) in every 5 seconds (by Form.Timer)
  2. Save featured logs into the MySql database locally (by ADO)
  3. Make status changes in a file on the drive i.e HasSavedLogs (by StreamWriter)
  4. It has to keep running and show error logs if something goes wrong with the above process

It has been taken care of disposing of all the IDisposealbes like for file(streamWriter) and DB(MySqlConnection, MySqlDataAdapter, MySqlCommand) are taken care of by the using statements. I can't unsubscribe from the timer event as I have ensure process keep running.

The Problem:

With the passage of time, it starts to consume a lot of memory which can be seen in Task Manager, after 1 day of execution it starts to take more than 1 GB of RAM. I ran a DotTrace as a profiler tool to further dig down what is taking such large memory. That pointed out a spike in unmanaged memory. Which can be seen in the following snapshot. It's good DotTrace pointed out the issue but I need to find out who is taking up all this memory. I have shared the DotTrace snaps, can someone help me on this?

enter image description here

enter image description here

Edit: (CODE: In @Jimi's comment response) enter image description here

Malik Khalil
  • 6,454
  • 2
  • 39
  • 33
  • Usually images are the culprit, are you always disposing those? – Charlieface Jun 06 '23 at 16:08
  • Nop @Charlieface this small app does not deal with images at all – Malik Khalil Jun 06 '23 at 16:13
  • Are you creating lots of Winforms objects (controls, forms etc) and not disposing? Another culprit can be `XmlSerializer` see https://stackoverflow.com/questions/23897145/memory-leak-using-streamreader-and-xmlserializer and any other dynamic code generation – Charlieface Jun 06 '23 at 16:14
  • Ermm Nah @Charlieface , no dynamic control generation is happening. In fact, the App is too basic that it has only 3 text fields, 2 buttons and two text areas to show error logs. – Malik Khalil Jun 06 '23 at 17:03
  • It just asks to specify an IP and Port from textFileds of the biometric device and fetch logs(ints, strings, dates hardly 10 properties) from it and convert them to type and save them to the database. and No XML is involved in that whole process @Charlieface – Malik Khalil Jun 06 '23 at 17:10
  • 2
    Some code needed. Maybe interop with CZKEM requires that you take care of the objects you handle on your side. Something like `Marshal.PtrToStringUni()`, what you allocate for `Marshal.PtrToStructure()`, other types of allocations etc. – Jimi Jun 06 '23 at 17:14
  • I just updated the question with the code, please find it marked as Edit @Jimi – Malik Khalil Jun 06 '23 at 17:36
  • I doesn't look like it's here. If that represents an interop call, it shouldn't leak (unless it's bugged). You're accumulating strings in class objects stored in a collection, but that should increase Generation 1, then go straight to Generation 2 after evaluation in Gen 1. But it appears you clean this collection at some point and, anyway, that's .NET stuff, not unmanaged stuff -- Are you using third-party Controls that may be active in this context, as Progress Bars or similar? Any third-party Custom Controls or even a Framework? Anything that is updated frequently, for presentation? – Jimi Jun 06 '23 at 20:01
  • Please do not post images of code or data, please paste in as text only – Charlieface Jun 06 '23 at 20:15
  • We're going to need to see what this interop service does. Is it your code? Perhaps there is a leak there. If it's a NuGet or Github project please say. – Charlieface Jun 06 '23 at 20:17
  • Thanks for pointing it out @Charlieface, Aa yes thats mine, the one responsible for fetching the logs. – Malik Khalil Jun 07 '23 at 03:25
  • Nah @Jimi, no unmanged stuff or controls are being used EXCEPT the device service (_device).That's an interop service allowing me to communicate with the physical device. Also the maximum communicating I am doing with the device would hardly be calling 5 interop methods i.e 1. Enable device. 2. Check to confirm if it has new logs. 3. Get the logs. 4. Delete them from device. 5. Finally set the enabled. Thats all my use of interop service methods – Malik Khalil Jun 07 '23 at 03:35
  • We can't help you without code. Let's see the interop code, I suspect something's going on there – Charlieface Jun 07 '23 at 06:18
  • 1
    You should sort by new object count to find objects which are small in managed code, but consume large amounts of unmanaged code. If nothing is there you need to use a different profiler which can also check unmanaged heaps. Personally I would resort to ETW with VirtualAlloc profiling. How is the memory increasing? Small increments over time, or some big spikes? – Alois Kraus Jun 09 '23 at 10:50
  • some suggestions, Thanks @AloisKraus. In 30 mins it takes 10 to 20 MB of memory. – Malik Khalil Jun 12 '23 at 05:40

0 Answers0