小C#程序占用太多内存 [英] Small C# program takes up too much memory

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

问题描述

我有一个小的C#程序(约400行代码),编译后只有28kb




然而当它运行时(需要一个整整5秒)它占用了近20MB

的内存而且我不明白为什么。


我使用的一些组件:


使用System;

使用System.Collections.Generic;

使用System.Windows.Forms;

使用System.Xml.Serialization;

使用System.IO;

使用System.Diagnostics;

使用System.Management;


程序读取(反序列化)XML文件,然后生成列表。然后

程序通过事件日志读取并查看是否有任何匹配

列表。


无论如何,我可以减少内存使用情况?

I have a small C# program (about 400 lines of code) that is only 28kb
after compiled.

However when it runs (takes a whole 5 seconds) it takes up nearly 20MB
of memory and I don''t see why.

Some of the assembly I used:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;
using System.Management;

The program read (de-serialize) a XML file, then generate a list. Then
the program read through the event log and see if anything match the
list.

Is there anyway I can reduce the memory usage?

推荐答案

el * **** @gmail.com 写道:

我有一个小的C#程序(约400行代码),只有28kb

编译后。


然而当它运行时(需要整整5秒)它需要近20MB

的内存而且我不喜欢不知道为什么。


我使用的一些组件:


使用System;

使用System。 Collections.Generic;

使用System.Windows.Forms;

使用System.Xml.Serialization;

使用System.IO;

使用System.Diagnostics;

使用System.Management;


程序读取(反序列化)XML文件,然后生成名单。然后

程序通过事件日志读取并查看是否有任何匹配

列表。


无论如何,我可以减少内存使用情况?
I have a small C# program (about 400 lines of code) that is only 28kb
after compiled.

However when it runs (takes a whole 5 seconds) it takes up nearly 20MB
of memory and I don''t see why.

Some of the assembly I used:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;
using System.Management;

The program read (de-serialize) a XML file, then generate a list. Then
the program read through the event log and see if anything match the
list.

Is there anyway I can reduce the memory usage?



如果没有其他信息,这几乎是不可能的。但是

让我们从摆脱不相关的信息开始吧。这些代码行数和编译程序集的大小在这里毫无意义。

我可以有一个只有几行代码的程序,但是那个代码

将一个100 MB的文件加载到内存中。与可执行文件的大小相比,

程序的运行时内存要求会非常大,但这是一个毫无意义的比较。 XML文件中有多少数据?什么

在生成的列表中?它是什么类型的清单?你如何处理

事件日志?

-

汤姆波特菲尔德

That is nearly impossible to answer without additional information. But
let''s start by getting rid of the irrelevant information. The number of
lines of code, and the size of the compiled assembly, are meaningless here.
I could have a program that was but a few lines of code, but that code
loaded a 100 MB file into memory. The runtime memory requirements for the
program would then be quite large compared to the size of the executable,
but it''s a pointless comparison. How much data is in your XML file? What
is in the generated list? What type of list is it? How are you processing
the event log?
--
Tom Porterfield




您可以使用任务管理器内存配置文件.Net应用程序,因为.Net应用程序不一定使用它们似乎正在使用的内存。例如,如果系统内存不足,垃圾收集器将比具有大量备用内存的系统更快地释放丢弃的对象。有一些技巧只能强制运行应用程序显示在任务管理器中的必要内存量,但它会导致程序运行速度变慢并且没有用处。


请记住,您的应用程序可能很小,但它使用了很多其他需要加载到内存中的类。例如,你的应用程序继承的Form类。


不要担心内存,不要忘记已实现这些方法的Dispose()和Close()对象。通常是流和资源。

2007年2月20日星期二17:39:58 +0100,< el ***** @ gmail.comwrote:
Hi,

You can memory profile .Net applications using Task Manager because .Net applications do not necessarily use the memory they appear to be using. For instance, if the system is running low on memory, the Garbage Collector will free discarded objects much faster than a on a system with plenty of spare memory. There are tricks to force only the necessary amount of memory to run the application show up in task manager, but it will lead a slower program and serves no purpose.

Remember that your application may be small, but it uses quite a few other classes that needs to be loaded into memory as well. For instance the Form class your application inherits from.

Stop worrying about the memory, and don''t forget to Dispose() and Close() objects that have implemented those methods. Typically streams and resources.
On Tue, 20 Feb 2007 17:39:58 +0100, <el*****@gmail.comwrote:

我有一个小的C#程序(约400行代码),编译后只有28kb




然而当它运行时(需要整整5秒)它占用了近20MB的内存,我不知道为什么。


我使用的一些组件:


使用System;

使用System.Collections.Generic;

使用System.Windows.Forms;

使用System.Xml.Serialization;

使用System.IO;

使用System.Diagnostics;

使用System.Management;


程序读取(反序列化)XML文件,然后生成列表。然后

程序通过事件日志读取并查看是否有任何匹配

列表。


无论如何,我可以减少内存使用情况?

I have a small C# program (about 400 lines of code) that is only 28kb
after compiled.

However when it runs (takes a whole 5 seconds) it takes up nearly 20MB
of memory and I don''t see why.

Some of the assembly I used:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;
using System.Management;

The program read (de-serialize) a XML file, then generate a list. Then
the program read through the event log and see if anything match the
list.

Is there anyway I can reduce the memory usage?




-

快乐编码!

Morten Wennevik [C#MVP]



--
Happy coding!
Morten Wennevik [C# MVP]



>

如果没有额外的话,几乎无法回答信息。但是

让我们从摆脱不相关的信息开始吧。这些代码行数和编译程序集的大小在这里毫无意义。

我可以有一个只有几行代码的程序,但是那个代码

将一个100 MB的文件加载到内存中。与可执行文件的大小相比,

程序的运行时内存要求会非常大,但这是一个毫无意义的比较。 XML文件中有多少数据?什么

在生成的列表中?它是什么类型的清单?你如何处理

事件日志?

-

Tom Porterfield
>
That is nearly impossible to answer without additional information. But
let''s start by getting rid of the irrelevant information. The number of
lines of code, and the size of the compiled assembly, are meaningless here.
I could have a program that was but a few lines of code, but that code
loaded a 100 MB file into memory. The runtime memory requirements for the
program would then be quite large compared to the size of the executable,
but it''s a pointless comparison. How much data is in your XML file? What
is in the generated list? What type of list is it? How are you processing
the event log?
--
Tom Porterfield



XML文件为6KB。该列表本身总共有大约20个对象,这是一个嵌套的通用列表。每个对象由大约10个字符串组成

(每个大约20个字符)和一些int。在C中,整个列表是
可能少于10KB的内存。


使用EventLog eLog = new EventLog(System)访问事件日志

我在一个

foreach循环中浏览每一个。事件日志的大小默认为512KB。


该程序使用了3个读取器/写入器流,然后关闭它们

。流的数量并不会真正影响内存大小,因为程序会消耗尽可能多的内存,包含1个流或3个
流。

The XML file is 6KB. The list itself has about 20 objects in total in
a nested generic list. Each of the object consist of about 10 string
(around 20 characters each) and a few int. In C the entire list is
probably less than 10KB of memory.

The event log is accessed with EventLog eLog = new EventLog("System")
I go through each of them in a
foreach loop. The size of the event log is at default, 512KB.

The program used 3 reader/writer streams, and I close them
afterwards. The number of streams does not really affect the memory
size, as the program consume as much memory with 1 streams or 3
streams.


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

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