Suppressing C# garbage collection

后端 未结 6 1076
挽巷
挽巷 2021-02-08 09:26

My application allocates a large amount of memory (millions of small objects totaling several gigabytes) and holds onto it for a long time.

  1. Is .NET wasting time ch
6条回答
  •  灰色年华
    2021-02-08 09:50

    My ASP.NET application - B2B system - used to start at 35-40MB when the first user came to it. After so minutes, the application used to grow up to 180 MB with 2 or 3 users hitting pages. After reading .net development best practices and GC performance guideline I find out that the problem was my application design. I did not agree at once.

    I was horrified about how easy we can do mistakes. I gave up many features and start to easy some objects up. Meaning:

    1. Avoid mixing so much pages and intelligent and communicative user controls (the ones with lot of functionalities which actually most exist for each page that uses this control).

    2. Stop engendering universal functionalities on base classes. Sometimes is preferable repeat. Inheritance is cost.

    3. On some complex functionality I put everything on the same function. YES, reaching 100 lines most. When I read this recommendation on .net performance guidance I did not believe it but it works. Call stacks is a problem, use class properties over local variables is a problem. Class level variables can be a hell…

    4. Stop using complex base classes, no base classes with more than 7 lines should exist. If you spread bigger classes on the entire framework, you'll have problem.

    5. I start use more static objects and functionalities. I saw application wich other guy designed. All dataaccess objects methods (insert, update, delete, selects) was static ones. The application with more concurrent users never reaches out more than 45MB.

    6. To save some projects, I like stead state pattern. I learned in the real world but the author Nygard also agree with me on his book: Release IT - Design and Deploy Production-Ready software. He calls such approach as steady state pattern. This patterns says we may need something to free up idle resources.

    7. You may want play with on machine config file. On the attribute memoryLimit you'll indicate the percentage of memory which could be reached before a process recycles.

    8. You may also want play with on machine config file. On this attribute, GC will dictate the machine behaviour (Workstation GC and Server GC). This option may dramatically changes memory consumption behaviour too.

    I had lot of success when I started to care about this items. Hope this help.

    -- EDITED IN 04-05-2014 I've changed my mind about many things due to GC new versions improvements and the advances of HTML 5 and MVC framework.

提交回复
热议问题