猿问

运行这个函数后程序内存没有释放,占了2个G,是什么原因啊

 private void CreateIndex()
        {
          

 

            writer = new IndexWriter("c:\\index", new StandardAnalyzer(), true);
            writer.SetMaxFieldLength(1000000);

 


            string strConn = "Server=.;uid=2009;pwd=d12345;database=2009new";


            System.Data.SqlClient.SqlConnection MyConn = new System.Data.SqlClient.SqlConnection(strConn);

 

            DataSet ds = new DataSet();

            SqlDataAdapter usr_table_SqlDataAdapter = new SqlDataAdapter("select   channelid,articleid,title,content,updatetime,infopurview from PE_Article where (channelid=1 or (channelid=1009 and classid=144)) and deleted=0 and status=3 order by updatetime desc", MyConn);


            usr_table_SqlDataAdapter.Fill(ds, "PE_Article");

            int i;

            for (i = 1; i < ds.Tables["PE_Article"].Rows.Count; i++)
            {


                Document doc = IndexDocument(ds.Tables["PE_Article"].Rows[i]);
                writer.AddDocument(doc);

             


              
                Application.DoEvents();

            }

            ds.Clear();
            ds.Dispose();
            usr_table_SqlDataAdapter.Dispose();
            MyConn.Close();


            button1.Text = i.ToString() + "OK";

            writer.Optimize();
            writer.Close();
    
        }

九州编程
浏览 361回答 3
3回答

慕盖茨4494581

writer这个变量你是在函数外声明的吧,从代码看最有可能是这个变量没有被释放掉,因为其他变量你虽然没用using保证他们被及时释放,但声明定义都是在函数内部,理论上说函数结束时他们就会被释放,不过你还是应该把所有可以dispose的变量都在最后dispose()或者用using括起来比较好 [汗,刚发现楼上竟然是dudu,班门弄斧了]

温温酱

你最好用 DataReader 来读。直接Fill ,如果数据量大,会全读到内存里面。没有释放,你可以调用GC.Collect () 强行回收一下看看能否释放。

郎朗坤

占用这么多内存,这个查询返回多少条记录? 另外建议下面的语句放在finally中: ds.Clear();ds.Dispose();usr_table_SqlDataAdapter.Dispose();MyConn.Close(); writer.Close();
随时随地看视频慕课网APP
我要回答