Skip to content

Commit 6235c2d

Browse files
committed
update memory collection
update signalr hub send msg
1 parent cbab29f commit 6235c2d

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

Controllers/HomeController.cs

+8-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Linq;
5+
using System.Threading;
56
using System.Threading.Tasks;
67
using Microsoft.AspNetCore.Mvc;
78
using Microsoft.Extensions.Logging;
@@ -11,7 +12,6 @@ namespace SignalRMonitor.Controllers
1112
{
1213
public class HomeController : Controller
1314
{
14-
private static List<byte[]> MemoryMonster = new List<byte[]>();
1515
private readonly ILogger<HomeController> _logger;
1616

1717
public HomeController(ILogger<HomeController> logger)
@@ -32,7 +32,6 @@ public IActionResult Privacy()
3232
[HttpPost("/cpu")]
3333
public async Task<IActionResult> UseCPU()
3434
{
35-
Console.WriteLine("c");
3635
var timeout = DateTime.Now.AddSeconds(5);
3736
while (DateTime.Now.CompareTo(timeout) < 0)
3837
{
@@ -42,19 +41,19 @@ public async Task<IActionResult> UseCPU()
4241
return Ok();
4342
}
4443
[HttpPost("/ram")]
45-
public async Task<IActionResult> UseRAM()
44+
public void UseRAM()
4645
{
4746
byte[] buff = new byte[1024 * 1024 * 1024];
48-
for (var i = 0; i < buff.Length; i++) buff[i] = (byte)(i % 256);
49-
MemoryMonster.Add(buff);
47+
for (var i = 0; i < buff.Length; i++) buff[i] = (byte)(i);
5048
//Sleep 5 seconds to keep the memory space "active"
51-
await Task.Delay(5000);
49+
Thread.Sleep(5000);
5250
//release memory
53-
MemoryMonster = new List<byte[]>();
54-
return Ok();
51+
buff = null;
52+
GC.Collect();
53+
GC.WaitForPendingFinalizers();
54+
GC.Collect();
5555
}
5656

57-
5857
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
5958
public IActionResult Error()
6059
{

Hubs/StatsHub.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public async Task GetMonitor()
2828

2929
//System.Console.WriteLine($"CPU : {cpuUsage} RAM : {memUsage}");
3030

31-
await Clients.All.SendAsync("SendMonitorData", JsonSerializer.Serialize(
31+
await Clients.Client(Context.ConnectionId).SendAsync("SendMonitorData", JsonSerializer.Serialize(
3232
new
3333
{
3434
CPU = cpuUsage,

0 commit comments

Comments
 (0)