的列表中給出此代碼內存溢出的例外:爲波蘇斯
public class Customer
{
public int CustomerID { get; set; }
public string Name { get; set; }
public List<Qualification> Qualifications { get; set; }
}
public class Qualification
{
public QualificationType QualificationType { get; set; }
public decimal Value { get; set; }
}
public class Action
{
public ActionID { get; set; }
public int CustomerID { get; set; }
public decimal ActionValue { get; set; }
}
public class Service : IService
{
public List<Customer> ProcessCustomers()
{
List<Customer> customers = _customerService.GetCustomers(); // 250,000 Customers
List<Action> actions = _actionService.GetActions(); // 6,000
foreach (var action in actions) {
foreach (affectedCustomer in customers.Where(x => x.CustomerID < action.CustomerID)) {
affectedCustomer.Qualifications.Add(new Qualification { QualificationType = QualificationType.Normal, Value = action.ActionValue});
}
foreach (affectedCustomer in customers.Where (x => SpecialRules(x))) {
affectedCustomer.Qualifications.Add(new Qualification { QualificationType = QualificationType.Special, Value = action.ActionValue});
}
}
}
}
的「最有資格」的客戶可以擁有12000個資質結束。平均而言,客戶可能最終獲得約100個資格。
但是,在大約50個動作被處理之後,我很早就得到了一個OOME。在那個時候,我的List仍然只有250,000個客戶,但是在整個客戶中增加了約5,000,000個資格。
這很多嗎?似乎有點讓我印象深刻。我懷疑我可以有數千萬的客戶,每個客戶平均擁有1000個資格,並且仍然很好。我甚至沒有接近這一點。
我可以在代碼中做些什麼來提高效率?我意識到我可以將每個(或批量分組)的結果寫入數據庫,但我寧願在寫入結果之前儘可能在內存中做更多的事情。
這樣做是週期通過6000個動作,每個動作,增加了資格對於一些客戶的可變數目。對於每個操作,所有具有customerID> = Action-Causing客戶的客戶都將添加一個資格認證。這就是~12億增加的記錄。另外,對於每一項行動,8-10名客戶都會獲得資格認證。與12億美元相比,只有6萬條記錄。
我試圖在內存中這樣做,因爲我不想將數十億記錄插入到數據庫中。在下一步處理中,我將需要這種記錄分離方式,它將從上到下查看客戶資格和客戶ID步驟的差異。儘管最終我最終將結果(比SUM更復雜)放在數據庫中。但我只能通過查看個人資格差異的步驟來達到這些結果,如曲線上的評分。
我很樂意提供幫助,如果你告訴我你的代碼應該做什麼:) –
@EyalPerry我添加了目標。 – Suamere