减少创建 excel 时产生的内存使用量 - .NET core

问题描述 投票:0回答:0

我有一个从通用列表创建 excel 的逻辑。创建的总记录大约为 99700,但问题是每次调用 excel 生成方法(有 10000 条记录)时,内存都会飙升(第一次迭代 - 520 MB,第二次迭代 1G 等)并且它没有被释放.下面你可以找到我用过的excel创建逻辑。

try
            {
                if (!filePath.Any())
                {
                    return new UAOperationResult<string>
                    {
                        Content = "File path is empty",
                        Success = false,
                        Exceptions = new Exception("File path is empty")
                    };
                }

                using SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(filePath, isEditable: true);
                WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
                WorksheetPart worksheetPart = workbookPart.WorksheetParts.FirstOrDefault();
                SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().FirstOrDefault();
                uint num = (uint)(sheetData.Elements<Row>().Count() + 1);
                PropertyInfo[] properties = typeof(T).GetProperties();
                foreach (T record in records)
                {
                    Row row = new Row
                    {
                        RowIndex = (UInt32Value)num
                    };
                    if (columns.Any())
                    {
                        foreach (string column in columns)
                        {
                            PropertyInfo propertyInfo = properties.FirstOrDefault((PropertyInfo x) => x.Name == column);
                            Cell newChild = new Cell
                            {
                                DataType = (EnumValue<CellValues>)CellValues.String,
                                CellValue = new CellValue(propertyInfo.GetValue(record)?.ToString()),
                                CellReference = (StringValue)(num + column)
                            };
                            row.AppendChild(newChild);
                        }
                    }
                    else
                    {
                        PropertyInfo[] array = properties;
                        foreach (PropertyInfo propertyInfo2 in array)
                        {
                            Cell newChild2 = new Cell
                            {
                                DataType = (EnumValue<CellValues>)CellValues.String,
                                CellValue = new CellValue(propertyInfo2.GetValue(record)?.ToString()),
                                CellReference = (StringValue)(num + propertyInfo2.Name)
                            };
                            row.AppendChild(newChild2);
                        }
                    }

                    sheetData.AppendChild(row);
                    num++;
                }

                workbookPart.Workbook.Save();
                GC.Collect();
                return new UAOperationResult<string>
                {
                    Content = "file successfully created as " + filePath,
                    Success = true,
                    Exceptions = null
                };
            }
            catch (Exception ex)
            {
                return new UAOperationResult<string>
                {
                    Content = ex.Message,
                    Success = false,
                    Exceptions = ex
                };
            }

这里我使用了“using”语句,所以我认为GC会被直接应用。但出于测试目的,我已经应用了 GC forceful,但内存仍然没有被释放。

这是内存使用情况的快照

执行结束时内存达到约 3.7 GB。

有什么方法可以控制这种内存冲突吗?

注意:也尝试过使用数据表的逻辑。但问题仍然存在。

asp.net-core memory-leaks garbage-collection openxml
© www.soinside.com 2019 - 2024. All rights reserved.