【发布时间】:2011-09-20 21:57:03
【问题描述】:
我正在使用 WPF 开发康威生命游戏的模拟器。
由于某种原因,有时程序会占用高达 400,000K 的内存(当我绘制大量单元格时非常快)。
如何减少内存使用和/或减少由此引起的延迟。
编辑 1: 主窗口代码: http://pastebin.com/mz0z7tBu
网格类: http://pastebin.com/ZHX1WBuK
细胞结构:
struct Cell
{
public int Neighbors {get; set;}
public bool Alive { get; set; }
}
编辑 2: 我将尝试解释程序结构: Cell 是一个结构,其中包含 AutoProperty 邻居的 int 类型和 AutoProperty IsAlive 的类型 bool。
CellGrid 是一个包装二维单元格数组的类。 每次迭代,每个 Cell 的 Neighbors 属性都会更新以包含存活的 Neighbors 的数量,然后将每个 Cell 的 IsALive 设置为 true 或 false,这取决于邻居的数量和之前的 IsAlive 状态。
MainWindow 类有一个 CellGrid 类型的对象。 它将网格渲染到屏幕上。
编辑 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>
【问题讨论】:
-
任何人都很难在没有看到代码的情况下提供指导。
-
如何控制世代之间的时间?
-
绘图代码还是单元格代码? @DMX 没有时间。只需单击一个按钮即可传递世代。
-
@Gilad Naaman:在共享代码之前,不要 / ,而是尝试对程序结构进行高级演示:例如。单元:它们是什么,有多大,何时/如何创建,哪些模块使用它们。例如。 “Generation manager”等。您可能会发现,在以适当的精度解释问题时,您可能会给自己一些想法,作为过度使用内存的可能来源。
-
添加程序结构+代码
标签: c# wpf xaml memory conways-game-of-life