2012-03-08 60 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

相关问题