Skip to content

SaveAs can cause excessive heap usage when saving workbooks with many large worksheet XML parts #2342

Description

@1436761858

Description

Description

We are seeing very high heap usage when calling SaveAs on workbooks created from large templates.

In our production workload, we use XLSX templates that contain many worksheets. The service opens the template, writes report data into it, and then calls SaveAs to generate the final workbook.

For some large templates, memory usage stays relatively low while opening the file and writing rows, but increases sharply during SaveAs. In one case, heap usage rose to around 1.5 GB during save. This can cause OOM in smaller production containers, for example 2 CPU / 2 GB memory containers.

The issue becomes more visible when the workbook contains many large worksheet XML parts. Those worksheet XML files can be spilled to temporary files when opening the workbook, but they are loaded back into memory during save.

Root Cause

In writeToZip, temp-file-backed entries are currently written with:

if n, err = fi.Write(f.readBytes(path)); int64(n) > math.MaxUint32 {
    f.zip64Entries = append(f.zip64Entries, path)
}

I will create a pr, could help check it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs more infoThis issue can't reproduce, need more info

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions