小编Ede*_*een的帖子

String VS Byte [],内存使用情况

我有一个使用大量字符串的应用程序.所以我有一些内存使用问题.我知道在这种情况下最好的解决方案之一是使用数据库,但我暂时不能使用它,所以我正在寻找其他解决方案.

在C#中,字符串存储在Utf16中,这意味着与Utf8(对于我的字符串的主要部分)相比,我丢失了一半的内存使用量.所以我决定使用utf8字符串的字节数组.但令我惊讶的是,这个解决方案在我的应用程序中占用了比简单字符串多两倍的内

所以我做了一些简单的测试,但我想知道专家的意见.

测试1:固定长度字符串分配

var stringArray = new string[10000];
var byteArray = new byte[10000][];
var Sb = new StringBuilder();
var utf8 = Encoding.UTF8;
var stringGen = new Random(561651);
for (int i = 0; i < 10000; i++) {
    for (int j = 0; j < 10000; j++) {
        Sb.Append((stringGen.Next(90)+32).ToString());
    }
    stringArray[i] = Sb.ToString();
    byteArray[i] = utf8.GetBytes(Sb.ToString());
    Sb.Clear();
}
GC.Collect();
GC.WaitForFullGCComplete(5000);
Run Code Online (Sandbox Code Playgroud)

内存使用情况

00007ffac200a510        1        80032 System.Byte[][]
00007ffac1fd02b8       56       152400 System.Object[]
000000bf7655fcf0      303      3933750      Free
00007ffac1fd5738    10004    224695091 System.Byte[]
00007ffac1fcfc40    10476    449178396 …
Run Code Online (Sandbox Code Playgroud)

c# memory string

12
推荐指数
1
解决办法
1161
查看次数

静态链接函数的调用每次都在Windows 8/10上崩溃,但不是7

问题 :

我已经构建了生成二进制文件的https://github.com/reorg/pg_repack项目.这个二进制文件与postgres 9.6可再发行组件链接(我使用EntrepriseDb提供的那些).一切正常在Windows 7上工作.我在构建期间或运行时没有问题.但是在Windows 8或10上,应用程序始终会崩溃并显示以下序列.二进制文件是使用visual studio 2013在Windows 7上生成的(在Windows 7上我尝试过使用在Windows 10上生成的版本,但它不会改变任何内容),在x64系统上,对于x64应用程序,并且优化被禁用,它使用动态基础.为了确保我使用正确的二进制文件,我将所有可再发行的二进制文件复制到我的应用程序的文件夹中.

Windows 7(工作案例)的装配细节:

在main之后很少行,应用程序调用函数set_pglocale_pgservice

set_pglocale_pgservice(argv[0], "pgscripts");
00007FF6E4B39C85  mov         eax,8  
00007FF6E4B39C8A  imul        rax,rax,0  
00007FF6E4B39C8E  lea         rdx,[default_options+120h (07FF6E4B43B10h)]  
00007FF6E4B39C95  mov         rcx,qword ptr [argv]  
00007FF6E4B39C9A  mov         rcx,qword ptr [rcx+rax]  
00007FF6E4B39C9E  call        qword ptr [__imp_set_pglocale_pgservice (07FF6E4B40520h)] 
Run Code Online (Sandbox Code Playgroud)

内存在07FF6E4B40520h

0x00007FF6E4B40520  00000001403e1da0   .>@....
0x00007FF6E4B40528  0000000000000000  ........
0x00007FF6E4B40530  0000000000000000  ........
0x00007FF6E4B40538  00007ff6e4b35348  HS.äö...
Run Code Online (Sandbox Code Playgroud)

(注意:从0x7FF6E4B40000到0x00007FF6E4B40560内存包含函数地址,而mapfile说:0002:00000520 __imp_set_pglocale_pgservice postgres:postgres.exe [postgres redistributable,动态链接])

然后调用qword ptr [__imp_set_pglocale_pgservice(07FF6E4B40520h)]

00000001403E1DA0  mov         qword ptr [rsp+18h],rbx  
00000001403E1DA5  push        rdi  
00000001403E1DA6  sub         rsp,0C40h  
00000001403E1DAD  mov         rax,qword ptr …
Run Code Online (Sandbox Code Playgroud)

windows assembly visual-studio-2013 postgres-9.6

6
推荐指数
1
解决办法
259
查看次数