std:字符串内存使用

编程入门 行业动态 更新时间:2024-10-16 22:20:32
本文介绍了std:字符串内存使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, A有关于std :: string内存的问题使用 我想创建一个动态字符串矩阵:

// 例如: int X_range = 100000 ; // 数十万行 int Y_range = 10 ; // ten coloumn string ** matrix; matrix = new * string [X_range]; for ( int x = 0 ; x< X_range; x ++) { matrix = new string [Y_range]; 矩阵[x] [y] = ABCD }

此代码工作正常。我可以从矩阵访问相关的字符串,我使用它们。 我的问题是这个矩阵使用~60MB的内存。 我认为它应该只使用100000 * 10 * 4字节= ~4MB的内存。 我检查了每个字符串的capacity()和size() 并且两个函数都返回4. 所以,我不明白为什么为~4MB文本分配~60MB内存。 你有什么想法吗?:) 提前致谢!!

解决方案

首先:你如何测量这60MB。 第二:你的尺寸计算错误。 你需要计算什么: 1)为每个X分配一个100000 指针的数组 2),你分配一个10 * sizeof(string)的数组 3)每个字符串由一个5个字符的字符数组初始化(四个字母加上终止零字符)。 字符串的实现确实不保证任何书籍大小(指向动态内容,大小......)。因此,字符串至少是一个指针,长度可能是 size_t 类型。除此之外,字符串实现可能已经选择仅在块中分配,而不是仅仅根据需要分配。 64位系统上的指针和size_t是 - 64位大小;-) 因此,计算空字符串的纯骨架(无内容,无容量)将导致 1)100000 * siteof(指针)= 800000 2)一个10个空字符串数组至少10 * sizeof(字符串)=至少10 *(8 + 8)= 160(或更多) 3)字符串的每个内容至少为5个字节,可能更多是由于内存管理考虑因素)= 5(或更多) 总结:最小预期内存大小= 100000 *(8 +(10 *((8 + 8)+(5 * 1))))~21MB 现在,一些操作系统可能会决定数组,它们会分配更多的字节,通常是之前返回的地址。这主要是为了允许 delete [] 来获取要删除的数据的大小。 每个动态数组将为这个内存管理书保存数组加8个字节: - 1 * X-array - 100000 * Y数组 - 1000000 *动态字符串内容 = 1100001 * 8字节~8MB 我计算的内存使用量为30MB或更多,具体取决于字符串储备的那个容量。 我猜你60MB仍然太大 - 你怎么衡量?进程还以块的形式从操作系统中获取内存,因此进程内存大小不是该矩阵内存使用情况的细粒度指示器。 如何优化:如果内存使用很关键,但访问可能很慢(很少)并且如果数据是常量: 创建一个抽象整个矩阵的类,将内容存储在一个大字符数组中,每个字符串 literal (例如ABC)以零字符终止(即'\ 0')并通过两个索引(x,y)访问内容,x搜索相应的x * 10 + y'\0'-字符,在最后一个字符后,搜索到的字符串跨越到下一个'\0' 。 干杯 Andi

我认为额外的内存使用量是由于初始容量字符串(取决于实现,请参见此页面:std :: string length and capacity [ ^ ])。 请注意,发布的代码有错误,应该是

int X_range = 100000 ; // 数十万行 int Y_range = 10 ; // ten coloumn string ** matrix; matrix = new string * [X_range]; for ( int x = 0 ; x< X_range; x ++) { matrix [x] = new string [Y_range]; for ( int y = 0 ; y< Y_range; y ++) matrix [x] [y] = ABCD; }

你是如何检查矩阵内存使用情况的? 在我的系统上(Win 8) 64位,VS 2012)字符串的容量是 15 )。

Hi All, A have a problem about std::string memory using I would like to create a dynamic string matrix:

//For example: int X_range=100000; // hundreds of tousands row int Y_range=10;//ten coloumn string **matrix; matrix = new *string[X_range]; for (int x=0;x<X_range;x++) { matrix = new string[Y_range]; matrix[x][y]="ABCD" }

This code is working fine.I can acces related strings from matrix and I using them. My problem is that this matrix using ~60MB of memory. I think that it should use only 100000*10*4 bytes = ~4MB of memory. I've checked every string's capacity() and size() and both function returned with 4. So, I don't understand why allocated ~60MB memory for ~4MB text. Have you got any idea?:) Thanks in advance!!

解决方案

First: how do you measure these 60MB. Second: your size calculation is wrong. What you have to calculate: 1) you allocate an array of 100000 pointers 2) for each X, you allocate an array of 10 * sizeof(string) 3) each string is initialized by a character array of 5 characters (four letters plus terminating zero character). The implementation of string does not guarantee any size for book keeping (pointer to dynamic content, size, ...). So, a string is at least a pointer and an length of probably size_t type. In addition to that, the string implementation may have choosen to allocate only in chunks and not exactly only as much as needed. A pointer and size_t on a 64 bit system are - 64 bits in size ;-) So, calculating the pure skeleton for empty strings (no content, no capacity) would result in 1) 100000 * siteof(pointer) = 800000 2) one array of 10 empty string is at least 10 * sizeof(string) = at least 10 * (8 + 8) = 160 (or more) 3) each content of the string is at least 5 bytes, probably more due to memory management considerations) = 5 (or more) Summing up: the minimul expected memory size = 100000 * (8 + (10 * ((8 + 8) + (5 * 1)))) ~ 21MB Now, some operating systems may decide for arrays, that they allocate some more bytes, usually before the returned address. This is mainly to allow delete[] to get the size of the data to delete. Each dynamic array will add up say 8 bytes for this memory management book keeping of arrays: - 1 * X-array - 100000 * Y array - 1000000 * dynamic string content = 1100001 * 8 bytes ~ 8MB My calculation comes to some memory usage of 30MB or more, depending of that capacity the string reserves. I guess you 60MB is still too large - how you measure that? Processes also acquire memory from the operarting system in chunks, so the process memory size is not a fine grained enough indicator for memory usage of this matrix. How to optimize: If memory usage is critical but accessing may be slow (and seldom) and if the data is constant: make a class that abstracts the whole matrix, store the content in one large character array with each string literal (e.g. "ABC") terminated with a zero-character (i.e. '\0') and access the content by two index (x, y), x searches the respective x * 10 + y '\0'-characters, after that last character, the searched string spans to the next '\0'. Cheers Andi

I think the additional memory usage is due to the initial capacity of the string (it is implementation dependent, see, for instance this page: "std::string length and capacity"[^]). Please note, posted code has mistakes, it should be

int X_range=100000; // hundreds of tousands row int Y_range=10;//ten coloumn string **matrix; matrix = new string * [X_range]; for (int x=0;x<X_range;x++) { matrix[x]= new string[Y_range]; for (int y=0; y<Y_range; y++) matrix[x][y]="ABCD"; }

How did you check matrix memory usage? On my system (Win 8 64 bits, with VS 2012) the capacity of the strings is 15).

更多推荐

std:字符串内存使用

本文发布于:2023-11-29 21:27:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647668.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   内存   std

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!