Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report native memory pressure to GC #1514

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

manodasanW
Copy link
Member

This PR reports the native memory pressure of any CsWinRT projected RCWs to the .NET GC. The .NET GC keeps track of managed memory and is not aware of native memory usage of anything that the C# managed objects are holding onto. So, in the case of CsWinRT projected RCWs, the .NET GC is aware of the managed memory usage of CsWinRT's RCW projection implementation, but the native WinRT object being held on can be using much more memory that the GC isn't aware of. By reporting this native memory pressure to the .NET GC, .NET is able to run the GC more often as needed based on memory usage and thereby making sure any collected objects are indeed finalized releasing the native memory. There is another similar change on the WinUI side this works together with which reports WinUI native memory pressure.

  • Reports native memory pressure based on the GCPressure attribute on RCWs and how much it indicates to report
  • Reports basic native memory pressure for all IObjectReferences as they are holding onto WinRT objects. This PR uses a simplified approach to achieving this by reporting it for any IObjectReference. In theory, multiple IObjectReferences can be pointing to the same WinRT object and we don't need to report native memory pressure each time, but we shouldn't have too much of an impact by doing so. In order to do the non-simplified approach, it will require another object allocation which isn't finalized until all the IObjectReferences held onto by an RCW or was from a QI from it is disposed. This gets more complicated to implement due to needing to wait for all related IObjectRefrences to finalize so deferring on that until there is a need for it.
  • Make use of the ComWrapper APIs that were added after .NET 5

src/WinRT.Runtime/ComWrappersSupport.cs Outdated Show resolved Hide resolved
src/cswinrt/code_writers.h Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants