小应用程序的高内存使用率

Rob*_*itt 3 c# memory-management

我只是构建一个非常简单的基于事件的代理监视器顶部禁用代理设置取决于网络位置是否可用.

问题是该应用程序是一个小的10KB,并具有最小的接口,但它使用10MB的内存.

代码非常简单:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.NetworkInformation;
using Microsoft.Win32;

namespace WCSProxyMonitor
{
    class _Application : ApplicationContext
    {
        private NotifyIcon NotificationIcon = new NotifyIcon();
        private string IPAdressToCheck = "10.222.62.5";

        public _Application(string[] args)
        {
            if (args.Length > 0) 
            {
                try
                {
                    IPAddress.Parse(args[0]); //?FormatException
                    this.IPAdressToCheck = args[0];
                }
                catch (Exception) 
                {}
            }

            this.enableGUIAspects();
            this.buildNotificationContextmenu();
            this.startListening();
        }

        private void startListening() 
        {
            NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(networkChangeListener);
        }

        public void networkChangeListener(object sender, EventArgs e)
        {
            //foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
            //{
                //IPInterfaceProperties IPInterfaceProperties = nic.GetIPProperties();
            //}

            //Attempt to ping the domain!
            PingOptions PingOptions = new PingOptions(128, true);
            Ping ping = new Ping();

            //empty buffer
            byte[] Packet = new byte[32];

            //Send
            PingReply PingReply = ping.Send(IPAddress.Parse(this.IPAdressToCheck), 1000, Packet, PingOptions);

            //Get the registry object ready.
            using (RegistryKey RegistryObject = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true)) 
            {
                if (PingReply.Status == IPStatus.Success)
                {
                    this.NotificationIcon.ShowBalloonTip(3000, "Proxy Status", "proxy settings have been enabled", ToolTipIcon.Info);
                    RegistryObject.SetValue("ProxyEnable", 1, RegistryValueKind.DWord);
                }
                else
                {
                    this.NotificationIcon.ShowBalloonTip(3000, "Proxy Status", "proxy settings have been disabled", ToolTipIcon.Info);
                    RegistryObject.SetValue("ProxyEnable", 0, RegistryValueKind.DWord);
                }
            }
        }

        private void enableGUIAspects()
        {
            this.NotificationIcon.Icon = Resources.proxyicon;
            this.NotificationIcon.Visible = true;
        }

        private void buildNotificationContextmenu()
        {
            this.NotificationIcon.ContextMenu = new ContextMenu();
            this.NotificationIcon.Text = "Monitoring for " + this.IPAdressToCheck;

            //Exit comes first:
           this.NotificationIcon.ContextMenu.MenuItems.Add(new MenuItem("Exit",this.ExitApplication));
        }

        public void ExitApplication(object Sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:

  • 对于基于C#构建的应用程序,这是正常的吗
  • 我该怎么做才能减少使用的内存量.

该应用程序构建于.NET 4.0的框架之上

问候.

Eri*_*ert 8

它不使用任何接近10 MB的RAM.它使用10 MB的地址空间.地址空间使用(几乎)与RAM无关.

加载.NET框架时,所有代码的空间都保留地址空间中.它没有加载到RAM中.代码根据需要以4kb块的形式加载到RAM中,但必须在地址空间中保留这些页面的空间,以确保进程在地址空间中有一个空间供所有人使用.它可能需要的代码.

此外,当每个页面加载到RAM中,如果你有在同一时间运行两个.NET应用程序,然后他们共享的RAM页.内存管理器负责确保共享代码页只加载一次到RAM中,即使它们位于千个不同的地址空间中.

如果您要测量内存使用量,那么您需要了解内存在现代操作系统中的工作原理.事情发生了变化,自286天以来.

看到这个相关的问题:

2 GB真的是我最大的吗?

关于这个主题的文章简要介绍了内存的实际运作方式.

http://blogs.msdn.com/b/ericlippert/archive/2009/06/08/out-of-memory-does-not-refer-to-physical-memory.aspx