Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Mon Sep 25, 2023 3:06 am

Hi there,

I found a problem that memory usage increase a lot when calling Sheet.Rows.Length. I am wondering is it memory leak or by design?

Test code shows as below

public MainWindow()
{
InitializeComponent();

var workbook = new Spire.Xls.Workbook();
workbook.LoadFromFile("test.xlsx");
var sheet1 = workbook.Worksheets[0];
for (int i = 0; i < sheet1.Rows.Length; i++) //memory usage increases more tha 10MB each loop.
{
//do nothing
}
}

thanks,
Pan

Panpan03
 
Posts: 3
Joined: Mon Sep 25, 2023 2:55 am

Mon Sep 25, 2023 3:53 am

Hi,

Thanks for your feedback.
I have tested the latest version 13.9.1 of our product, but I was unable to reproduce the problem you described. Typically, there should be no memory increase if no variables are being manipulated within the loop.

To further investigate this matter, could you please confirm whether you are using the latest commercial version of our product? If not, we kindly suggest updating to the latest version and retrying the test. Additionally, if the issue persists after the update, it would be helpful if you could provide us with the test document and details about your testing environment.

We appreciate your cooperation in resolving this matter. Should you have any further questions or concerns, please feel free to contact us. We are here to assist you.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Mon Sep 25, 2023 6:24 am

HI Triste,

Thanks for your quick verification, I am currently using version 13.9.1.0, and the code was built by Visual studio 2019.

You can watch the process memory by debugging the code.

Please find the attached test code and excel document.

thanks,
Pan
Attachments
WpfApp2.zip
test code and excel document
(1.52 MiB) Downloaded 375 times

Panpan03
 
Posts: 3
Joined: Mon Sep 25, 2023 2:55 am

Mon Sep 25, 2023 7:48 am

Hi Pan,

Thank you for your feedback.
I have successfully reproduced the issue you reported and consulted our development team regarding this matter. They explained that when using the sheet1.Rows.Length property, the CellRange array inside it is rebuilt every time. Considering the large number of rows in your document, this results in increased memory usage with each iteration of the for loop.

To optimize the process, we suggest using one of the following approaches:

Option 1:

Code: Select all
CellRange[] rows = sheet1.Rows;
for (int i = 0; i < rows.Length; i++)
{
    // Your code here
}

Option 2:

Code: Select all
int rowCount = sheet1.Rows.Length;
for (int i = 0; i < rowCount; i++)
{
    // Your code here
}


By implementing either of these approaches, you can avoid unnecessary memory consumption during each iteration. Please let us know if you have any further questions or concerns.

Thank you for bringing this to our attention, and we appreciate your patience and understanding.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Mon Sep 25, 2023 1:09 pm

Get it,thanks a lot! :lol:

Panpan03
 
Posts: 3
Joined: Mon Sep 25, 2023 2:55 am

Tue Sep 26, 2023 1:31 am

Hi Pan,

Thanks for your feedback. If you have any other questions, just feel free to contact us.

Have a nice day! :D

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.XLS