Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem Description
Fix for issue#1591
Since CasparCG 2.4, the NDI consumer has exhibited a memory leak, particularly when used with some integrated graphics (like Intel Iris Xe). The issue can also affect Nvidia discrete GPU users. The leak occurs upon starting NDI output and causes system memory to continuously grow until the system runs out of RAM (or VRAM for discrete GPUs).
Root Cause
The NDI consumer's frame buffer queue had no size limit, allowing frames to accumulate indefinitely when the NDI send operation cannot keep up with the incoming frame rate. On Intel integrated graphics (which share system RAM), this manifests as an obvious memory leak. On Nvidia discrete GPUs (with dedicated VRAM), the issue is less noticeable.
Additionally, frame objects were held in memory longer than necessary, not being released until the next loop iteration after the sleep period.
Proposed Solution
This fix implements three key changes:
Limits the frame buffer to a maximum of 8 frames. When the buffer exceeds this limit, excess frames are dropped rather than accumulated. This prevents unbounded memory growth while maintaining smooth output.
Aggregates dropped frame warnings to log once per second rather than per-frame, preventing log spam while still providing visibility into system performance.
Explicitly releases frame memory after sending and sleeping, rather than waiting for the next loop iteration. This ensures immediate memory reclamation.
Performance Notes
On systems where NDI encoding cannot maintain real-time performance (particularly Intel integrated graphics at high resolutions/framerates), this fix will drop frames to maintain system stability rather than accumulating them in memory. This is the correct behavior - graceful degradation is preferable to memory exhaustion.
Users with high-performance discrete GPUs should see no change in behavior, as the buffer limit of 4 frames is sufficient for normal timing jitter absorption.
Files Changed
modules/newtek/consumer/newtek_ndi_consumer.cppFuture Enhancements (Optional)
The buffer size could potentially be made configurable via the CasparCG configuration file to allow power users to tune performance for their specific hardware:
However, this is not included in the current fix to keep the change minimal and focused on resolving the memory leak.