C# 中容易忽视的 Encoding.GetByteCount 内存问题

如果想在 C# 中判断字符是全角还是半角的,通常的办法是使用 Encoding.Default.GetByteCount 方法,使用它的时候却有很容易忽视的内存问题,具体表现为多次(数万次,不同电脑可能不同)调用 GetByteCount 方法时,会导致内存垃圾回收,那么意味着在这个过程中产生了大量的临时对象。

下面这段测试代码就是对总长为 6 万的 char 数组计算它的字节数,循环 10 次。其中测试一:一次取 1 个字符,每次循环调用 GetByteCount 60000 次;测试二:一次取 2 个字符,每次循环调用 30000 次;测试三:一次取 5 个字符,每次循环调用 12000 次;这样一直到测试六:一次取 60000 个字符,每次循环调用 1 次。其中用到的 CodeTimer 类是一个来自老赵的性能计数器。

char[] charArr = new char[60000];
for (int i = 0; i < 60000; i++)
{
	charArr[i] = (char)RandomExt.Next(char.MaxValue);
}
GC.Collect();
CodeTimer.Time("TestGetByteCount 1", 10, () =>
{
	for (int i = 0; i < 60000; i++)
	{
		Encoding.Default.GetByteCount(charArr, i, 1);
	}
});
CodeTimer.Time("TestGetByteCount 2", 10, () =>
{
	for (int i = 0; i < 60000 / 2; i++)
		Encoding.Default.GetByteCount(charArr, i * 2, 2);
});
CodeTimer.Time("TestGetByteCount 5", 10, () =>
{
	for (int i = 0; i < 60000 / 5; i++)
		Encoding.Default.GetByteCount(charArr, i * 5, 5);
});
CodeTimer.Time("TestGetByteCount 10", 10, () =>
{
	for (int i = 0; i < 60000 / 10; i++)
		Encoding.Default.GetByteCount(charArr, i * 10, 10);
});
CodeTimer.Time("TestGetByteCount 100", 10, () =>
{
	for (int i = 0; i < 60000 / 100; i++)
		Encoding.Default.GetByteCount(charArr, i * 100, 100);
});
CodeTimer.Time("TestGetByteCount 65536", 10, () =>
{
	Encoding.Default.GetByteCount(charArr, 0, 60000);
});

不用看测试结果也知道,效率肯定是前面的低,后面的高。但重点不是这个,下面是测试结果,注意看 Gen 0 这一项(表示 0 代垃圾回收次数)。

TestGetByteCount 1
  Time Elapsed:   52ms
  CPU Cycles:     113,265,292
  Gen 0:   8
  Gen 1:   0
  Gen 2:   0
TestGetByteCount 2
  Time Elapsed:   41ms
  CPU Cycles:     90,435,216
  Gen 0:   5
  Gen 1:   0
  Gen 2:   0
TestGetByteCount 5
  Time Elapsed:   35ms
  CPU Cycles:     77,586,978
  Gen 0:   2
  Gen 1:   0
  Gen 2:   0
TestGetByteCount 10
  Time Elapsed:   32ms
  CPU Cycles:     71,327,412
  Gen 0:   1
  Gen 1:   0
  Gen 2:   0
TestGetByteCount 100
  Time Elapsed:   32ms
  CPU Cycles:     65,847,702
  Gen 0:   0
  Gen 1:   0
  Gen 2:   0
TestGetByteCount 65536
  Time Elapsed:   34ms
  CPU Cycles:     72,340,460
  Gen 0:   0
  Gen 1:   0
  Gen 2:   0

单独把垃圾回收次数列出来,分别是 8,5,2,1,0,0,有没有感觉很神奇?明明没有创建任何临时对象,却导致了好几次的内存回收。用 VS 自带的性能分析器分析看看,得到下面的图:

图 1 分配最多内存的函数

好吧,现在知道全都是 System.Text.EncodingNLS.GetByteCount(char[], int32, int32) 的错了……但是这是系统自带的函数,还是要先尝试从自身找问题,再看看分配视图:

图 2 分配视图

看分配数遥遥领先的第一项:System.Text.InternalEncoderBestFitFallbackBuffer,好吧,原来就是 EncoderFallbackBuffer 的问题,它是提供一个允许回退处理程序在无法编码输入的字符时返回备用字符串到编码器的缓冲区。在调用 Encoding.GetByteCount 时,有可能会发生回退,因此编码器内部会创建一个缓冲区以处理回退问题。又由于在每次调用时都会创建新的缓冲区,用完即扔,因此就会导致上面的现象——大量的临时缓冲区被创建,又被回收,导致内存压力增大。

这种问题并不明显,需要有六七万次以上的调才行(在我的电脑上),但是有问题就要想办法去解决。

我这里提供一个简单的办法,就是调用 Encoding.Default.GetEncoder(),获取默认编码的编码器,然后调用这个编码器的 GetByteCount 方法,就可以完美解决。这里需要注意的是,Encoder 的 GetByteCount 方法比 Encoding 的方法多了一个参数 flush,表示时候要在计算后模拟编码器内部状态的清除过程,需要注意。

更改后的代码为:

char[] charArr = new char[60000];
for (int i = 0; i < 60000; i++)
{
	charArr[i] = (char)RandomExt.Next(char.MaxValue);
}
Encoder encoder = Encoding.Default.GetEncoder();
CodeTimer.Time("TestGetByteCount 1", 10, () =>
{
	for (int i = 0; i < 60000; i++)
	{
		encoder.GetByteCount(charArr, i, 1, true);
	}
});
CodeTimer.Time("TestGetByteCount 2", 10, () =>
{
	for (int i = 0; i < 60000 / 2; i++)
		encoder.GetByteCount(charArr, i * 2, 2, true);
});
CodeTimer.Time("TestGetByteCount 5", 10, () =>
{
	for (int i = 0; i < 60000 / 5; i++)
		encoder.GetByteCount(charArr, i * 5, 5, true);
});
CodeTimer.Time("TestGetByteCount 10", 10, () =>
{
	for (int i = 0; i < 60000 / 10; i++)
		encoder.GetByteCount(charArr, i * 10, 10, true);
});
CodeTimer.Time("TestGetByteCount 100", 10, () =>
{
	for (int i = 0; i < 60000 / 100; i++)
		encoder.GetByteCount(charArr, i * 100, 100, true);
});
CodeTimer.Time("TestGetByteCount 65536", 10, () =>
{
	encoder.GetByteCount(charArr, 0, 60000, true);
});

测试结果为:

TestGetByteCount 1
  Time Elapsed:   45ms
  CPU Cycles:     98,742,656
  Gen 0:   0
  Gen 1:   0
  Gen 2:   0
TestGetByteCount 2
  Time Elapsed:   38ms
  CPU Cycles:     83,395,672
  Gen 0:   0
  Gen 1:   0
  Gen 2:   0
TestGetByteCount 5
  Time Elapsed:   34ms
  CPU Cycles:     74,867,809
  Gen 0:   0
  Gen 1:   0
  Gen 2:   0
TestGetByteCount 10
  Time Elapsed:   31ms
  CPU Cycles:     70,190,804
  Gen 0:   0
  Gen 1:   0
  Gen 2:   0
TestGetByteCount 100
  Time Elapsed:   31ms
  CPU Cycles:     68,862,872
  Gen 0:   0
  Gen 1:   0
  Gen 2:   0
TestGetByteCount 65536
  Time Elapsed:   30ms
  CPU Cycles:     65,830,539
  Gen 0:   0
  Gen 1:   0
  Gen 2:   0

可以很明显的看到,内存问题完全解决了,而且速度也有略微提升。如果需要多次调用 GetByteCount,还是调用 Encoder 的方法更好。

posted @ 2013-03-26 21:24  CYJB  阅读(4214)  评论(1编辑  收藏  举报
Fork me on GitHub