Skip to content

Interactive 2D wave optics simulator. Turn any image into a refractive scene, add sources, watch waves refract, diffract and interfere. Probes, intensity maps, video export.

C#

The frame budget respects the commit limit; a preview bitmap that cannot be allocated no longer takes the app down

Alexander Graef043c38db43a4
diff --git a/CLAUDE.md b/CLAUDE.md
index 2fba504..6696bea 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -101,7 +101,10 @@ FrameStore: native memory up to the budget, the rest in a temp file (%LOCALAPPDA
the GPU, the 256-entry table serves the image sources. The importer's fallbacks differ from the model's defaults on purpose.
- **Frames vs steps**: frame k is the state after step `k × VisualizeEvery`.
- **The engine choice is a user setting** (Simulation > Use the GPU, default on, CPU fallback logged).
-- **Frame store**: budget = free memory minus 2 GB, capped by Simulation > RAM cache size (Auto or a share); the
+- **Frame store**: budget = the lesser of free physical memory and available commit (2026-10-19: Windows refuses an
+ allocation at the commit limit first, and a run that filled the physical measure left no room for the preview's
+ bitmap, which took the app down; a failed bitmap is now logged and the picture stays empty) minus 2 GB, capped by
+ Simulation > RAM cache size (Auto or a share); the
outgoing run's frames count as reclaimed and the new store waits for their release. No memory mapping, no OS
cache reliance (both wrote at disk speed). Disposed on the thread pool. Full HD, 2500 steps, both planes: 10.4 GB
in 16-bit, half in 8-bit.
diff --git a/WaveDudeNext.WPF/Rendering/FramePresenter.cs b/WaveDudeNext.WPF/Rendering/FramePresenter.cs
index 3b477fb..278c216 100644
--- a/WaveDudeNext.WPF/Rendering/FramePresenter.cs
+++ b/WaveDudeNext.WPF/Rendering/FramePresenter.cs
@@ -99,6 +99,9 @@ public sealed class FramePresenter
_source = value;
if (!sameShape)
{
+ // the old pictures go first, so their memory can be reclaimed for the new ones
+ FieldBitmap = null;
+ IntensityBitmap = null;
FieldBitmap = value != null && value.Has(FrameLayer.Field) ? NewBitmap(value) : null;
IntensityBitmap = value != null && value.Has(FrameLayer.Intensity) ? NewBitmap(value) : null;
}
@@ -137,7 +140,23 @@ public sealed class FramePresenter
Kick();
}
- private static WriteableBitmap NewBitmap(IFrameSource source) => new(source.Width, source.Height, 96, 96, PixelFormats.Bgr32, null);
+ /// <summary>
+ /// A picture of the source's size, or null when Windows has no memory for it (the bitmap is a shared section, which
+ /// fails at the commit limit as MILERR_WIN32ERROR; it took the application down on 2026-10-19 after a run had
+ /// filled the memory): logged as an error, the picture then stays empty until a source with a new shape comes.
+ /// </summary>
+ private static WriteableBitmap? NewBitmap(IFrameSource source)
+ {
+ try
+ {
+ return new WriteableBitmap(source.Width, source.Height, 96, 96, PixelFormats.Bgr32, null);
+ }
+ catch (Exception ex) when (ex is COMException or OutOfMemoryException)
+ {
+ Logger.Error(ex, "No memory for a preview picture of {Width}×{Height}: the picture stays empty", source.Width, source.Height);
+ return null;
+ }
+ }
private void Rebuild()
{
diff --git a/WaveDudeNext/Runs/FrameMemory.cs b/WaveDudeNext/Runs/FrameMemory.cs
index cb02eda..9ab9702 100644
--- a/WaveDudeNext/Runs/FrameMemory.cs
+++ b/WaveDudeNext/Runs/FrameMemory.cs
@@ -48,12 +48,18 @@ public static class FrameMemory
return GC.GetGCMemoryInfo().TotalAvailableMemoryBytes;
}
- /// <summary>Bytes of physical memory free right now (the standby cache counts as free, as Windows reports it).</summary>
+ /// <summary>
+ /// Bytes free for new allocations right now: the lesser of the free physical memory (the standby cache counts as
+ /// free, as Windows reports it) and the commit still available (physical memory plus page file minus what every
+ /// process has committed). Windows refuses any allocation at the commit limit, which with a small page file comes
+ /// before the physical memory is used up: a full-HD run of 12,500 frames filled the budget by the physical
+ /// measure alone and the preview's next bitmap then failed to allocate (2026-10-19).
+ /// </summary>
public static long AvailableBytes()
{
if (OperatingSystem.IsWindows() && TryStatus(out MemoryStatus status))
{
- return (long)Math.Min(status.AvailPhys, long.MaxValue);
+ return (long)Math.Min(Math.Min(status.AvailPhys, status.AvailPageFile), long.MaxValue);
}
return PhysicalBytes() / 2;
}