For a web program, it is hosted in the w3wp process provided by IIS. The memory size occupied by this process has a direct relationship with the usage of your application. Your program writes the standard, the memory usage is relatively low. the pseudo-pattern written by your program will cause high memory usage if the released item is not released by the system (some objects cannot be recycled by GC, for a 32-bit system, the maximum is 1.6 GB. After the value is exceeded, the process will automatically fail!
For local services, we generally use windowservice and windowform to carry them. It will have a process on its own. Recently, the problem that my windowservice occupies too much memory really occurred. In less than five minutes, the process has reached more than 500 mb, and is still processing the increasing trend. when we review the code, we found a big problem. Check whether the code below has also been found, bad taste in code
Public class user_sendmessagejob: jobbase, ijob {private static object lockobj = new object (); Private object ibigrepository = new object (); Public void execute (ijobexecutioncontext context) {lock (lockobj) {# tasks to be processed by region // logger. info (context. jobdetail. key. name + datetime. now); # endregion }}}
The above Code declares two global variables lockobj and ibigrepository, in which the ibigrepository is called in the method execute and is also called in turn. In order to avoid concurrency conflicts, the lock is used to design the exclusive lock, this global object should be released after the program runs successfully. However,Let's think, if thread 1 is executing the code in the lock, and thread 2, due to the Round Training Service, also begins to enter the method, then the ibigrepository object is not released, thread 2 produces a new object. At this time, our ibigrepository object is getting more and more, resulting in larger and larger memory consumption!
The correct method should be to declare the ibigrepository object in the execute method as a local variable. When the lock ends, it will be automatically added by the system. After the next thread 2 comes in, the new ibigrepository object will be created. In this way, we will save it. In the Round Training Service, only one ibigrepository object will always be created. This design is correct.
Let's take a look at the modified Code.
Public class user_sendmessagejob: jobbase, ijob {private static object lockobj = new object (); Public void execute (ijobexecutioncontext context) {lock (lockobj) {object ibigrepository = new object (); # tasks to be processed by region // logger. info (context. jobdetail. key. name + datetime. now); # endregion }}}
After modifying the program, let's take a look at the memory, which is only 200 MB, and there is no increasing trend. This is the right program. Therefore, some basic knowledge is very important, we should not ignore it, as Lao Zhao said: learning the operating system well can write a good windows program, learning the IIS operating mechanism, and writing a good web program!