本文介绍了当我们在控制台应用程序中使用STAThread时,会累积大量内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#控制台应用程序.这与广告有关.目的该应用程序的功能是在容器内显示子对象.该容器大约有50000个对象.

我使用DirectoryEntry类获取容器的详细信息.这dirEntry.Children给出了内部子项的详细信息容器.

程序片段如下.

添加了System.DirectoryServices的AD_Display.cs作为参考

命名空间AD_Display
{
班级课程
{
[STAThread]
静态void Main(string [] args)
{
目录条目dirEntry =新DirectoryEntry("LDAP://localhost/CN = xyz,DC =<域>,DC = com");
foreach(dirEntry.Children中的DirectoryEntry childEntry)
{
Console.WriteLine(childEntry.Properties ["distinguishedName"] [0]);
}
}
}
}

如果未指定[STAThread],则不会发生内存累积.但是如果我们如果指定[STAThread],则在循环发生时会发生大量内存累积被执行.

所以问题是
1. STAThread的意义是什么?
2.为什么在使用STAThread时会看到巨大的内存堆积?
3.我们什么时候必须使用STAThread?
4.如果删除STAThread,可能会遇到哪些性能问题?

期待答复

I have a C# console application. this is pertaining to AD. the purposeof the application is to display the child objects inside a container.the container has around 50000 objects.

i get the details of the container using the DirectoryEntry class. thedirEntry.Children gives the details of the children inside thecontainer.

the program snippet is as follows.

AD_Display.cs with System.DirectoryServices added as a reference

namespace AD_Display
{
    Class program
    {
        [STAThread]
        Static void Main(string []args)
        {
            DirectoryEntrydirEntry = new      DirectoryEntry("LDAP://localhost/CN=xyz,DC=<domain>,DC=com");
            foreach (DirectoryEntry childEntry in dirEntry.Children)
            {
              Console.WriteLine(childEntry.Properties["distinguishedName"][0]);
            }
        }
    }
}

If we do not specify [STAThread], no memory build-up happens. But if wespecify [STAThread], a huge memory build-up happens as the loop isgetting executed.

so the question is
1. what is the relevance of STAThread?
2. why is it that we see a huge memory build-up when we use STAThread?
3. when do we have to use STAThread?
4. what are the performance issues that we might see if we remove STAThread?

Looking forward to the reply

推荐答案

1. STAThread的意义是什么?
注释:线程正在单个线程中执行.

1. what is the relevance of STAThread?
Comment: Thread is executing in a single threaded aparment.

2.为什么在使用STAThread时会看到大量的内存堆积?
注释:您的所有执行路径都在单线程下,并且仅当线程停止/暂停时才进行内存声明执行.

2. why is it that we see a huge memory build-up when we use STAThread?
Comment: All of your execution path is under single thread and memory reclarim only occurs when your thread stops/pause from execution.

3.
评论:这是您的设计理念.经验法则是在执行长时间运行的任务时将线程分开,该任务占用大量资源,例如内存,TCP/IP或数据库连接,屏幕更新等.也就是说,启动一个新线程(通常是MTA线程)来执行除单线程之外的此类操作.

3. when do we have to use STAThread?
Comment: It's your design philoshophy. Rule of thumbs is to seperate thread while you're executing a long running task which takes up lot of resources such as memory, TCP/IP or database connection, screen updates etc. That is, start a new thread (usually a MTA Thread) to execute such operation apart from the single thread.

4.如果删除STAThread,可能会看到哪些性能问题?
注释:您的代码将在MTA线程下运行,因此将是多线程的. MTA Thread可以更好地利用资源,并且可以在执行时逐渐回收内存.

4. what are the performance issues that we might see if we remove STAThread?
Comment: Your code will run under MTA Thread, so will be multithreaded. MTA Thread may better utilizes resources and can reclaim memory gradually as it executes.

最后,我认为您应该在一个新的MTA线程中运行长时间运行的代码.以下URL还可以帮助您更清楚地了解Windows中的线程模型:

希望这会有所帮助.

欢呼


这篇关于当我们在控制台应用程序中使用STAThread时,会累积大量内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 18:11