C#:模拟内存泄漏 [英] c# : simulate memory leaks

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

问题描述

我想用c#编写以下代码. a)模拟内存泄漏的小型控制台应用程序. b)小型控制台应用程序,它将调用上述应用程序并立即释放它,以模拟管理内存泄漏问题.

I would like to write the following code in c#. a) small console application that simulates memory leak. b) small console application that would invoke the above application and release it right away simulating managing memory leak problem..

换句话说,(b)应用程序将连续调用并释放应用程序(a),以模拟如何包含叛逆"内存泄漏应用程序,而没有解决根本原因,即应用程序(a).

In other words the (b) application would continuously call and release application (a) to simulate how the "rebellious" memory leak application is being contained with out addressing the root cause which is application (a).

应用(a)和(b)的一些示例代码将非常有帮助.

Some sample code for application (a) and (b) would be very helpful.

谢谢

推荐答案

泄漏的应用程序可能看起来像:

The leaking application might look like:

public static void Main()
{
  var list = new List<byte[]>();
  while (true)
  {
    list.Add(new byte[1024]); // Change the size here.
    Thread.Sleep(100); // Change the wait time here.
  }
}

调用应用程序可能如下所示:

And the calling application might look like:

public static void Main()
{
  Process process = Process.Start("leaker.exe");
  process.Kill();
}

看看属性上的属性.有几个返回该进程消耗多少内存.您也许可以使用其中之一来有条件地终止进程.

Take a look at the properties on the Process class. There are several that return how much memory the process is consuming. You may be able to use one of them to conditionally kill the process.

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

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