文件读取如何杀死内存 [英] how file reading kills memory

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

问题描述

Im busi具有读取大量文本文件的程序,但是问题是使用TStringLists会导致内存使用过度,并且程序倾向于冻结或阻塞计算机速度.

Im busi with a proram that reads numorous text files, but the problem is that using TStringLists the memory is over used and the program tends to freeze or block out the computer speed. is the a way to do file accessing without using TStringLists, TString objects and fstreams?

推荐答案

读取文件不会占用内存.当然,您不必使用TStringListTString来读取文件.如果看起来像您使用文本文件.您可以使用一个字符串读取文件.不使用fstream读取文件的想法很荒谬.这正是您需要做的.如果您只是阅读,则应使用ifstream,请参阅 http://www.cplusplus.com/reference/iostream/ifstream/ [ ^ ].

—SA
File reading does not kill memory. Of course you don''t have to use TStringList or TString to read file. If looks like you work with text files. You can read the file in one single string. The idea of reading file without fstream is ridiculous. This is exactly what you need to do. If you just read, you should use ifstream, see http://www.cplusplus.com/reference/iostream/ifstream/[^].

—SA


我假设您使用C ++ Builder,因为我所知道的唯一称为TStringTStringList的内置类可以可以在Borland Delphi(和C ++ Builder,可以通过快速搜索确认)中找到.

您可能要检查这些文件可能消耗多少内存,以及是否必须同时将它们全部保留在内存中.另外,如果您要根据阅读的内容构建其他数据结构,请检查这将占用更多的内存,以及是否也需要将其保留在内存中.

如果您的内存不足,这是您唯一可以做的事情. fstreamTStringTStringList都应该安全使用.但是,如果要使用new构建其他数据结构,请确保在不再需要它们时再次正确地执行delete.例如,如果您使用new创建TStringList对象,请确保在重新分配指针之前或在指针超出范围之前调用delete
I am assuming you work with C++Builder, as the only built-in classes called TString or TStringList I know of can be found in Borland Delphi (and C++Builder, as a quick search confirmed).

You might want to check how much memory these files might consume, and whether you have to keep them all in memory at the same time. Also, if you''re building additional data structures based on what you read, check how much more memory this takes, and whether you need to keep that in memory as well.

If you''re running low on memory, this is about the only thing you can do. fstream, TString, and TStringList all should be safe to use. However, if you''re building additional data structures using new, make sure that you do delete them properly again when you don''t need them any more. For instance if you create TStringList objects with new, make sure to call delete before reassigning the pointer, or before the pointer runs out of scope!


这篇关于文件读取如何杀死内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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