【问题标题】:Reducing memory footprint of my application减少我的应用程序的内存占用
【发布时间】:2012-03-25 19:05:46
【问题描述】:

我正在开发内存有限的 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; }
    }
}

【问题讨论】:

  • 我不禁注意到您为每个站点存储 BusRoute,为每个路线存储 BusStop,这不是浪费内存吗?在您的情况下,链接列表不会更有效,因为每个节点都是 BusStop。

标签: c# .net memory memory-management


【解决方案1】:

【讨论】:

    【解决方案2】:

    简单点 - 不要将它们加载到内存中。浪费而且完全不需要。嘿,当我订购一辆装满油的超级油轮来更换我车里的油时,大部分都是浪费,我能做些什么 - 好吧,只订购你需要的油量,而不是装满油的超级油轮。

    数据库的存在是有原因的,你知道的。

    【讨论】:

      【解决方案3】:

      要么不将它们加载到内存中,要么仅在需要它们时才加载它们,或者使用享元模式将某些属性共用。

      也许代理模式在某些方面也很有用,因为您负担不起加载/卸载如此大的对象。

      只是提出一些想法,但 20mb 的对象太疯狂了!你有图像和类似的东西吗?还是只有属性?因为据我所见,我可以想象您至少可以共享一些属性/对象!

      工厂模式也可以派上用场,以限制无用的实例化,并使您能够非常轻松地共享实例!

      资源:

      Factory pattern

      Proxy pattern

      Flyweight pattern

      Prototype pattern

      【讨论】:

        猜你喜欢
        • 2016-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多