/
GC.cs
117 lines (92 loc) · 2.58 KB
/
GC.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#region Header
// _,-'/-'/
// . __,-; ,'( '/
// \. `-.__`-._`:_,-._ _ , . ``
// `:-._,------' ` _,`--` -: `_ , ` ,' :
// `---..__,,--' (C) 2023 ` -'. -'
// # Vita-Nex [http://core.vita-nex.com] #
// {o)xxx|===============- # -===============|xxx(o}
// # #
#endregion
#region References
using System;
using System.Diagnostics;
using Server;
using Server.Network;
#endregion
namespace VitaNex.Commands
{
/// <summary>
/// Force full Garbage Collection cycle on all generations.
/// </summary>
public static class GCCommand
{
private static bool _Initialized;
private static bool _Optimizing;
public static void Initialize()
{
if (_Initialized)
{
return;
}
_Initialized = true;
CommandUtility.Register(
"GC",
AccessLevel.Administrator,
e =>
{
var message = true;
if (e.Arguments != null && e.Arguments.Length > 0)
{
message = e.GetBoolean(0);
}
Optimize(e.Mobile, message);
});
CommandUtility.RegisterAlias("GC", "Optimize");
}
public static void Optimize(bool message)
{
Optimize(null, message);
}
public static void Optimize(Mobile m, bool message)
{
if (World.Saving || World.Loading || _Optimizing)
{
return;
}
NetState.FlushAll();
NetState.Pause();
_Optimizing = true;
var now = DateTime.UtcNow;
if (message)
{
World.Broadcast(0x35, true, "[{0}]: The world is optimizing, please wait.", now.ToShortTimeString());
}
var watch = new Stopwatch();
watch.Start();
double mem = GC.GetTotalMemory(false);
GC.Collect();
mem -= GC.GetTotalMemory(false);
mem = (mem / 1024.0) / 1024.0;
watch.Stop();
_Optimizing = false;
if (m != null)
{
m.SendMessage("[{0}]: GC done in {1:F2} seconds.", now.ToShortTimeString(), watch.Elapsed.TotalSeconds);
m.SendMessage("[{0}]: GC reports {1:#,0.00} MB freed memory.", now.ToShortTimeString(), mem);
}
Console.WriteLine("[{0}]: GC done in {1:F2} seconds.", now.ToShortTimeString(), watch.Elapsed.TotalSeconds);
Console.WriteLine("[{0}]: GC reports {1:#,0.00} MB freed memory.", now.ToShortTimeString(), mem);
if (message)
{
World.Broadcast(
0x35,
true,
"[{0}]: World optimization complete. The entire process took {1:F1} seconds.",
DateTime.UtcNow.ToShortTimeString(),
watch.Elapsed.TotalSeconds);
}
NetState.Resume();
}
}
}