{"id":2016,"date":"2024-02-19T02:38:23","date_gmt":"2024-02-19T02:38:23","guid":{"rendered":"http:\/\/write.muthu.co\/?p=2016"},"modified":"2024-02-19T02:38:23","modified_gmt":"2024-02-19T02:38:23","slug":"ram-and-cpu-usage-optimization-in-c-code","status":"publish","type":"post","link":"http:\/\/write.muthu.co\/ram-and-cpu-usage-optimization-in-c-code\/","title":{"rendered":"Ram and CPU usage optimization in C# code"},"content":{"rendered":"\n
In response to the growing usage of our windows application and the need to enhance its performance, our development team embarked on a project to refactor our legacy C# codebase. A key focus of this effort was to streamline the application’s memory usage to ensure optimal performance and scalability. Recognizing that inefficient memory management can significantly impact the application’s responsiveness and overall user experience, we undertook thorough research and collaborative brainstorming sessions to pinpoint the most effective strategies for optimizing RAM consumption.<\/p>\n\n\n\n
Through our analysis, we uncovered several areas within the codebase where memory inefficiencies were prevalent. These inefficiencies ranged from redundant data structures to suboptimal algorithms and memory leaks. Addressing these issues became paramount in our quest to enhance the application’s efficiency and responsiveness.<\/p>\n\n\n\n
After careful consideration and experimentation, we distilled our findings into a set of best practices and optimization techniques tailored to our specific use case and technology stack. These techniques are designed not only to reduce RAM consumption but also to improve the application’s performance and stability.<\/p>\n\n\n\n
In the following sections, I will outline the key strategies that emerged from our research. These strategies represent our roadmap for optimizing RAM and CPU usage in our C# application, encompassing both low-level memory optimizations and high-level architectural improvements. By implementing these strategies conscientiously, we aim to achieve a leaner, more efficient application that can meet the evolving needs of our users while maintaining high standards of performance and reliability.<\/p>\n\n\n\n
Theoretically, a Hashset is better than a List, we decided to prove it through some benchmarking. We simulated and ran a series of operations on both a The result is as shown below: you can clearly see that the List performs poorly as the data size increases, while the Hashset takes nearly the same time for all operations <\/p>\n\n\n\nHashSet<\/code> and a
List<\/code>. These operations included adding elements, checking for element existence, and removing elements. What we found was both intriguing and insightful. As the dataset size grew, the performance of the
List<\/code> started to lag behind significantly. Each operation took longer to complete, and the time it took increased exponentially with the size of the dataset. On the other hand, the
HashSet<\/code> remained steadfast and consistent, delivering comparable performance across all operations, regardless of dataset size.<\/p>\n\n\n\n