timz_123
timz_123

Reputation: 437

How to solve SystemOutOfMemory Exception

I am getting below exception in my application at line:

Workbook workbook = new Workbook(fstream);

Exception:

Exception of type 'System.OutOfMemoryException' was thrown.

C# Code:

string tempPath = @"C:\File\01_TDv01.xlsx";
foreach (string templateFile in Directory.GetFiles(tempPath))
{
    using (FileStream fstream = new FileStream(templateFile, FileMode.Open))
    {                    
         Workbook workbook = new Workbook(fstream);     //getting exception here
         Worksheet worksheet = workbook.Worksheets[0];
         ArrayList List = new ArrayList();

    //other code
    }
}
catch (Exception ex)
{

}

I am processing the Excel file of size 38,436 KB; with 10K rows in it.

Web Config Settings:

<add key="maxFileSizeLimit" value="2147483647" />

<httpRuntime targetFramework="4.5"  maxQueryStringLength="52768" enable="true" maxRequestLength="2147483647"/>

<requestLimits maxQueryString="52768" maxAllowedContentLength="4294967295" />

What is wrong in my code and how can I resolve this exception?

I can't make lot of changes in code as it is already live.

Upvotes: -1

Views: 1555

Answers (1)

Amjad Sahi
Amjad Sahi

Reputation: 1931

@user3196511

Could you provide us your template file "01_TDv01.xlsx". We need to evaluate your issue using your template file. Please also make sure you have sufficient memory (e.g 10 times or more memory of the size of the file) available for the big process as you are loading a large file We also recommend you to kindly try our latest version/fix: Aspose.Cells for .NET v17.6.x (if you are not already using it). Moreover, you should try to set MemoryPreference attribute on while loading your template file, see the following sample code for your reference: e.g Sample code:

        //Specify the LoadOptions            
        LoadOptions opt = new LoadOptions();
        //Set the memory preferences
        opt.MemorySetting = MemorySetting.MemoryPreference;

        using (FileStream fstream = new FileStream(templateFile, FileMode.Open))
{                    
     Workbook workbook = new Workbook(fstream, opt);     //getting exception here
     Worksheet worksheet = workbook.Worksheets[0];


//other code
}

I am working as Support developer/ Evangelist at Aspose.

Upvotes: 1

Related Questions