I use ASP.NET Core 6 MVC with ClosedXML.Excel reader from Excel via parallel.For, but this function eats more than 1.5 Gb of Ram memory.
Image how much memory uses the function
public static void ReadFile(IFormFile MyFileCollection)
{
IFormFile MyFile;
int FileLen;
System.IO.Stream MyStream;
MyFile = MyFileCollection;
FileLen = Convert.ToInt32(MyFile.Length);
byte[] input = new byte[FileLen];
if (MyFile.FileName.Contains('~'))
{
NameOfUpload = MyFile.FileName.Split('~')[1].Split('.')[0];
}
// Initialize the stream.
MyStream = MyFile.OpenReadStream();
// Read the file into the byte array.
MyStream.Read(input, 0, FileLen);
bool _FindedHead = false;
var workbook = new XLWorkbook(MyStream, XLEventTracking.Disabled);
var worksheet = workbook.Worksheet(1);
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
using (var reader = ExcelReaderFactory.CreateReader(MyStream))
{
var result = reader.AsDataSet();
DataSet _DtSet = reader.AsDataSet();
DataTable dataTable = _DtSet.Tables[0];
DataView _Workscheet_DataViewData = dataTable.AsDataView();
var rows = dataTable.Rows.Count;
int _GlobalRow_Count = dataTable.Rows.Count;
string[,] _Data_ERS = new string[_GlobalRow_Count + 10, 3];
int _StartPosition = 1;
int localSizes = dataTable.Rows.Count;
string[] _Number_Nakladnoi = new string[localSizes];
string[] _NumberOrderOf_Sender = new string[localSizes];
string[] _Partiya = new string[localSizes];
string[] _NomerRZ = new string[localSizes];
int[] _PlaceCount = new int[localSizes];
string[] _MethodDelivery = new string[localSizes];
string[] _TypeOf_delivery = new string[localSizes];
string[] _CityDelivery = new string[localSizes];
string[] _PVZ_Target = new string[localSizes];
string[] _Kladr_PointDelivery = new string[localSizes];
DateTime[] _DateFaktDelivery = new DateTime[localSizes];
string[] _CargoState = new string[localSizes];
string[] _ReasonDontArrive = new string[localSizes];
double[] _WeightBySize = new double[localSizes];
double[] _WeightFakt = new double[localSizes];
double[] _COD = new double[localSizes];
string[] _TypeOfPay = new string[localSizes];
//string[] _LstERs = new string[localSizes];
string[] _TempCheck = new string[localSizes];
for (int z = 1; z < dataTable.Rows.Count; z++)
{
var _Parametr_Row = worksheet.Row(z);
_Number_Nakladnoi[z] = _Parametr_Row.Cell(1).GetValue<string>().ToString().Trim();
if (string.IsNullOrEmpty(_Number_Nakladnoi[z]))
{
continue;
}
if (_Number_Nakladnoi[z].ToLower() == "номер посылки")
{
_FindedHead = true; _StartPosition = z + 1;
break;
}
if (!_FindedHead)
{
continue;
}
}
ParallelOptions options = new ParallelOptions();
options.MaxDegreeOfParallelism = 150;
ConcurrentBag<DataFromFile> ConCurentCollection = new ConcurrentBag<DataFromFile>();
ConcurrentBag<int> ConCurentTest = new ConcurrentBag<int>();
DataFromFile[] _BagCollectionArray = new DataFromFile[dataTable.Rows.Count];
var resultParallel = Parallel.For(_StartPosition, dataTable.Rows.Count, options, (i, state) =>
{
var _Parametr_Row = worksheet.Row(i).Cell(4).CachedValue;
_Number_Nakladnoi[i] = worksheet.Row(i).Cell(1).GetValue<string>().ToString().Trim(); //
_NumberOrderOf_Sender[i] = worksheet.Row(i).Cell(2).GetValue<string>().ToString().Trim(); //
_Partiya[i] = worksheet.Row(i).Cell(4).CachedValue.ToString();// worksheet.Row(i).Cell(4).GetValue<string>(); //
_NomerRZ[i] = worksheet.Row(i).Cell(5).GetValue<string>().ToString().Trim(); //
_PlaceCount[i] = string.IsNullOrEmpty(worksheet.Row(i).Cell(6).GetValue<string>().ToString()) ? 0 : worksheet.Row(i).Cell(6).GetValue<int>(); //
_MethodDelivery[i] = worksheet.Row(i).Cell(7).GetValue<string>().ToString().Trim(); //
_TypeOf_delivery[i] = worksheet.Row(i).Cell(8).GetValue<string>().ToString().Trim();
_CityDelivery[i] = addresses.GetCorrectCityName(worksheet.Row(i).Cell(10).GetValue<string>().ToString().Trim()); //The city Name
_PVZ_Target[i] = worksheet.Row(i).Cell(11).GetValue<string>().ToString().Trim();
_Kladr_PointDelivery[i] = worksheet.Row(i).Cell(12).GetValue<string>().ToString().Trim(); //
_DateFaktDelivery[i] = !string.IsNullOrEmpty(worksheet.Row(i).Cell(15).GetValue<string>().ToString().Trim()) ? SystemClass.GetDateTimeFromString(worksheet.Row(i).Cell(15).GetValue<string>()) : SystemClass.GetDateTimeFromString(worksheet.Row(i).Cell(13).GetValue<string>()); //
_CargoState[i] = worksheet.Row(i).Cell(18).GetValue<string>().ToString().Trim();
_ReasonDontArrive[i] = worksheet.Row(i).Cell(19).GetValue<string>().ToString().Trim();
_WeightBySize[i] = string.IsNullOrEmpty(worksheet.Row(i).Cell(20).GetValue<string>().ToString()) ? 0 : worksheet.Row(i).Cell(20).GetValue<double>();
_WeightFakt[i] = string.IsNullOrEmpty(worksheet.Row(i).Cell(21).GetValue<string>().ToString()) ? 0 : worksheet.Row(i).Cell(21).GetValue<double>();
_COD[i] = string.IsNullOrEmpty(worksheet.Row(i).Cell(23).GetValue<string>().ToString()) ? 0 : worksheet.Row(i).Cell(23).GetValue<double>();
_BagCollectionArray[i] = new DataFromFile
{
_Number_Nakladnoi = _Number_Nakladnoi[i],
_NumberOrderOf_Sender = _NumberOrderOf_Sender[i],
_Partiya = _Partiya[i],
_NomerRZ = _NomerRZ[i],
_PlaceCount = _PlaceCount[i],
_MethodDelivery = _MethodDelivery[i],
_TypeOf_delivery = _TypeOf_delivery[i],
_CityDelivery = _CityDelivery[i],
_PVZ_Target = _PVZ_Target[i],
_Kladr_PointDelivery = _Kladr_PointDelivery[i],
_DateFaktDelivery = _DateFaktDelivery[i],
_CargoState = _CargoState[i],
_ReasonDontArrive = _ReasonDontArrive[i],
_WeightBySize = _WeightBySize[i],
_WeightFakt = _WeightFakt[i],
_COD = _COD[i]
};
ConCurentCollection.Add(AddNewElement(_BagCollectionArray[i]));
ConCurentTest.Add(i);
});
}
MyStream.Close();
worksheet.Delete();
workbook.Dispose();
}
I tried to dispose everything, but everything looks like before. Maybe I forget something or do something wrong.
I try to dispose MyStream, workbook, worksheet - but there is no result in the usage of memory.
I try reduce code and find out that the problem is on ClosedXML. I have tried many approaches to release resources but nothing help me. I also tried use not static function, objects and GC.
GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect();
Maybe somebody have an Idea about how to dispose ClosedXML? Or maybe I do something wrong