Skip to content

Commit

Permalink
feat: Add Skia memory manager support
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Jan 26, 2022
1 parent e8e63f7 commit 699731b
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Uno.UWP/Generated/3.0.0.0/Windows.System/MemoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Windows.System
#endif
public static partial class MemoryManager
{
#if false || __IOS__ || NET461 || false || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || __IOS__ || NET461 || false || false || __NETSTD_REFERENCE__ || __MACOS__
[global::Uno.NotImplemented("__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public static ulong AppMemoryUsage
{
Expand All @@ -17,7 +17,7 @@ public static ulong AppMemoryUsage
}
}
#endif
#if false || __IOS__ || NET461 || false || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || __IOS__ || NET461 || false || false || __NETSTD_REFERENCE__ || __MACOS__
[global::Uno.NotImplemented("__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public static ulong AppMemoryUsageLimit
{
Expand Down
5 changes: 5 additions & 0 deletions src/Uno.UWP/System/MemoryManager.Wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ public partial class MemoryManager
{
private const string JsType = "Windows.System.MemoryManager";

static MemoryManager()
{
IsAvailable = true;
}

public static ulong AppMemoryUsage
{
get
Expand Down
1 change: 1 addition & 0 deletions src/Uno.UWP/System/MemoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Windows.System;

public partial class MemoryManager
{
internal static bool IsAvailable { get; private set; }
internal static float HighPressureThreshold { get; set; } = .90f;
internal static float MediumPressureThreshold { get; set; } = .70f;

Expand Down
90 changes: 90 additions & 0 deletions src/Uno.UWP/System/MemoryManager.netcore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#if __SKIA__
#nullable enable

using System;
using System.Globalization;
using System.Reflection;
using Uno.Foundation;

namespace Windows.System
{
public partial class MemoryManager
{
#if !NET6_0_OR_GREATER
private static PropertyInfo? _appMemoryUsageProperty;
private static PropertyInfo? _highMemoryLoadThresholdBytesProperty;
private static MethodInfo? _getGCMemoryInfoMethod;

static MemoryManager()
{
InitializeGCMemoryInfo();
}

private static void InitializeGCMemoryInfo()
{
_getGCMemoryInfoMethod = typeof(GC).GetMethod("GetGCMemoryInfo");

if (_getGCMemoryInfoMethod != null)
{
var gcMemoryInfo = Type.GetType("System.GCMemoryInfo");

if (gcMemoryInfo != null)
{
// Use TotalCommittedBytes first, if available.
_appMemoryUsageProperty = gcMemoryInfo.GetProperty("TotalCommittedBytes");

// Use TotalCommittedBytes first, otherwise use HeapSizeBytes (net5).
_appMemoryUsageProperty = _appMemoryUsageProperty ?? gcMemoryInfo.GetProperty("HeapSizeBytes");
_highMemoryLoadThresholdBytesProperty = gcMemoryInfo.GetProperty("HighMemoryLoadThresholdBytes");

IsAvailable = true;
return;
}
}

IsAvailable = false;
}
#endif

public static ulong AppMemoryUsage
{
get
{
#if NET6_0_OR_GREATER
return GC.GetGCMemoryInfo().MemoryLoadBytes;
#else
if (IsAvailable)
{
var info = _getGCMemoryInfoMethod!.Invoke(null, null);
return (ulong)(long)_appMemoryUsageProperty!.GetValue(info)!;
}
else
{
return 0;
}
#endif
}
}

public static ulong AppMemoryUsageLimit
{
get
{
#if NET6_0_OR_GREATER
return GC.GetGCMemoryInfo().HighMemoryLoadThresholdBytes;
#else
if (IsAvailable)
{
var info = _getGCMemoryInfoMethod!.Invoke(null, null);
return (ulong)(long)_highMemoryLoadThresholdBytesProperty!.GetValue(info)!;
}
else
{
return 0;
}
#endif
}
}
}
}
#endif

0 comments on commit 699731b

Please sign in to comment.