计划需要太多的内存 [英] Program takes too much memory

查看:145
本文介绍了计划需要太多的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WPF开发康威的生命游戏的模拟器。



从某些原因,有时程序占用到内存400,000K(当我画很多细胞真快)的。



我怎样才能减少内存使用和/或减少因它的滞后。



修改1:
主窗口代码:
HTTP:



网格类nofollow的> http://pastebin.com/ZHX1WBuK



细胞结构:

 结构细胞
{
公众诠释邻居{搞定;设置;}
公共布尔活着{搞定;组; }
}



编辑2:
我会尝试解释程序结构:
细胞是包含AutoProperty邻居ofType int类型的结构,AutoProperty的IsAlive ofType布尔



CellGrid是一个封装A级一个二维数组单元。
每次迭代,每个单元的邻居属性被更新,以包含邻居的数量还活着,然后在每个单元的的IsAlive设置为true或false,取决于邻居和以前的IsAlive状态的数量。



MainWindow类有一个类型CellGrid的对象。
这使得电网到屏幕上。



修改3:



XAML: http://pastebin.com/Zp3dr8zc



resources.xaml:

 < ResourceDictionary中的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml>
<风格的TargetType ={X:类型菜单项}>
< setter属性=Horizo​​ntalContentAlignmentVALUE =中心/>
< setter属性=VerticalContentAlignmentVALUE =中心/>
< setter属性=了maxHeightVALUE =32/>
< /样式和GT;
<风格的TargetType ={X:类型菜单项}X:键=ParentMenuItem>
< setter属性=宽度值=46/>
< /样式和GT;
< / ResourceDictionary的>


解决方案

这是使用的DrawingContext / DrawingVisual的结果。它实际上是良性的,都应该被垃圾收集系统需要它,但内存使用都可以报警。如果你要,而是画在画布上的形状,那么你可能不会看到这个问题。我碰到的,在过去​​的自定义绘制控件同样的问题。切换到更多的基于矢量的绘图技术(即在画布上的形状)固定内存消耗问题。


I'm using WPF to develop a simulator of Conway's Game of Life.

From some reason, sometimes the program takes up to 400,000K memory (When I draw a lot of cells really fast).

How can I reduce the memory usage and/or reduce the lags caused by it.

Edit 1: Main Window Code: http://pastebin.com/mz0z7tBu

Grid class: http://pastebin.com/ZHX1WBuK

cell struct:

struct Cell
{
    public int Neighbors {get; set;}
    public bool Alive { get; set; }
}

Edit 2: I'll try to explain Program Structure: Cell is a structure that contains AutoProperty neighbors ofType int, and AutoProperty IsAlive ofType bool.

CellGrid is a Class that wraps a 2D array of Cells. Every iteration, each Cell's Neighbors property is updated to contain the number of Neighbors alive, and then each Cell's IsALive is set to true or false, depends on number of neighbors and previous IsAlive state.

The MainWindow class has an object of type CellGrid. It renders the grid to the screen.

Edit 3:

XAML: http://pastebin.com/Zp3dr8zc

resources.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type MenuItem}">
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="MaxHeight" Value="32" />
    </Style>
    <Style TargetType="{x:Type MenuItem}" x:Key="ParentMenuItem">
        <Setter Property="Width" Value="46" />
    </Style>
</ResourceDictionary>

解决方案

This is the result of using a DrawingContext/DrawingVisual. It's actually benign and should all be garbage collected as the system needs it, but the memory usage can be alarming. If you were to, instead, draw shapes on a canvas then you would probably not see this problem. I've run into this same issue with custom drawn controls in the past. Switching to more vector-based drawing techniques (i.e., shapes on a canvas) fixed the memory consumption problem.

这篇关于计划需要太多的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆