1
- using System ;
1
+ #nullable enable
2
+
3
+ using System ;
2
4
using Android . OS ;
3
5
using Android . App ;
4
6
using Uno . UI ;
@@ -7,25 +9,52 @@ namespace Windows.System
7
9
{
8
10
public partial class MemoryManager
9
11
{
12
+ private static ulong _appMemoryUsage ;
13
+ private static ulong _appMemoryUsageLimit ;
14
+
15
+ private static Debug . MemoryInfo ? _mi ;
16
+ private static ActivityManager . MemoryInfo ? _memoryInfo ;
17
+ private static global ::System . Diagnostics . Stopwatch _updateWatch = global ::System . Diagnostics . Stopwatch . StartNew ( ) ;
18
+ private static long _lastUpdate = long . MinValue ;
19
+
20
+ private readonly static long _updateResolution = global ::System . Diagnostics . Stopwatch . Frequency ;
21
+
10
22
public static ulong AppMemoryUsage
11
23
{
12
24
get
13
25
{
14
- Debug . MemoryInfo mi = new Debug . MemoryInfo ( ) ;
15
- Debug . GetMemoryInfo ( mi ) ;
16
- var totalMemory = mi . TotalPss * 1024 ;
17
- return ( ulong ) totalMemory ;
26
+ Update ( ) ;
27
+
28
+ return _appMemoryUsage ;
18
29
}
19
30
}
20
31
21
32
public static ulong AppMemoryUsageLimit
22
33
{
23
34
get
24
35
{
25
- ActivityManager . MemoryInfo memoryInfo = new ActivityManager . MemoryInfo ( ) ;
26
- ActivityManager . FromContext ( ContextHelper . Current ) ? . GetMemoryInfo ( memoryInfo ) ;
36
+ Update ( ) ;
37
+
38
+ return _appMemoryUsageLimit ;
39
+ }
40
+ }
41
+
42
+ private static void Update ( )
43
+ {
44
+ var now = _updateWatch . ElapsedTicks ;
45
+ if ( now - _lastUpdate > _updateResolution )
46
+ {
47
+ _lastUpdate = now ;
48
+
49
+ _mi ??= new Debug . MemoryInfo ( ) ;
50
+ Debug . GetMemoryInfo ( _mi ) ;
51
+ var totalMemory = _mi . TotalPss * 1024 ;
52
+ _appMemoryUsage = ( ulong ) totalMemory ;
53
+
54
+ _memoryInfo ??= new ActivityManager . MemoryInfo ( ) ;
55
+ ActivityManager . FromContext ( ContextHelper . Current ) ? . GetMemoryInfo ( _memoryInfo ) ;
27
56
28
- return AppMemoryUsage + ( ulong ) memoryInfo . AvailMem - ( ulong ) memoryInfo . Threshold ;
57
+ _appMemoryUsageLimit = _appMemoryUsage + ( ulong ) _memoryInfo . AvailMem - ( ulong ) _memoryInfo . Threshold ;
29
58
}
30
59
}
31
60
}
0 commit comments