Filesystemwatcher causes high CPU usage of Mono ASP. NET Applications

Source: Internet
Author: User
Tags fam inotify high cpu usage

Everyone knows ASP. NET Website application (website) can automatically detect your asp. NET application file modification, which is used to monitor changes to files/directories on the disk, the filesystemwatcher class allows the application to take any step in the response to the event that it deems necessary for file creation/Deletion/modification.

Mono's filesystemwatcher has made every effort to adapt to various environments (Linux/Windows/* BSD) and execute the assigned tasks in various operating system environments, the following backend systems are supported in UNIX:

  • Fam
  • Kevent (BSD */MacOSX only)
  • Gamin
  • Inotify (Linux only)
  • Managed watcher

Assume that you are running Linux (Kernel 2.6.13 or later ),InotifyIt is a preferred backend mechanism because it needs to be part of a user-state application. Instead of using polling, it uses the Linux kernel's notification mechanism (in our example, mono Runtime Library ). However, it requires the Linux kernel to support the mechanism.

If your kernel does not support inotify, Mono will try to use user-State applications such as FAM and gamin to monitor changes to the file/directory of the file system and then notify mono when it is running, in this way, the efficiency is greatly reduced, and the performance is very bad. If mono cannot detect inotify, fam, and gamin, Mono uses the last option to manage watcher, which is implemented in managed code, A separate thread is used for monitoring and polling changes to the file system in the selected file/directory. Because the application may (and sometimes not in ASP. NET) recursively view directories, it may be very expensive and need to check and change to a large group of files. For each running change detection, you need to check whether the file/directory exists (In case that the hosted observer program is called by two Stat (2 ), then, check the changed file metadata (size, modification time, etc.) to generate an event. About everyOnce in MSAnd bring a lot of load to the server's CPU, leading to soaring CPU usage.

The solution is also very simple. If you can, disable file system monitoring (which means your asp.. NET application will not automatically restart to modify the web. config, the file will not be re-compiled, If you modify the code to hide. CS or. aspx ,. files ). Mono supportMono_managed_watcherSet environment variables to valuesDisableTo reduce the number of file system polling transactions in your applications described above. Since it is a production environment, there will be no frequent updates to close this feature and it will not have a big impact, it can also save background thread resources.

When running an ASP. NET Website on Linux, sometimes the CPU usage is high. After a while, it is normal again. In VPs, the probability of such a situation is greater. solution:
1. If you don't care about it, it will naturally fall down, but it takes a little longer to wait, and it may appear again after a while. The reason is the above description, if this problem occurs on the VPs or cloud host, you must analyze the cause well. Is it caused by this feature? If so, turn it off;
2. Run ASP using jexus.. Net website, in JWS. start/JWS. insert export mono_managed_watcher = Disable to disable ASP. net Automatic Detection, of course, the side effect is: After you modify the source code, you have to manually restart this website.

3. Run the Apache mod_mono command.MonoSetEnv [server_alias] MONO_MANAGED_WATCHER=disable

Use the following code to check which filesystemwatcher is used on your Linux Server

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. IO;
Using system. reflection;

Namespace filewatchdetect
{
Class Program
{
Static void main (string [] ARGs)
{
Object watcher = new filesystemwatcher (). GetType ()
. Getfield ("Watcher", bindingflags. nonpublic | bindingflags. Static). getvalue (null );

Console. writeline (watcher! = NULL? Watcher. GetType (). fullname: "unknown ");
Console. Read ();
}
}
}

The following results are run on an opensuse 12.0.4 on Windows azure:

Inotify: an efficient and real-time Linux File System event monitoring framework

Use fam to monitor Linux File System Changes

Tip: Mono ASP. NET application burning CPU in idle state-filesystemwatcher

Related Article

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.