Map的内存问题 [英] Memory issues with Map

查看:85
本文介绍了Map的内存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个程序,将一个包含3列的文件加载到

地图中。以下是声明:


struct strE

{

char iEID [22 + 1];

char acEN [6 + 1];

int iDed;

};


map< string,struct strE mEro ;


struct strE eStr = {0,};

if(0< vDataColumn.size())

eStr .iDzReceived =

atol(vDataColumn.at(0).c_str());

// vDataColumn包含来自文件的列|分隔符。

我们将行拆分为列并将其存储在vDataColumn中


mEro.insert(std :: pair< std :: string,

struct strERO>(key,eStr)

);


我看到6MB文件的常驻内存约为12 MB 。它好像
看起来像Map中存在内存泄漏或限制。我是

不确定Map使用什么样的内存管理。可能有人

在处理地图时会有什么最好的处理方法和消费税


I have written a program that loads a file having 3 columns into a
map. Here is the declaration:

struct strE
{
char iEID[22+1];
char acEN[6+1];
int iDed;
};

map<string, struct strE mEro;

struct strE eStr = {0,};
if ( 0 < vDataColumn.size() )
eStr.iDzReceived =
atol(vDataColumn.at(0).c_str());
//vDataColumn has columns from file which is "|" delimiter separated.
We split the line into columns and store it in vDataColumn

mEro.insert(std::pair<std::string,
struct strERO>(key, eStr)
);

I am seeing that for 6MB file the resident memory is around 12 MB. It
looks like either there is a memory leak or limitation in Map. I am
not sure what kind of memory mangement is used by Map. Could somebody
shed some light as what would be the best approach and dos and donts
when dealing with Maps

推荐答案

mo**********@gmail.com 写道:

我编写了一个程序,将一个包含3列的文件加载到

地图中。以下是声明:


struct strE

{

char iEID [22 + 1];

char acEN [6 + 1];

int iDed;

};


map< string,struct strE mEro ;
I have written a program that loads a file having 3 columns into a
map. Here is the declaration:

struct strE
{
char iEID[22+1];
char acEN[6+1];
int iDed;
};

map<string, struct strE mEro;



您可以在这里省略关键字''struct''。这不是必需的。 ''strE''

是* type *的名称(就像''string'')。我们不在C任何

更多。

You may omit the keyword ''struct'' here. It''s not needed. ''strE''
is the name of the *type* (just like ''string''). We''re not in C any
more.


>

struct strE eStr = {0 ,};
>
struct strE eStr = {0,};



在这里,你可以省略''struct''。

Same here, you may omit ''struct ''.


if(0< vDataColumn .size())

eStr.iDzReceived =
if ( 0 < vDataColumn.size() )
eStr.iDzReceived =



''strE''中没有成员''iDzReceived''。 br />

There is no member ''iDzReceived'' in ''strE''.


atol(vDataColumn.at(0).c_str());

// vDataColumn包含来自文件的列| ;分隔符。

我们将行拆分为列并将其存储在vDataColumn中


mEro.insert(std :: pair< std :: string,

struct strERO>(key,eStr)
atol(vDataColumn.at(0).c_str());
//vDataColumn has columns from file which is "|" delimiter separated.
We split the line into columns and store it in vDataColumn

mEro.insert(std::pair<std::string,
struct strERO>(key, eStr)



什么是''strERO''?

What''s ''strERO''?


);


我看到6MB文件的常驻内存约为12 MB。它好像
看起来像Map中存在内存泄漏或限制。
);

I am seeing that for 6MB file the resident memory is around 12 MB. It
looks like either there is a memory leak or limitation in Map.



内存泄漏可以通过使用特定用途的工具来发现。 std :: map中的限制?看起来更像是

" overhead"而不是限制对我来说......

Memory leak can be discovered by using the tools that exist for that
particular purpose. Limitation in std::map? Looks a lot more like
"overhead" than "limitation" to me...


我是

不确定Map使用了什么样的内存管理。可能有人

在处理地图时会有一些亮点,因为这是最好的方法和消费税吗?
I am
not sure what kind of memory mangement is used by Map. Could somebody
shed some light as what would be the best approach and dos and donts
when dealing with Maps



请找Josuttis撰写的The C ++ Standard Library。它b / b
包含了关于标准

容器的内存分配的大量信息,这个帖子的边距太窄而无法持有

它。 />

V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

Please find a copy of ''The C++ Standard Library'' by Josuttis. It
contains so much information on memory allocation for standard
containers that the margins of this posting are too narrow to hold
it.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


mo ********** @ gmail.com 写道:

我写了一个程序,将具有3列的文件加载到

地图中。以下是声明:


struct strE

{

char iEID [22 + 1];

char acEN [6 + 1];

int iDed;

};


map< string,struct strE mEro ;


struct strE eStr = {0,};

if(0< vDataColumn.size())

eStr .iDzReceived =

atol(vDataColumn.at(0).c_str());

// vDataColumn包含来自文件的列|分隔符。

我们将行拆分为列并将其存储在vDataColumn中


mEro.insert(std :: pair< std :: string,

struct strERO>(key,eStr)

);


我看到6MB文件的常驻内存约为12 MB 。它好像
看起来像Map中存在内存泄漏或限制。我是

不确定Map使用什么样的内存管理。可能有人

在处理地图时会有一些亮点,因为这是最好的方法和消费税吗?
I have written a program that loads a file having 3 columns into a
map. Here is the declaration:

struct strE
{
char iEID[22+1];
char acEN[6+1];
int iDed;
};

map<string, struct strE mEro;

struct strE eStr = {0,};
if ( 0 < vDataColumn.size() )
eStr.iDzReceived =
atol(vDataColumn.at(0).c_str());
//vDataColumn has columns from file which is "|" delimiter separated.
We split the line into columns and store it in vDataColumn

mEro.insert(std::pair<std::string,
struct strERO>(key, eStr)
);

I am seeing that for 6MB file the resident memory is around 12 MB. It
looks like either there is a memory leak or limitation in Map. I am
not sure what kind of memory mangement is used by Map. Could somebody
shed some light as what would be the best approach and dos and donts
when dealing with Maps



除了Victor在另一篇文章中指出的不一致之处,

还有一些可能有用的东西。


有多少条目6MB文件。


你说你的文件有三列。这些是什么?我问,因为

你的struct strE(或strERO?)有三个成员变量和

的键,地图每个条目会产生四个值。因此,如果您的数据每个条目有三个

值,并且您每个条目存储(密钥和其他三个)

是否会导致大小变化?


数据文件中的三个值是什么类型的?对于每个

''字符串''类型值,最大,最小和平均尺寸是多少?


你有没有可能'' d最好用

std :: string而不是char数组重写你的结构?或者如果内存使用是一个很大的问题,或者写一些非常好的
轻量级字符串类?


另外,你系统上的sizeof(int)是多少?比特是一个字母?


我认为鉴于你目前提供的信息,很难给b $ b给出一个有用的答案。 br />

LR

Aside from the inconstancies that Victor pointed out in another post,
there are a few things that might be useful to know.

How many entries in the 6MB file.

You say that your file has three columns. What are they? I ask because
your struct strE (or strERO?) has three member variables and the key of
the map would make four values per entry. So if your data has three
values per entry and you''re storing per entry (key and three others)
would that account for the change in size?

What types are the three values in the data file? And for each of the
''string'' type values what is the largest, smallest, and average size?

Is it possible that you''d be better off rewriting your struct with
std::string instead of char arrays? Or perhaps writing some very
lightweight string class if memory usage is a big concern?

Also, what is sizeof(int) on your system and how many bits is a char?

I think given the information you''ve provided so far, it''s difficult to
give a useful answer.

LR


LR写道:
LR wrote:
mo ********** @ gmail.com 写道:

>我编写了一个程序,将一个包含3列的文件加载到
地图中。这是声明:

struct strE
{i + ID [22 + 1];
char acEN [6 + 1];
int iDed ;
};

map< string,struct strE mEro;
[..]
>I have written a program that loads a file having 3 columns into a
map. Here is the declaration:

struct strE
{
char iEID[22+1];
char acEN[6+1];
int iDed;
};

map<string, struct strE mEro;
[..]



是否有可能你最好用

std :: string而不是char数组重写你的结构?或者如果内存使用是一个很大的问题,或者写一些非常好的
轻量级字符串类?


Is it possible that you''d be better off rewriting your struct with
std::string instead of char arrays? Or perhaps writing some very
lightweight string class if memory usage is a big concern?



考虑到数组只有23个字节和7个字节长,它看起来好像使用''std :: string''(它通常至少包含
a指针和一个size_t,使其成为8个字节,加上它在堆上某处需要堆开销的地方分配它的b / b缓冲区可能不是

有益(除了方便的观点)。轻量级

字符串类也占用内存...

Considering that the arrays are only 23 bytes and 7 bytes long, it
seems that using ''std::string'' (which usually contains at least
a pointer and a size_t, making it 8 bytes, plus it allocates its
buffer somewhere on the heap requiring heap overhead) might not be
beneficial (aside from the convenience point of view). Lightweight
string classes also take memory...


此外,系统上的sizeof(int)是多少,以及炭?
Also, what is sizeof(int) on your system and how many bits is a char?



它们在文件中是否相同?

Wouldn''t they be the same in the file?


我认为鉴于你的信息到目前为止,提供一个有用的答案是很困难的


I think given the information you''ve provided so far, it''s difficult
to give a useful answer.



std :: map带来一些开销。对于小的元素,

的开销会比使用大的元素更强大。只需要存储数据就可以支付搜索速度和便利性,只需要存储数据并不是那么糟糕,如果你这么多,请问我。


V

-

请在通过电子邮件回复时删除资金''A'

我请不要回复最热门的回复,请不要问

std::map carries some overhead. With elements that are small, the
overhead is going to be felt stronger than with elements that are
large. Paying for search speed and convenience with as much of the
memory as required to store just the data is not that awful, if you
ask me.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于Map的内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆