Fix video source memory handling #122
Merged
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.
This PR addresses two memory issues related to video capture, the first of which was identified by the Unity Profiler as a key contributor to broader performance problems:
WebCameraSource: the read buffer method was performing two unnecessary heap allocations per call, leading to large GC events and severe performance impacts in other parts of the SDK (namely in audio capture):*
Span<T>.ToArrayalso performs heap allocation as per the docs.The solution is to only allocate the temporary buffer once until the configuration is invalidated, and use
MemoryMarshalandSpanmethods to avoid the additionalToArraycall:RtcVideoSourceand its subclasses was not disposed of, causing a memory leak; when a native array is created usingAllocator.Persistent, a manual call to dispose is required.RtcVideoSourcenow implementsIDisposeto ensure the native array is disposed of even ifDispose()is not manually called.CLT-1613