delphi – 字符串和高内存使用率

前端之家收集整理的这篇文章主要介绍了delphi – 字符串和高内存使用率前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我在XE4中运行此代码时,应用程序最终使用~800 MB.

为什么不接近100 MB?

使用Ansistring而不是字符串没有区别.

  1. const
  2. N = 10000000; // 10 million
  3. M = 10;
  4. var
  5. i,j: integer;
  6. s: string;
  7. X: array of string;
  8. begin
  9. setlength(X,N);
  10. for i:= 1 to N do
  11. begin
  12. s:= '';
  13. for j:= 1 to M do s:= s+chr(65+random(25));
  14. X[i-1]:= s;
  15. end;
  16. showmessage('pause');
  17. end;

解决方法

XE4中长度为10的字符串使用34字节的内存(请参阅 DocWiki).内容为20字节,#0终结符为2字节,管理数据为12字节.

每个数组条目都是指向那种内存的指针.因此,阵列中的那1000万个字符串最终使用380 MB(字符串为340,数组项为40).

猜你在找的Delphi相关文章