Problem: The memory leaks (accumulates) over time, and reaches 99% capacity eventually.
I have the following C#
code that constantly pushes data into PostgreSQL DB using while loop. I'm really struggling because I'm not a C#
programmer. My main language is Python
. I've been trying to look up C#
references to solve my issue, but failed cuz I simply don't understand lots of syntax. The C#
code is written by someone else in my company, but he's not available now.
Here is the code:
var connString = "Host=x.x.x.x;Port=5432;Username=postgres;Password=password;Database=database";
using (var conn = new Npgsql.NpgsqlConnection(connString)){
conn.Open();
int ctr = 0;
// Insert some data
while(@tag.TerminateTimeScaleLoop == 100)
{
@Info.Trace("Pushed Data: PostGre A " + ctr.ToString());
using (var cmd = new Npgsql.NpgsqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO TORQX VALUES (@r,@p)";
cmd.Parameters.AddWithValue("r", System.DateTime.Now.ToUniversalTime());
cmd.Parameters.AddWithValue("p", @Tag.RigData.Time.TORQX);
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
cmd.CommandText = "INSERT INTO BLKPOS VALUES (@s,@t)";
cmd.Parameters.AddWithValue("s", System.DateTime.Now.ToUniversalTime());
cmd.Parameters.AddWithValue("t", @Tag.RigData.Time.BLKPOS);
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
// @Info.Trace("Pushed Data: PostGre " + ctr.ToString());
}
ctr = ctr + 1;
}
@Info.Trace("Pushed Data: PostGre A Terminated");
The code successfully established a connection in the beginning, and uses only one connection the entire time. It correctly inserts data to DB. But after memory capacity reaches 99%, its not inserting very well. The source of issue I can think of is that this code is constantly creating new object, but does not clear that object after one iteration is done. Can anyone tell me where the source of problem is & provide possible solution to this?
++ please understand that I'm not a C# programmer... I'm not too familiar with the concept of memory handling. But I will try my best to understand