编译表达式树是否泄漏?

时间:2017-03-27 08:17:34

标签: c# .net memory-leaks expression expression-trees

据我了解,JIT-ed代码在程序运行时永远不会从内存中释放出来。这是否意味着在表达式树上反复调用groovy-2.4.9.jar会泄漏内存?

这意味着只能在静态构造函数中编译表达式树,或者以其他方式缓存它们,这可能不那么简单。正确?

2 个答案:

答案 0 :(得分:9)

他们可能是GCed ... LambdaExpression.Compile()使用LambdaCompiler.Compile(LambdaExpression, DebugInfoGenerator)类,through one of the LambdaCompiler constructors使用来自MSDN的DynamicMethod

  

定义并表示可以编译,执行和丢弃的动态方法。 可以使用废弃的方法进行垃圾回收。

答案 1 :(得分:5)

我尝试通过在后台连续生成表达式树然后收集所有垃圾并监控GUI线程中的已用空间来测试它。

几个小时之后,内存使用量似乎会稳定在655000字节左右。因此,我说使用表达树可以安全地使用它。

Expression tree memory usage

如果有人想要我的hacky测试代码,这里是:

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading;
using System.Windows.Forms;

namespace Experiments
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // Ensuring that always the same amount of memory is used for point storage.
            bytesUsed = new Queue<long>(1000);
            var points = chart1.Series[0].Points;
            for (var i = 0; i < 1000; ++i)
            {
                bytesUsed.Enqueue(0);
                points.Add(0);
            }


            thread = new Thread(ThreadMethod);
            thread.Start();
            timer1.Interval = 10000;
            timer1.Enabled = true;
            timer1_Tick(null, null);
        }

        private readonly Queue<long> bytesUsed;
        private void timer1_Tick(object sender, EventArgs e)
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            bytesUsed.Dequeue();
            bytesUsed.Enqueue(GC.GetTotalMemory(false));

            var points = chart1.Series[0].Points;
            points.Clear();
            foreach (var value in bytesUsed)
                points.Add(value);
        }

        private Thread thread;
        private volatile bool stopping;
        private void ThreadMethod()
        {
            var random = new Random();

            while (!stopping)
            {
                var constant = Expression.Constant(random.Next(), typeof(int));
                var param = Expression.Parameter(typeof(int));

                var mul = Expression.Multiply(param, constant);
                var add = Expression.Multiply(mul, param);
                var sub = Expression.Subtract(add, constant);

                var lambda = Expression.Lambda<Func<int, int>>(sub, param);
                var compiled = lambda.Compile();
            }
        }

        protected override void Dispose(bool disposing)
        {
            stopping = true;
            if (thread != null && disposing)
                thread.Join();

            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}