How to make .Net4.X winForm app win?

I was working on one windows forms app’s performance issues like decreasing app performance,“OutOfMemoryException” errors and application crashes. Here is what I found and how performance can be improved.

  1. Most of the times .csproj file says its for “anyCPU”. But actually its running on 32 bit system as project references 32 bit assemblies. If only single reference is 32 bit, then also app will run on 32 bit system. 64 bit apps are compatibility with 64-bit libraries, Enhanced Security, Future-proofing and work with significantly larger datasets
  2. Use NuGet packages, avoid using old assemblies. NuGet always tells you which libraries need to be updated. And for performance and security perspective you need to keep them updated.
  3. In legacy code its common to have ample use of static. Avoid that wherever its possible to optimize memory usage for your windows app. Use Singleton pattern(as static will take memory when app is initialized but singleton instances will be created when it is called first time)
  4. Check Class coupling with profiler. I worked on one project which has class coupling is up to 600 and also check cyclomatic complexity.
  5. In legacy solutions DataTable/Dataset is shared between methods/classes. We can use IList<T> instead of DataTable/DataSet for this purpose.
  6. Instead of running few queries in one go, multiple calls to db is made. Even when DataSet is used.
  7. Extensive use of xml/XmlDocuments, rather than using json
  8. When there are tabs with so much data, we can load Tab on demand and show loading picture.
  9. Save used to save all the loaded data. Don’t save data that is not even changed.
  10. WCF/ASMX services to API. XmlSerializer also makes the app slow.
  11. Restricting count of popups
  12. App which I analyzed is using hashtable everywhere. Its better to use dictionary. hashtable to dictionary transition
  13. Dont show error message that is hidden among the popups. show error message at top
  14. Not responding service call should be abandoned/retried and should be async. It should not block main thread.
  15. Popups or child forms should be closed and in this article can help
  16. Close button should be visible on forms and popups(child forms). here this solution can work.
  17. Long running task can be Async and can be done by background worker
  18. Restrict user from opening multiple forms without closing the existing.
  19. There are some Cyclic references. When we are running the application, cyclic references are not a problem. But it restrict the garbage collection. Avoid these cyclic references.
  20. Weak references is also a problem when we have opened multiple forms(popups). Those objects are not garbage collected.

https://medium.com/@codinglikev/understanding-and-preventing-memory-leaks-in-c-c9c46c35f09c

Leave a Reply