码迷,mamicode.com
首页 > 编程语言 > 详细

由于线程停在某个无法进行垃圾回收的点(可能是因为已对代码进行了优化),因此无法计算表达式的值

时间:2015-04-28 22:14:04      阅读:1093      评论:0      收藏:0      [点我收藏+]

标签:

前段时间在调试代码的过程中,调试器无法跟踪到变量的值并报异常,AnisometryT Cannot evaluate expression because a thread is stopped at a point where garbage collection is impossible, possibly because the code is optimized.

如下图所示:

 

技术分享

 发现在以上代码的下面,有一个类型的构造方法含有很多个参数:

技术分享

后查找资料,发现在 .net 3.5 以及32位应用程序中,单个方法中所有参数所占用内存的总大小不能超过256个字节。

用一段简单的代码重现了以上现象:

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace ConsoleApplication1
{
    class TestClass
    {
        public void TestParams()
        {
            MyObject mo = new MyObject();
            Console.WriteLine(mo.ID);
            Bytes128 bytes128 = new Bytes128();            
          
            TestParams256(bytes128, bytes128);
        }

        private void TestParams256(Bytes128 bytes1281, Bytes128 bytes1282)
        {
            Console.WriteLine(bytes1281);
        }

        private void TestParams128(Bytes128 bytes128)
        {
            Console.WriteLine(bytes128);
        }
    }

    struct Bytes16
    {
        int word;
        int word2;
        int word3;
        int word4;
    }

    struct Bytes32
    {
        Bytes16 word16_1;
        Bytes16 word16_2;
    }

    struct Bytes64
    {
        Bytes32 word32_1;
        Bytes32 word32_2;
    }

    struct Bytes128
    {
        Bytes64 word64_1;
        Bytes64 word64_2;
    }
}
View Code
技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class MyObject
    {
        public int ID
        {
            get;
            set;
        }
    }
}
View Code

运行并在mo变量处断点:

技术分享

 

其中方法TestParams256的两个参数都为Bytes128,Bytes128为Struct值类型,传递的时候会采用值传递,两个参数共占用256个字节,导致以上现象出现。

 

解决方法:

1、编译为x64位程序

2、采用.net 4.0 或更高版本的Framework

3、将Struct值类型定义为Class引用类型

4、将方法拆分为几个方法,确保每个方法所传递参数占用内存大小小于256个字节。

 

另外,以上异常只是影响调试,在程序运行过程中并不会有影响。

 

由于线程停在某个无法进行垃圾回收的点(可能是因为已对代码进行了优化),因此无法计算表达式的值

标签:

原文地址:http://www.cnblogs.com/Jason-han/p/4463922.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!