I would suggest using a DataReader instead of a DataTable. It looks like the way you're doing this there will be two copies of your data in memory.
"Out of Memory" error when using store procedures
-
14-04-2022 - |
Pergunta
I wrote some services for creating dbs, copying some records among dbs or tables etc, so many db operations. But im dealing with memory problems. One of them:
Im getting records with store procedures and filling in entity lists;
public List<FpBatteryEntity> LoadFPBattery()
{
List<FpBatteryEntity> retVal = new List<FpBatteryEntity>();
DataSet dataSet = _dataAccessProvider.ExecuteSPDataSet("sp_fp_battery", null);
if (dataSet != null && dataSet.HasData())
{
DataTable requestsTable = dataSet.Tables[0];
if (requestsTable != null && requestsTable.HasData())
{
foreach (DataRow row in requestsTable.Rows)
{
FpBatteryEntity entity = new FpBatteryEntity()
{
Faceplate = row["tf_faceplate"],
Battery = row["tf_battery"]
};
retVal.Add(entity);
}
}
}
return retVal;
}
Then for each entity in list, executing a new sp;
public bool LoadFPBattery(List<FpBatteryEntity> entityList)
{
foreach (var entity in entityList)
{
DBParameterCollection parameters = new DBParameterCollection
{
new DBParameter("@faceplate", SqlDbType.VarChar, entity.Faceplate),
new DBParameter("@battery", SqlDbType.VarChar, entity.Battery)
};
_dataAccessProvider.ExecuteSPNonQuery("sp_ins_fp_battery", parameters);
}
return true;
}
Unfortunately, I have more than 30 operations like that and also some of them are processing thousands of records.
Calling steps like that;
.....
List<MapColorEntity> mapColor = _dbRepository.LoadMapColor(marketId);
if (!_otherDbRepository.LoadMapColor(mapColor))
return false;
List<AttributesEntity> attributes = _dbRepository.LoadOptionAttrib(marketId);
if (!_otherDbRepository.LoadOptionAttrib(OptionAttributes))
return false;
List<FpBatteryEntity> fpBattery = _dbRepository.LoadFPBattery();
if (!_otherDbRepository.DelFPBattery())
return false;
if (!_otherDbRepository.LoadFPBattery(fpBattery))
return false;
.....
After 5-6 step operations are corrupting with "Out of Memory" error. I have to make these all operations step by step. Just wondering that if you have a suggestion to make these operations more fruitful and smooth. How can i solve this memory problem? What happens in back?
Usages;
Windows 7 Operating System, 3Gb Usable Ram, Sybase ASE, Odbc Driver
Solução