最近写了一个WinFrom程序。此程序侦听TCP端口,接受消息处理,然后再把处理后的消息,利用线程池通过WebService发送出去(即一进一出)。

在程序编写完成后,进行压力测试。用Fiddler提交1万请求。

                ThreadPool.QueueUserWorkItem((o) =>
{
try
{
APPService.AddLog(o as MDMPortalWCF.LogInfo);//发送此Log时,是提交WebService请求的。
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
} },log);

 使用procexp.exe查看,随着TCP请求的增多,2代GCHeap不断增大。是明显的内存泄漏线索。

使用WinDbg分析,GC2明显大,线程正在忙。发现很多String(LogInfo中的属性)被System.Threading.ThreadPoolWorkQueue.QueueSegment引用。正是内存高的原因。

使用Fiddler查看,发现出乎意料的行为,1万个TCP接受完成后,Log却没有发送完。而是持续之后很久,一直发送,直到几分钟后才发送完成。

    此行为表明,ThreadPool倾向于少数线程排队任务,不倾向于开很多线程迅速完成任务。

当发送日志的任务在ThreadPool的队列排队时,LogInfo不会被GC回收,而是被QueueSegment持有。待ThreadPool执行完成后,QueueSegment不再引用任务对象,故内存被回收。

后来想想,ThreadPool.QueueUserWorkItem(WaitCallback callBack, object state)中的state对象,在被执行前,将一直被引用,这是合情合理的。但由于ThreadPool的排队性质,导致内存释放也是缓慢的,往往是人们想不到的。在此记录,以示后人。

2018-03-23  

在网上读到一篇文章(http://www.albahari.com/threading/#_Optimizing_the_Thread_Pool)说的非常详细。更好的解释了如上的现象。SetMinThreads是一种高级优化线程池的技术。线程池策略是经济型的,即尽量节省线程新建,这是为了防止多个短生命期任务造成内存突然膨胀。但当入队的任务,超过半秒执行时间,线程池才开始每半秒新增一个Thread。当然,这也造成了一些问题,比如多个访问互联网的线程,我们希望同时一起访问。这时需要设置ThreadPool.SetMinThreads(100,100),这条语句,告诉线程池管理器,在前100个线程内,不要等待半秒,任务来了,立即创建线程。原文如下:

How Does the Minimum Thread Count Work?

Increasing the thread pool’s minimum thread count to x doesn’t actually force x threads to be created right away — threads are created only on demand. Rather, it instructs the pool manager to create up to x threads the instant they are required. The question, then, is why would the thread pool otherwise delay in creating a thread when it’s needed?

The answer is to prevent a brief burst of short-lived activity from causing a full allocation of threads, suddenly swelling an application’s memory footprint. To illustrate, consider a quad-core computer running a client application that enqueues 40 tasks at once. If each task performs a 10 ms calculation, the whole thing will be over in 100 ms, assuming the work is divided among the four cores. Ideally, we’d want the 40 tasks to run on exactly four threads:

  • Any less and we’d not be making maximum use of all four cores.
  • Any more and we’d be wasting memory and CPU time creating unnecessary threads.

And this is exactly how the thread pool works. Matching the thread count to the core count allows a program to retain a small memory footprint without hurting performance — as long as the threads are efficiently used (which in this case they are).

But now suppose that instead of working for 10 ms, each task queries the Internet, waiting half a second for a response while the local CPU is idle. The pool manager’s thread-economy strategy breaks down; it would now do better to create more threads, so all the Internet queries could happen simultaneously.

Fortunately, the pool manager has a backup plan. If its queue remains stationary for more than half a second, it responds by creating more threads — one every half-second — up to the capacity of the thread pool.

The half-second delay is a two-edged sword. On the one hand, it means that a one-off burst of brief activity doesn’t make a program suddenly consume an extra unnecessary 40 MB (or more) of memory. On the other hand, it can needlessly delay things when a pooled thread blocks, such as when querying a database or calling WebClient.DownloadFile. For this reason, you can tell the pool manager not to delay in the allocation of the first x threads, by calling SetMinThreads, for instance:

ThreadPool.SetMinThreads (50, 50);

(The second value indicates how many threads to assign to I/O completion ports, which are used by the APM, described in Chapter 23 of C# 4.0 in a Nutshell.)

The default value is one thread per core.

.Net线程池ThreadPool导致内存高的问题分析的更多相关文章

  1. 线程池ThreadPool的初探

    一.线程池的适用范围 在日常使用多线程开发的时候,一般都构造一个Thread示例,然后调用Start使之执行.如果一个线程它大部分时间花费在等待某个事件响应的发生然后才予以响应:或者如果在一定期间内重 ...

  2. C#多线程学习 之 线程池[ThreadPool](转)

    在多线程的程序中,经常会出现两种情况: 一种情况:   应用程序中,线程把大部分的时间花费在等待状态,等待某个事件发生,然后才能给予响应                   这一般使用ThreadPo ...

  3. 多线程Thread,线程池ThreadPool

    首先我们先增加一个公用方法DoSomethingLong(string name),这个方法下面的举例中都有可能用到 #region Private Method /// <summary> ...

  4. C# 线程池ThreadPool的用法简析

    https://blog.csdn.net/smooth_tailor/article/details/52460566 什么是线程池?为什么要用线程池?怎么用线程池? 1. 什么是线程池? .NET ...

  5. C#多线程学习 之 线程池[ThreadPool]

    在多线程的程序中,经常会出现两种情况: 一种情况:   应用程序中,线程把大部分的时间花费在等待状态,等待某个事件发生,然后才能给予响应                   这一般使用ThreadPo ...

  6. 线程池ThreadPool实战

    线程池ThreadPool 线程池概念 常用线程池和方法 1.测试线程类 2.newFixedThreadPool固定线程池 3.newSingleThreadExecutor单线程池 4.newCa ...

  7. [转]C#多线程学习 之 线程池[ThreadPool]

    在多线程的程序中,经常会出现两种情况: 一种情况:   应用程序中,线程把大部分的时间花费在等待状态,等待某个事件发生,然后才能给予响应                   这一般使用ThreadPo ...

  8. 【C# 线程】线程池 ThreadPool

    Overview    如今的应用程序越来越复杂,我们常常需要使用<异步编程:线程概述及使用>中提到的多线程技术来提高应用程序的响应速度.这时我们频繁的创建和销毁线程来让应用程序快速响应操 ...

  9. 高效线程池(threadpool)的实现

    高效线程池(threadpool)的实现 Nodejs编程是全异步的,这就意味着我们不必每次都阻塞等待该次操作的结果,而事件完成(就绪)时会主动回调通知我们.在网络编程中,一般都是基于Reactor线 ...

随机推荐

  1. oracle连接封装方法

    public static void updateSqlOracle(String sqlstr,String linkIP,String username,String password) thro ...

  2. 路飞学城详细步骤 part2

    一 显示课程列表 需求:当你点击课程,course.vue在 <router-view>渲染,并不需要你进行其他点击,所欲的课程列表直接在前端显示,数据是从数据库拿到的. 补充1:生命周期 ...

  3. Linux(13):期中架构(5)--- 前端部分:keepalived高可用 & HTTPS & iptables防火墙

    keepalived 高可用集群 1. keepalived服务概念说明 # 1.1 keepalived软件的作用? Keepalived软件起初是专为LVS负载均衡软件设计的, 用来管理并监控LV ...

  4. 【2017YYHS WC】

    因为本葳蕤分数太低去不了WC,只能同去WC的各位大爷一起训练一波,就称作是YYHS WC吧,其实就是WC难度的多校 day1:早上8:30考的试,下午1:00去吃中饭 T1:考场打得暴力结果矩阵乘法后 ...

  5. 关于事件委托和时间冒泡(以及apply和call的事项)

    搜索事件委托和事件冒泡,网上一大堆乱七八糟的解释,当然意思都对,没毛病. but,真的无聊. 事件冒泡:事件会从点击的元素开始依次向上流出,直到html,遇见事件监听则执行. 事件委托:原因——父元素 ...

  6. 再看c语言之getchar/putchar

  7. R语言入门视频笔记--3-1--矩阵与数组

    生成一个新矩阵,多用一些参数吧这次: x <- c(12,13,14,15) rname <- c("R1","R2") nname <- c ...

  8. Laravel5.1的控制器分组

    Laravel5.1的控制器分组 默认的控制器时直接在app\Http\Controllers下,创建分组名字,路由配置使用:分组名\控制器名@方法名.

  9. [Inside HotSpot] Serial垃圾回收器 (二) Minor GC

    Serial垃圾回收器Minor GC 1. DefNewGeneration垃圾回收 新生代使用复制算法做垃圾回收,比老年代的标记-压缩简单很多,所有回收代码都位于DefNewGeneration: ...

  10. BUPT复试专题—统计时间间隔(2013计院)

    题目描述 给出两个时间(24小时制),求第一个时间至少要经过多久才能到达第二个时间.给出的时间一定满足的形式,其中x和y分别代表小时和分钟.0≤x<24,0≤y<60. 输入格式 第一行为 ...