简体   繁体   English

std :: istream_iterator,内存消耗

[英]std::istream_iterator, memory consumption

I try to iterate through a text file using iterators: 我尝试使用迭代器遍历文本文件:

1) std::istream_iterator<MyData> itRecordStreamBegin(fileStream);
2) std::istream_iterator<MyData> itRecordStreamEnd; 

After the first line it reads some data from the file (it uses operator >> for MyData type). 第一行之后,它从文件中读取一些数据(它对MyData类型使用运算符>>)。 As expected. 如预期的那样。

Unfortunately after that memory consumption rises and it seems like istream_iterator tried to load whole file in memory. 不幸的是,在那之后,内存消耗增加了,似乎istream_iterator试图将整个文件加载到内存中。 Several seconds later (when there are out_of_memory exceptions thrown) debugger gets into the second line. 几秒钟后(当抛出out_of_memory异常时)调试器进入第二行。 But file reading failed and I cannot read anything more. 但是文件读取失败,我无法读取更多内容。

Questions : 问题

Is this correct behaviour for istream_iterator (to load file into memory)? istream_iterator是否正确(将文件加载到内存中)?

I do not observe this for smaller files (like 20MB). 对于较小的文件(例如20MB),我没有观察到。

Maybe for such large file I need to use common getline way? 也许对于这么大的文件,我需要使用常见的getline方法?

The istream_iterator just uses the >> operator on the type. istream_iterator仅在类型上使用>>运算符。 It keeps at most one instance of the type in memory. 它在内存中最多保留一种类型的实例。 When using it, you do make copies of the instance. 使用它时,您确实要复制该实例。 I would suspect (but without seeing the code) that either your copy constructor or destructor is defective, or you leak memory somewhere in your >> operator. 我会怀疑(但看不到代码)您的复制构造函数或析构函数有缺陷,或者您在>>运算符的某个位置泄漏了内存。 I would be very surprised if the problem is related to istream_iterator . 如果问题与istream_iterator有关,我将感到非常惊讶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM