Service-Oriented Architecture ~ High memory usage of the Local Training Service

Source: Internet
Author: User

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!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.