【发布时间】:2011-07-19 15:03:06
【问题描述】:
我只是构建一个非常简单的基于事件的代理监视器顶部禁用代理设置,具体取决于网络位置是否可用。
问题是应用程序很小,只有 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();
}
}
}
我的问题是:
- 这对于基于 C# 构建的应用程序是否正常
- 我可以做些什么来减少正在使用的内存量。
应用程序建立在 .NET 4.0 的框架上
问候。
【问题讨论】:
-
当编译成 .NET IL 时,你会得到更小的二进制文件,所以 10kB 的应用程序大小是很正常的。但是,不确定 10MB RAM 的使用情况会怎样。
-
您的代码库可能是 10k,但您忘记了您正在使用的框架,哪个不是,并且也已加载。
-
应用程序以 10K 编译,我已经将它从发布文件夹迁移到桌面,我可以理解大约 2~3 MB 内存,但是对于这样一个小应用程序来说,10MB 是很多资源的方式: (
-
它还预先获取了一些 RAM 以供您的应用程序增长,因此保留了 10MB 但不一定使用。
-
不幸的是,这是唯一让我感到舒服的语言。
标签: c# memory-management