2012-03-08 59 views
0

我正在開發限制內存的C#應用​​程序。我初始化了大量的對象,如下面那些有幾個屬性的對象,大約需要20MB。如何減少我的應用程序使用的內存量。減少我的應用程序的內存佔用量

public class BusStop 
{ 
    private List<BusRoute> busRoutes = new List<BusRoute>(); 
    private string name; 
    // ... Other properties omitted like Code, ID, Location, etc. 

    public BusStop(string name) 
    { 
     this.name = name; 
    } 

    public List<BusStop> BusRoutes 
    { 
     get { return this.busRoutes; } 
    } 

    public string Name 
    { 
     get { return this.name; } 
    } 
} 

public class BusRoute 
{ 
    private List<BusStop> busStops = new List<BusStop>(); 
    private string name; 
    // ... Other properties omitted like Code, ID, Location, etc. 

    public BusStop(string name) 
    { 
     this.name = name; 
    } 

    public List<BusStop> BusStops 
    { 
     get { return this.busStops; } 
    } 

    public string Name 
    { 
     get { return this.name; } 
    } 
} 
+1

我不禁注意到你存儲BusRoute的每一站和巴士站就爲每一個路線,不是嗎」那是浪費內存?因爲每個節點都是BusStop,所以你的情況不會更有效。 – alykhalid 2012-03-08 09:32:47

回答

3

簡單一點 - 不要把它們加載到內存中。浪費和完全不需要。嘿,當我點了一輛裝滿油的超級油輪來替換我車裏的油時,大部分都是浪費,我該怎麼辦 - 好吧,只需要儘可能多地點油,而不是超級油輪滿油。

數據庫是有原因的,你知道。

2

要麼不將它們加載到內存中,要麼只在需要它們時加載它們,或者使用享元模式來共同使用某些屬性。

也許代理模式也可能在某些方面有用,因爲你無法負擔加載/卸載這樣的大對象。

只是在想法,但20MB的對象是非常瘋狂的!你有圖像和類似的東西?還是隻有屬性?因爲從我所看到的,我可以想象你至少可以分享一些屬性/對象!

工廠模式也可以派上用場,以限制無用的實例,並使您能夠輕鬆共享實例!

資源:

Factory pattern

Proxy pattern

Flyweight pattern

Prototype pattern

+0

用於輕量級模式 – MattDavey 2012-03-08 09:59:22

相關問題