RAM 使用率高,多次调用同一端点

发布于 2025-01-17 06:25:12 字数 1489 浏览 3 评论 0原文

我制作了一个控制器只是为了测试 .net core 服务器的稳定性和速度。当使用 n = 10 调用此端点时,RAM 使用量会上升到 500mb,我不介意,但多次调用同一端点时,RAM 使用量会上升到 3Gb。这是正常的吗?我做错了什么吗?我可以降低这个使用量吗?

    [ApiController]
    [Route("[controller]")]
    public class DebugController : Controller
    {       
        [HttpGet]
        public  IActionResult Index(int n)
        {
          
                long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
                
                var T = new DataSet();
                if (n <= 0) { n = 1; }
                else if (n > 10) { n = 10; }

                for (int i = 0; i < n; i++)
                {
                    var table = new DataTable("table" + i);
                    table.Columns.Add("test");
                    for (int j = 0; j < 500_000; j++)
                    {
                        table.Rows.Add("");
                    }
                    T.Tables.Add(table);
                }


                return Ok(new test(now, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), T));
            
        }
             

        public struct test:IDisposable
        {
            public test(long i, long f, DataSet d)
            {
                ini = i;
                fi = f;
                data = d;
            }

            public long ini;
            public long fi;
            public DataSet data;

            public void Dispose()
            {
                data.Dispose();
            }
        }
    }

I made a controller just to test the stability and velocity of my .net core server. When calling this endpoint with n = 10, the RAM usage goes up to 500mb, which I don't mind, but calling the same endpoint multiple times it ramps up to 3Gb. It is normal? I'm doing something wrong? Can I lower this usage?

    [ApiController]
    [Route("[controller]")]
    public class DebugController : Controller
    {       
        [HttpGet]
        public  IActionResult Index(int n)
        {
          
                long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
                
                var T = new DataSet();
                if (n <= 0) { n = 1; }
                else if (n > 10) { n = 10; }

                for (int i = 0; i < n; i++)
                {
                    var table = new DataTable("table" + i);
                    table.Columns.Add("test");
                    for (int j = 0; j < 500_000; j++)
                    {
                        table.Rows.Add("");
                    }
                    T.Tables.Add(table);
                }


                return Ok(new test(now, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), T));
            
        }
             

        public struct test:IDisposable
        {
            public test(long i, long f, DataSet d)
            {
                ini = i;
                fi = f;
                data = d;
            }

            public long ini;
            public long fi;
            public DataSet data;

            public void Dispose()
            {
                data.Dispose();
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小忆控 2025-01-24 06:25:12

您已呼叫 n 次。这意味着每次调用下面的部分时,都会向表中添加一行。这会导致将您的结构(表数据)保存在内存中。

for (int j = 0; j < 500_000; j++)
{
    table.Rows.Add("");
}

多次迭代该部分会增加行数,因此您需要更多的内存存储空间。由于 C# 是一种托管语言,垃圾收集器应在一段时间后自动清除内存。
在我看来,内存使用量如此之高是正常的。

还有其他方法可以处理这种情况,例如延迟加载。这可能会减少 RAM 使用量。但是,我无法告诉您使用 IEnumerable 等其他数据结构是否适合您的上下文。
使用 IEnumerable 而不调用 .ToList().ToArray() 在需要数据之前不会消耗内存。

You are calling n times. This means each time you call the part below you add a row to your table. This results in saving your structure (your table data) in memory.

for (int j = 0; j < 500_000; j++)
{
    table.Rows.Add("");
}

Iterating that part multiple time increase the amount of rows and therefore you need more storage in memory. Memory should be cleared up after some time by the Garbage Collector automaticlly, since C# is a managed language.
Seems to me to be normal to have such a high amount of RAM usage.

There a other ways to handle such situatuion like doing by lazy loading. This may reduce the RAM usage. However I can not tell you if using other data structs like IEnumerable<T> are fitting to your context.
Working with IEnumerable<T> without calling .ToList() or .ToArray() will not consume memory until the data is needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文