小型应用程序的高内存使用率 [英] High memory usage for small application

查看:90
本文介绍了小型应用程序的高内存使用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是构建了一个非常简单的基于事件的代理监视器,因此请根据网络位置是否可用来禁用代理设置.

im just building a very simple event based proxy monitor top disable the proxy settings depending on if a network location is available.

问题在于该应用程序只有10KB的微小空间,并且具有最小的界面,但它使用的是10MB的内存.

the issue is that the application is a tiny 10KB and has minimal interface, but yet it uses 10MB of ram.

代码非常简单:

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();
        }
    }
}

我的问题是:

  • 这是基于C#构建的应用程序的正常情况吗
  • 我该怎么做以减少所使用的内存量.

该应用程序基于.NET 4.0框架

the application is built on the framework of .NET 4.0

致谢.

推荐答案

它不使用任何接近10 MB的 RAM .它使用10 MB的地址空间.地址空间的使用与RAM几乎没有任何关系.

It doesn't use anywhere near 10 MB of RAM. It uses 10 MB of address space. Address space usage has (almost) nothing whatsoever to do with RAM.

加载.NET框架时,在地址空间保留所有代码的空间.它没有加载到RAM中.该代码会根据需要以称为页面"的4kb块的形式加载到RAM中,但是必须在地址空间保留保留这些页面的空间.确保过程在地址空间中有一个空格,用于它可能需要的所有代码.

When you load the .NET framework, space for all the code is reserved in your address space. It is not loaded into RAM. The code is loaded into RAM in 4kb chunks called "pages" on an as-needed basis, but space for those pages has to be reserved in the address space so that the process is guaranteed that there is a space in the address space for all the code it might need.

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

Furthermore, when each page is loaded into RAM, if you have two .NET applications running at the same time then they share that page of RAM. The memory manager takes care of ensuring that shared code pages are only loaded once into RAM, even if they are in a thousand different address spaces.

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

If you're going to be measuring memory usage then you need to learn how memory works in a modern operating system. Things have changed since the 286 days.

请参阅以下相关问题:

2 GB真的是我的最大值吗?

我的主题文章简要介绍了内存的实际工作原理.

And my article on the subject for a brief introduction to how memory actually works.

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

这篇关于小型应用程序的高内存使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆