2017-01-17 45 views
-1

在ASP.NET網站,我有static readonly XGeoPhone provider = new XGeoPhone();。在XGeoPhone類裏面我有static readonly XPhoneInfo[] phoneInfo = new[] { new XPhoneInfo(......), ... 250000 more objects ... }。當我開發機還是真實服務器上運行這一點 - 它崩潰的new XGeoPhone()StackOverflowException。我不太明白它試圖在棧上創建這個巨大的數組,而不是堆?到底是怎麼回事?靜態只讀字段導致StackOverflowException

UPDATE:最簡單的版本會崩潰:

public partial class XGeoPhone 
{ 
    public XPhoneInfo GetInfo(long phone) 
    { 
     return 
      phoneInfos. 
      FirstOrDefault(pi => 
       pi.Start <= phone && 
       phone <= pi.End); 
    } 

    static readonly XPhoneInfo[] phoneInfos = new[] 
    { 
     new XPhoneInfo(2000000000, 2099999999, XCountryId.EG, null, null), 
     new XPhoneInfo(2120000000, 2129999999, XCountryId.MA, null, null), 
     new XPhoneInfo(2130000000, 2139999999, XCountryId.DZ, null, null), 
     new XPhoneInfo(2160000000, 2169999999, XCountryId.TN, null, null), 
     new XPhoneInfo(2180000000, 2189999999, XCountryId.LY, null, null), 
     ......... 
    } 
} 

public class XPhoneInfo 
{ 
    public XPhoneInfo(long start, long end, XCountryId? country, string region, string provider) 
    { 
     this.Start = start; 
     this.End = end; 
     this.Country = country; 
     this.Region = region; 
     this.Provider = provider; 
    } 

    public long Start { get; private set; } 
    public long End { get; private set; } 
    public XCountryId? Country { get; private set; } 
    public string Region { get; private set; } 
    public string Provider { get; private set; } 
} 

class Program 
{ 
    static readonly XGeoPhone g = new XGeoPhone(); 

    static void Main(string[] args) 
    { 
     var v = g.GetInfo(79054567890); 
    } 
} 
+2

可能是你有一個遞歸結構 –

+1

'250000'對象手動添加的代碼? **代碼味道** –

+3

'.... 25萬多個對象....'那氣味orribly! –

回答

-1

好吧,我找到了解決辦法 - 我可以創建一個線程和線程的構造函數中指定的堆棧大小,就像描述here。有用。但我決定將這個大陣列移動到csv文件。