工作表的内存问题

时间:2019-01-16 19:12:31

标签: epplus

当我尝试获取工作簿中所有工作表的名称时,我看到了过度使用内存的感觉。尝试打开包(excelPackage.Workbook.Worksheets)内的工作表后,我的测试将使用约400 MB的内存,并花费大约20秒钟来完成该行代码。我想知道对于30 MB .xlsx文件和3个工作表来说,该工具的负载是否正常。我更大的问题是,即使我完成了ListWorksheets任务,该工具似乎仍可以保留1.2 GB的内存。因此,a)对于一个30 MB的文件来说,1.2 GB似乎过多了; b)工具完成后,应该释放内存吗?

Memory plot

    [TestMethod]
    public void WorkBook_OpenAndCloseWorkBook_MemoryBehavesCorrectly()
    {
        var template = Path.Combine(testDocumentsRootDirectory, @"Templates\30MB_File.xlsx");
        var outputSmokeTest = Path.Combine(testDocumentsRootDirectory, @"Results\MemTest.xlsx");
        for (int x = 20; x > 0; x--)
        {

            smokeTestWorkbook = new Workbook(key);
            if (File.Exists(outputSmokeTest))
                File.Delete(outputSmokeTest);
            smokeTestWorkbook.Open(template);
            var worksheets = smokeTestWorkbook.ListWorksheets();
            smokeTestWorkbook.SaveAs(outputSmokeTest);
            smokeTestWorkbook.Close();
            //GC.Collect();
            System.Threading.Thread.Sleep(5000);
        }

    }

    /// <summary>
    /// Opens a reference to the workbook
    /// </summary>
    /// <returns></returns>
    public void Open(string workbookPath)
    {            
        var loggerString = "Open";
        if (File.Exists(workbookPath))
        {
            try
            {
                logger.Track(loggerString);
                excelPackage = new ExcelPackage(new FileInfo(workbookPath));         
            }              
            catch (Exception ex)
            {
                logger.TrackException(loggerString, ex);
            }
        }
    }

    /// <summary>
    /// Obtain an array of worksheet names
    /// </summary>
    /// <returns></returns>
    public string[] ListWorksheets()
    {
        var returnValue = new List<string>();
        var loggerString = "ListWorksheets";
        try
        {
            logger.Track(loggerString);
            returnValue.AddRange(excelPackage.Workbook.Worksheets.Select(excelWorksheet => excelWorksheet.Name));
        }
        catch (Exception ex)
        {
            logger.TrackException(loggerString, ex);
            throw;
        }
        return returnValue.ToArray();
    }

    /// <summary>
    /// SaveAs document
    /// </summary>
    /// <param name="filename"></param>
    /// <returns></returns>
    public void SaveAs(string filename)
    {            
        var loggerString = "SaveAs";
        try
        {
            logger.Track(loggerString);                            
            excelPackage.SaveAs(new FileInfo(filename));                
        }
        catch (Exception ex)
        {
            logger.TrackException(loggerString, ex);
            throw;
        }            
    }

    /// <summary>
    /// Closes the document
    /// </summary>
    /// <returns></returns>
    public void Close()
    {
        var loggerString = "Close";
        try
        {
            logger.Track(loggerString);
            excelPackage.Dispose();
        }
        catch (Exception ex)
        {
            logger.TrackException(loggerString, ex);
            throw;
        }
    }

0 个答案:

没有答案