C#-Postgresql-随时间推移的内存泄漏 [英] C# - postgresql - memory leak over time

查看:141
本文介绍了C#-Postgresql-随时间推移的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:随着时间的流逝,内存泄漏(累积),最终达到99%的容量.

Problem: The memory leaks (accumulates) over time, and reaches 99% capacity eventually.

我有以下C#代码,它们使用while循环不断将数据推入PostgreSQL DB中.我真的很挣扎,因为我不是C#程序员.我的主要语言是Python.我一直在尝试查找C#引用来解决我的问题,但是由于失败,我根本不了解很多语法. C#代码是由我公司中的其他人编写的,但现在不可用.

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.

这是代码:

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");

该代码从一开始就成功建立了连接,并且在整个过程中仅使用一个连接.它将数据正确插入到DB.但是在内存容量达到99%之后,其插入效果不是很好.我能想到的问题根源是这段代码不断创建新对象,但是在完成一次迭代后并没有清除该对象.谁能告诉我问题的根源在哪里?提供可能的解决方案?

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?

++,请理解我不是C#程序员...我对内存处理的概念不太熟悉.但我会尽力了解

++ 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

推荐答案

您可以尝试以下方法.请注意,命令和参数的实例化发生在循环之外,而不是在每次迭代时发生.

Here is something you can try. Notice the instantiation of the command and parameters happens outside of the loop, not on every iteration.

我正在回收参数.结果,我使用的是Add()而不是AddWithValue(),并且您必须填写第二个参数的数据库类型,并考虑适当地使用precision和scale参数. 仅当两个命令使用相同的参数类型时,此方法才有效.您可能会考虑创建两个命令,每个查询一个.

I am recycling the parameters. As a result I am using Add(), not AddWithValue() and you must fill in the database type for the second parameter and consider using precision and scale parameters too as appropriate. This will only work if the two commands use the same parameter types. You might consider creating two commands, one for each query.

知道以@开头的变量名使我成为c#开发人员感到畏缩....

Know that variable names beginning with @ makes me cringe as a c# developer....

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;

    @Info.Trace("Pushed Data: PostGre A " + ctr.ToString());

    using (var cmd = new Npgsql.NpgsqlCommand())
    {
        cmd.Connection = conn;

        var par_1 = cmd.Parameters.Add("@p1", /*< appropriate datatype here >*/);
        var par_2 = cmd.Parameters.Add("@p2", /*< appropriate datatype here >*/);

        while(@tag.TerminateTimeScaleLoop == 100)
        {
            cmd.CommandText = "INSERT INTO TORQX VALUES (@p1,@p2)";
            par_1.Value = System.DateTime.Now.ToUniversalTime());
            par_2.Value = @Tag.RigData.Time.TORQX;

            cmd.ExecuteNonQuery();

            cmd.CommandText = "INSERT INTO BLKPOS VALUES (@p1,@p2)";

            par_1.Value = System.DateTime.Now.ToUniversalTime());
            par_2.Value = @Tag.RigData.Time.BLKPOS;

            cmd.ExecuteNonQuery();

            ctr = ctr + 1;
        }
    }
}
@Info.Trace("Pushed Data: PostGre A Terminated");

这篇关于C#-Postgresql-随时间推移的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆