C
C#10mo ago
Groophy

392,2 MB allocated in SOH

I'm gonna write, please wait one minute.
No description
No description
No description
No description
3 Replies
Groophy
GroophyOP10mo ago
I'm gonna host some files which mostly dll files. And I get 200req/s so I made an pool system but It seems not work async and so much allocate, so after three days, I may need help. NetworkListener.cs
var listener = listenerList[i].listener;
var port = listenerList[i].port;
var requestPool = new RequestPool(10);

// call before action
action?.Invoke(port);

listener.Start();

await Task.Run(() =>
{
requestPool.SetAction(async callback =>
{
TcpClient client = listener.EndAcceptTcpClient(callback);

// Store endpoint
var endPoint = client.Client.LocalEndPoint;

// handle client request and response
await HandleClient(path, client);

// closes client if not closed.
if (GetState(endPoint) != TcpState.Closed)
client.Close();
});

while (true)
{
// If server was trying to stop.
if (listenFixer.isJobFixed) break;

// Accept to connect request
listener.BeginAcceptTcpClient(
async (callback)
=> requestPool.AddToPool(callback),
null);
}

listener.Stop();
listenFixer.SetState(State.Done);
});
var listener = listenerList[i].listener;
var port = listenerList[i].port;
var requestPool = new RequestPool(10);

// call before action
action?.Invoke(port);

listener.Start();

await Task.Run(() =>
{
requestPool.SetAction(async callback =>
{
TcpClient client = listener.EndAcceptTcpClient(callback);

// Store endpoint
var endPoint = client.Client.LocalEndPoint;

// handle client request and response
await HandleClient(path, client);

// closes client if not closed.
if (GetState(endPoint) != TcpState.Closed)
client.Close();
});

while (true)
{
// If server was trying to stop.
if (listenFixer.isJobFixed) break;

// Accept to connect request
listener.BeginAcceptTcpClient(
async (callback)
=> requestPool.AddToPool(callback),
null);
}

listener.Stop();
listenFixer.SetState(State.Done);
});
and it's gets so much allocate also after one time, can't handle requests. RequestPool.cs
public class RequestPool
{
private int _poolSize;
private Queue<IAsyncResult> _pool;
private Action<IAsyncResult> _action;
private List<Task> _workerTasks;

public RequestPool(int poolSize)
{
_poolSize = poolSize;
_pool = new Queue<IAsyncResult>();
_action = (_) => {};
_workerTasks = new List<Task>();

for (int i = 0; i < _poolSize; i++)
{
_workerTasks.Add(Task.Run(() => ProcessRequestsAsync(i)));
}
}

public void AddToPool(IAsyncResult request)
{
lock (_pool)
{
_pool.Enqueue(request);
}
}

public void SetAction(Action<IAsyncResult> action)
{
_action = action;
}

private async Task ProcessRequestsAsync(int i)
{
int taskCount = 0;
bool inProg = false;
while (true)
{
IAsyncResult request;

lock (_pool)
{
if (_pool.Count == 0 || inProg)
{
continue; // No requests to process, wait for new ones
}

request = _pool.Dequeue();
inProg = true;
}
Console.WriteLine($"Pool[{i}] took {taskCount}nd task.");

// Perform the action on the request
_action(request);

inProg = false;
taskCount++;
}
}
}
public class RequestPool
{
private int _poolSize;
private Queue<IAsyncResult> _pool;
private Action<IAsyncResult> _action;
private List<Task> _workerTasks;

public RequestPool(int poolSize)
{
_poolSize = poolSize;
_pool = new Queue<IAsyncResult>();
_action = (_) => {};
_workerTasks = new List<Task>();

for (int i = 0; i < _poolSize; i++)
{
_workerTasks.Add(Task.Run(() => ProcessRequestsAsync(i)));
}
}

public void AddToPool(IAsyncResult request)
{
lock (_pool)
{
_pool.Enqueue(request);
}
}

public void SetAction(Action<IAsyncResult> action)
{
_action = action;
}

private async Task ProcessRequestsAsync(int i)
{
int taskCount = 0;
bool inProg = false;
while (true)
{
IAsyncResult request;

lock (_pool)
{
if (_pool.Count == 0 || inProg)
{
continue; // No requests to process, wait for new ones
}

request = _pool.Dequeue();
inProg = true;
}
Console.WriteLine($"Pool[{i}] took {taskCount}nd task.");

// Perform the action on the request
_action(request);

inProg = false;
taskCount++;
}
}
}
and it's gets so much allocate also after one time, can't handle requests.
Like able to see in photos, It tooks 40s which just totally 23mb. :/
Groophy
GroophyOP10mo ago
Oh 900mb ram usage
No description
Groophy
GroophyOP10mo ago
It's absolutely memory leak I'm not sure if ProcessRequestsAsync really works correctly, I haven't used lock before, I came across an example on the internet and it solved it for a short time, but I may have used it wrong or I don't think it really works async.
Want results from more Discord servers?
Add your server