关于windows下mysql进程中线程只增不减的问题

MySql 码拜 9年前 (2015-11-12) 983次浏览
现象:通过任务管理器查看mysqld进程中线程数起始为24左右,运行下述测试程序,线程增加100左右(测试电脑4核,运行过程cpu利用率100%),程序执行结束,线程依然保持124左右,关闭程序 线程降低至40左右。另外程序运行过程内存也会随着程序跑起来而增加,关闭而减少,但是减少到的值大于mysqld进程起来时的值
问题:程序执行结束,线程依然保持124左右,怀疑是mysql线程缓存引起(另外测试过,下述程序进程在不关闭的情况下 执行完100个线程后 再重复执行这100个线程,任务管理器中内存cpu均不变),但是为什么测试程序关闭后线程会马上降下来,程序中已经有显示的关闭连接,还是说本人关闭的方式有问题?哪位同僚能帮忙解答一下。
程序代码如下

            for (int i = 0; i < 100; i++)
            {
                Thread t = new Thread(()=>
                {
                    int baseInt = i;
                    for (int index = 0; index < 100; index++)
                    {
                        MySql.Data.MySqlClient.MySqlConnection conn = null;
                        MySql.Data.MySqlClient.MySqlCommand cmd = null;
                        MySql.Data.MySqlClient.MySqlDataReader reader = null;
                        try
                        {
                            conn = new MySql.Data.MySqlClient.MySqlConnection("Server=127.0.0.1;Port=6666;Database=unit;Uid=root;Password=;Pooling=true;Min Pool Size=0;Max Pool Size=1000");
                            cmd = conn.CreateCommand();
                            conn.Open();
                            cmd.CommandText = "insert into tbtable(param1) value(""system"");";
                            reader = cmd.ExecuteReader();
                            while (reader.Read())
                            {
                                // process the results
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.ToString());
                        }
                        finally
                        {
                            if (reader != null)
                            {
                                reader.Close();
                                reader.Dispose();
                                reader = null;
                            }
                            if (cmd != null)
                            {
                                cmd.Dispose();
                                cmd = null;
                            }
                            if (conn != null)
                            {
                                conn.Close();
                                conn.Dispose();
                                conn = null;
                            }
                        }
                    }
                });
                t.Start();
            }

题外话:本帖分数不多见谅

解决方案:20分
假如使用了数据库连接池,则会导致这种现象。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于windows下mysql进程中线程只增不减的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)