2011-04-25 78 views
0

我正在开发一个C#中的程序,它使用vfpoledb驱动程序将大约100,000行插入到版本4 dbase文件(* .dbf)中。VFPOledb驱动程序可能的内存泄漏?

返回连接字符串是如下的方法,

 
internal string GetDBaseConnectionString(string path) 
{    
    return "Provider=vfpoledb;Data Source=" + path + ";Collating Sequence=general;";    
} 

的代码段,做所述插入件是如下,

 internal long Execute() 
     { 
      OleDbConnection con = null; 
      OleDbCommand cmd = null; 

      try 
      { 
       con = new OleDbConnection(AppSettings.Current.GetDBaseConnectionString(_dbfPath)); 
       con.Open(); 

       cmd = new OleDbCommand(_sql); 
       cmd.Connection = con;     
       cmd.CommandTimeout = AppSettings.Current.DefaultCommandTimeOutMinutes; 

       long rowIndex = 0; 
       int countInBatch = 0; 

       for (int i = 0; i < _reader.FieldCount; i++) 
        cmd.Parameters.Add(new OleDbParameter());     

       while (_reader.Read()) 
       { 
        for (int i = 0; i < cmd.Parameters.Count; i++) 
         cmd.Parameters[i].Value = _reader.GetValue(i); 

        cmd.ExecuteNonQuery(); 

        rowIndex += 1; 

        if (_progressChangeRowCount > 0) 
        { 
         countInBatch += 1; 

         if (countInBatch >= _progressChangeRowCount) 
         { 
          countInBatch = 0; 

          ProgressChangedEventArgs args = new ProgressChangedEventArgs(rowIndex); 
          this.OnProgressChanged(args); 
         } 
        } 
       } 

       _reader.Close(); 

       con.Close(); 
       con.Dispose(); 
       cmd.Dispose(); 

       return rowIndex; 
      } 
      catch (Exception ex) 
      {     
       if (con != null) 
       { 
        con.Close(); 
        con.Close(); 
       } 

       if (cmd != null) 
        cmd.Dispose(); 

       if(_reader!= null) 
        _reader.Close(); 

       throw ex; 
      } 
     } 

该段在三个线程同时运行。因此,数据将从3个SqlDataReaders中同时插入到三个dbase文件中。

我的问题是,我的程序吃了大约每分钟50-100 MB,它只会增加,直到我关闭该程序。由于这个System.OutOfMemoryExceptions在程序中引发,操作系统很快就会关闭它。我可以看到任务管理器中的页面文件使用从540 MB变为2.2 GB。

我已经缩小到行cmd.ExecuteNonQuery();如果我注释掉这一行,程序只执行大约1或2 MB的内存增量。

因此

  1. 这可能是由于司机VFPOledb内存泄漏?我正在使用最新版本9
  2. 如果是这样,我该怎么做才能应付它? (把它作为一个单独的进程来包装,这样操作系统就会清理掉任何出现内存泄漏的声音,但这应该是最后的手段)
  3. 您是否看到或知道可能导致此错误的其他错误?
  4. 更改驱动程序可能会有所帮助,但Jet驱动程序缓慢而缓慢。 dbase批量插入是否有其他选项?我也在这里问过这个问题Efficient way to bulk insert into Dbase (.dbf) files

在此先感谢。

+0

你可以也应该开始用'using(){}'块来简化它。 – 2011-04-25 10:35:49

+0

广告'抛出;'应该'抛出;' – 2011-04-25 10:40:12

+0

请添加_sql文本和如何/你在哪里创建_reader。 – 2011-04-25 10:41:09

回答

1

经过很多次尝试后,我将这段代码封装在单独的进程中,以便操作系统在退出后清理。这是我能找到的最佳解决方案。