std:字符串内存使用 [英] std:string memory using
问题描述
大家好,
A有关于std :: string内存的问题使用
我想创建一个动态字符串矩阵:
// < span class =code-comment>例如:
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仍然太大 - 你怎么衡量?进程还以块的形式从操作系统中获取内存,因此进程内存大小不是该矩阵内存使用情况的细粒度指示器。
如何优化:如果内存使用很关键,但访问可能很慢(很少)并且如果数据是常量:
创建一个抽象整个矩阵的类,将内容存储在一个大字符数组中,每个字符串< b> 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 probablysize_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 allowdelete[]
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 thestring
s is15
).
这篇关于std:字符串内存使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!