| | | 1 | | using System; |
| | | 2 | | using System.Diagnostics; |
| | | 3 | | using System.Threading; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using SwitchBlade.Contracts; |
| | | 6 | | using SwitchBlade.Core; |
| | | 7 | | |
| | | 8 | | namespace SwitchBlade.Services |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Background service for tracking memory usage and cache statistics to diagnose leaks. |
| | | 12 | | /// Runs every 60 seconds. |
| | | 13 | | /// </summary> |
| | | 14 | | public class MemoryDiagnosticsService : IDisposable |
| | | 15 | | { |
| | | 16 | | private readonly IPeriodicTimer _timer; |
| | | 17 | | private readonly CancellationTokenSource _cts; |
| | | 18 | | private Task? _executionTask; |
| | | 19 | | |
| | | 20 | | private readonly IWindowOrchestrationService _orchestrationService; |
| | | 21 | | private readonly IIconService _iconService; |
| | | 22 | | private readonly IWindowSearchService _searchService; |
| | | 23 | | private readonly ILogger _logger; |
| | | 24 | | private readonly IProcessFactory _processFactory; |
| | | 25 | | private readonly IMemoryInfoProvider _memoryInfoProvider; |
| | | 26 | | |
| | 14 | 27 | | public MemoryDiagnosticsService( |
| | 14 | 28 | | IWindowOrchestrationService orchestrationService, |
| | 14 | 29 | | IIconService iconService, |
| | 14 | 30 | | IWindowSearchService searchService, |
| | 14 | 31 | | ILogger logger, |
| | 14 | 32 | | IProcessFactory? processFactory = null, |
| | 14 | 33 | | IMemoryInfoProvider? memoryInfoProvider = null, |
| | 14 | 34 | | Func<TimeSpan, IPeriodicTimer>? timerFactory = null, |
| | 14 | 35 | | TimeSpan? interval = null) |
| | 14 | 36 | | { |
| | 14 | 37 | | _orchestrationService = orchestrationService ?? throw new ArgumentNullException(nameof(orchestrationService) |
| | 13 | 38 | | _iconService = iconService ?? throw new ArgumentNullException(nameof(iconService)); |
| | 12 | 39 | | _searchService = searchService ?? throw new ArgumentNullException(nameof(searchService)); |
| | 11 | 40 | | _logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| | 10 | 41 | | _processFactory = processFactory ?? new ProcessFactory(new SystemProcessProvider()); |
| | 10 | 42 | | _memoryInfoProvider = memoryInfoProvider ?? new SystemMemoryInfoProvider(); |
| | | 43 | | |
| | 10 | 44 | | var timerInterval = interval ?? TimeSpan.FromSeconds(60); |
| | 10 | 45 | | _timer = timerFactory?.Invoke(timerInterval) ?? new SystemPeriodicTimer(timerInterval); |
| | 10 | 46 | | _cts = new CancellationTokenSource(); |
| | 10 | 47 | | } |
| | | 48 | | |
| | | 49 | | public Task StartAsync(CancellationToken cancellationToken) |
| | 4 | 50 | | { |
| | 4 | 51 | | _ = cancellationToken; |
| | 4 | 52 | | _logger.Log("MemoryDiagnosticsService starting..."); |
| | 4 | 53 | | _executionTask = RunDiagnosticsLoop(); |
| | 4 | 54 | | return Task.CompletedTask; |
| | 4 | 55 | | } |
| | | 56 | | |
| | | 57 | | public async Task StopAsync(CancellationToken cancellationToken) |
| | 5 | 58 | | { |
| | 5 | 59 | | _ = cancellationToken; |
| | 5 | 60 | | _logger.Log("MemoryDiagnosticsService stopping..."); |
| | 5 | 61 | | _cts.Cancel(); |
| | 5 | 62 | | if (_executionTask != null) |
| | 4 | 63 | | { |
| | | 64 | | try |
| | 4 | 65 | | { |
| | 4 | 66 | | await _executionTask; |
| | 2 | 67 | | } |
| | 6 | 68 | | catch (OperationCanceledException) { } |
| | 4 | 69 | | } |
| | 5 | 70 | | } |
| | | 71 | | |
| | | 72 | | private async Task RunDiagnosticsLoop() |
| | 4 | 73 | | { |
| | | 74 | | // First delay before first tick (standard periodic timer behavior) |
| | | 75 | | try |
| | 4 | 76 | | { |
| | 5 | 77 | | while (await _timer.WaitForNextTickAsync(_cts.Token)) |
| | 1 | 78 | | { |
| | 1 | 79 | | ForceLogMemoryStats(); |
| | 1 | 80 | | } |
| | 1 | 81 | | } |
| | 3 | 82 | | catch (Exception ex) when (ex is not OperationCanceledException) |
| | 1 | 83 | | { |
| | 1 | 84 | | _logger.LogError("MemoryDiagnosticsService loop error", ex); |
| | 1 | 85 | | } |
| | 2 | 86 | | } |
| | | 87 | | |
| | | 88 | | public void ForceLogMemoryStats() |
| | 3 | 89 | | { |
| | | 90 | | try |
| | 3 | 91 | | { |
| | | 92 | | // Force a check on the current process |
| | 3 | 93 | | using var proc = _processFactory.GetCurrentProcess(); |
| | 2 | 94 | | proc.Refresh(); |
| | | 95 | | |
| | 2 | 96 | | long managedMemory = _memoryInfoProvider.GetTotalMemory(false); // Bytes |
| | 2 | 97 | | long workingSet = proc.WorkingSet64; // Bytes (RAM) |
| | 2 | 98 | | long privateBytes = proc.PrivateMemorySize64; // Bytes (Committed) |
| | 2 | 99 | | int handleCount = proc.HandleCount; |
| | 2 | 100 | | int threadCount = proc.ThreadCount; |
| | | 101 | | |
| | | 102 | | // Cache Stats — using IDiagnosticsProvider.CacheCount via interfaces |
| | 2 | 103 | | int winCache = _orchestrationService.CacheCount; |
| | 2 | 104 | | int iconCache = _iconService.CacheCount; |
| | | 105 | | |
| | 2 | 106 | | string msg = $"\n[MEM-DIAG] " + |
| | 2 | 107 | | $"Managed: {FormatBytes(managedMemory)} | " + |
| | 2 | 108 | | $"Private: {FormatBytes(privateBytes)} | " + |
| | 2 | 109 | | $"WorkingSet: {FormatBytes(workingSet)} | " + |
| | 2 | 110 | | $"Handles: {handleCount} | " + |
| | 2 | 111 | | $"Threads: {threadCount} | " + |
| | 2 | 112 | | $"caches(Win/Icon): {winCache}/{iconCache}"; |
| | | 113 | | |
| | 2 | 114 | | _logger.Log(msg); |
| | 2 | 115 | | } |
| | 1 | 116 | | catch (Exception ex) |
| | 1 | 117 | | { |
| | 1 | 118 | | _logger.LogError("Failed to log memory stats", ex); |
| | 1 | 119 | | } |
| | 3 | 120 | | } |
| | | 121 | | |
| | | 122 | | private static string FormatBytes(long bytes) |
| | 6 | 123 | | { |
| | 6 | 124 | | return $"{bytes / 1024 / 1024} MB"; |
| | 6 | 125 | | } |
| | | 126 | | |
| | | 127 | | public void Dispose() |
| | 11 | 128 | | { |
| | 11 | 129 | | _cts.Dispose(); |
| | 11 | 130 | | _timer.Dispose(); |
| | 11 | 131 | | GC.SuppressFinalize(this); |
| | 11 | 132 | | } |
| | | 133 | | } |
| | | 134 | | } |