chore(deps): update actions/setup-node action to v4.2.0
[ArchiSteamFarm.git] / ArchiSteamFarm.CustomPlugins.PeriodicGC / PeriodicGCPlugin.cs
blobaa87002eb4bd3d965c22a8bab078a1cbaabc6439
1 // ----------------------------------------------------------------------------------------------
2 // _ _ _ ____ _ _____
3 // / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
4 // / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
5 // / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
6 // /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
7 // ----------------------------------------------------------------------------------------------
8 // |
9 // Copyright 2015-2025 Ɓukasz "JustArchi" Domeradzki
10 // Contact: JustArchi@JustArchi.net
11 // |
12 // Licensed under the Apache License, Version 2.0 (the "License");
13 // you may not use this file except in compliance with the License.
14 // You may obtain a copy of the License at
15 // |
16 // http://www.apache.org/licenses/LICENSE-2.0
17 // |
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the License is distributed on an "AS IS" BASIS,
20 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 // See the License for the specific language governing permissions and
22 // limitations under the License.
24 using System;
25 using System.Composition;
26 using System.Runtime;
27 using System.Text.Json.Serialization;
28 using System.Threading;
29 using System.Threading.Tasks;
30 using ArchiSteamFarm.Core;
31 using ArchiSteamFarm.Plugins.Interfaces;
32 using JetBrains.Annotations;
34 namespace ArchiSteamFarm.CustomPlugins.PeriodicGC;
36 [Export(typeof(IPlugin))]
37 [UsedImplicitly]
38 internal sealed class PeriodicGCPlugin : IPlugin {
39 private const byte GCPeriod = 60; // In seconds
41 private static readonly Lock Lock = new();
42 private static readonly Timer PeriodicGCTimer = new(PerformGC);
44 [JsonInclude]
45 public string Name => nameof(PeriodicGCPlugin);
47 [JsonInclude]
48 public Version Version => typeof(PeriodicGCPlugin).Assembly.GetName().Version ?? throw new InvalidOperationException(nameof(Version));
50 public Task OnLoaded() {
51 TimeSpan timeSpan = TimeSpan.FromSeconds(GCPeriod);
53 ASF.ArchiLogger.LogGenericWarning($"Periodic GC will occur every {timeSpan.ToHumanReadable()}. Please keep in mind that this plugin should be used for debugging tests only.");
55 lock (Lock) {
56 PeriodicGCTimer.Change(timeSpan, timeSpan);
59 return Task.CompletedTask;
62 private static void PerformGC(object? state = null) {
63 ASF.ArchiLogger.LogGenericWarning($"Performing GC, current memory: {GC.GetTotalMemory(false) / 1024} KB.");
65 lock (Lock) {
66 GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
67 GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true);
70 ASF.ArchiLogger.LogGenericWarning($"GC finished, current memory: {GC.GetTotalMemory(false) / 1024} KB.");