dotnet / Open-XML-SDK

Open XML SDK by Microsoft
https://www.nuget.org/packages/DocumentFormat.OpenXml/
MIT License
4.05k stars 547 forks source link

Memory Concumption GetFirstChild is way too big #1511

Open Berethor opened 1 year ago

Berethor commented 1 year ago

Before Saving excel files image After Saving excel file( GC just collected all unused data) image And After appending DataValidation with code below image

That memory usage after Validation look very awful, when output excel file has only 20mb size, and if i need add another validation to another list, memory just going higher like that image

i'm using that code to perform adding Validation

         using (SpreadsheetDocument document = SpreadsheetDocument.Open(docName, true))
        {
            IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == worksheetName);

            if (sheets.Count() == 0)
            {
                return;
            }

            WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheets.First().Id);

            WorksheetExtensionList worksheetExtensionList = new WorksheetExtensionList();
            WorksheetExtension worksheetExtension = new WorksheetExtension() { Uri = "{CCE6A557-97BC-4b89-ADB6-D9C93CAAB3DF}" };
            worksheetExtension.AddNamespaceDeclaration("x14", "http://schemas.microsoft.com/office/spreadsheetml/2009/9/main");

            DataValidations dataValidations = new DataValidations();
            DataValidation dataValidation = null;
            if (!isRange)
            {
                dataValidation = new DataValidation
                {
                    Type = DataValidationValues.List,
                    AllowBlank = true,
                    SequenceOfReferences = new ListValue<StringValue> { InnerText = string.Format("{0}1:{0}1048576", column) }
                };

                dataValidation.Append(
                    new Formula1 { Text = dataContainingSheet }
                    );
            }
            else
            {
                X14.DataValidations dataValidations14 = new X14.DataValidations() { Count = (UInt32Value)1U };
                dataValidations14.AddNamespaceDeclaration("xm", "http://schemas.microsoft.com/office/excel/2006/main");
                dataValidations14.Append(new X14.DataValidation()
                {
                    Type = DataValidationValues.List,
                    AllowBlank = true,
                    ReferenceSequence = new DocumentFormat.OpenXml.Office.Excel.ReferenceSequence(string.Format("{0}1:{0}1048576", column)),
                    DataValidationForumla1 = new X14.DataValidationForumla1() { Formula = new DocumentFormat.OpenXml.Office.Excel.Formula(dataContainingSheet) }
                });

                worksheetExtension.Append(dataValidations14);
                worksheetExtensionList.Append(worksheetExtension);
                worksheetPart.Worksheet.Append(worksheetExtensionList);
                worksheetPart.Worksheet.Save();
                return;
            }
            var oldDataValidations = worksheetPart.Worksheet.GetFirstChild<DataValidations>();
            if (oldDataValidations != null)
            {
                if (dataValidation != null)
                    oldDataValidations.Append(dataValidation);
            }
            else
            {
                dataValidations.Append(dataValidation);
                worksheetPart.Worksheet.AppendChild(dataValidations);
            }

on worksheetPart.Worksheet.GetFirstChild it's just taking a lot of memory for that action.

If it was only on my PC, there is no problem, but i have to run that on PC with only 8Gb RAM and it throws OutOfMemory exception sometimes

Desktop (please complete the following information):

Additional context Just don't know how to solve that problem, or there is some another way to add DataValidations to excel files without that memory concumption

Berethor commented 9 months ago

Is there still no help or solution for this issue?

AlfredHellstern commented 9 months ago

@Berethor have you tried this with the most recent 3.0.1?

Berethor commented 1 week ago

@Berethor have you tried this with the most recent 3.0.1?

Yep, tried on last version, still the same