如何在 Delphi 中使用大文件?
当我在内存流或文件流中使用大文件时,我看到“内存不足”错误 我该如何解决这个问题?
例子:
procedure button1.clıck(click);
var
mem:TMemoryStream;
str:string;
begin
mem:=Tmemorystream.create;
mem.loadfromfile('test.txt');----------> there test.txt size 1 gb..
compressstream(mem);
end;
When I use a large file in memorystream or filestream I see an error which is "out of memory"
How can I solve this problem?
Example:
procedure button1.clıck(click);
var
mem:TMemoryStream;
str:string;
begin
mem:=Tmemorystream.create;
mem.loadfromfile('test.txt');----------> there test.txt size 1 gb..
compressstream(mem);
end;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的实现非常混乱。我不知道 CompressStream 到底是做什么的,但是如果您想将大文件作为流处理,您可以通过简单地使用 TFileStream 来节省内存,而不是尝试一次将整个文件读入 TMemoryStream 中。
此外,当您使用完 TMemoryStream 后,您永远不会释放它,这意味着您将泄漏大量内存。 (除非 CompressStream 解决了这个问题,但从代码中并不清楚,这样写确实不是一个好主意。)
Your implementation is very messy. I don't know exactly what CompressStream does, but if you want to deal with a large file as a stream, you can save memory by simply using a TFileStream instead of trying to read the whole thing into a TMemoryStream all at once.
Also, you're never freeing the TMemoryStream when you're done with it, which means that you're going to leak a whole lot of memory. (Unless CompressStream takes care of that, but that's not clear from the code and it's really not a good idea to write it that way.)
您无法将整个文件放入单个连续的 32 位地址空间块中。因此出现内存不足错误。
将文件分成较小的部分读取并逐部分处理。
You can't fit the entire file into a single contiguous block of 32 bit address space. Hence the out of memory error.
Read the file in smaller pieces and process it piece by piece.
回答标题中的问题,如果需要的话,您需要逐个、逐字节地处理文件:您绝对不会将文件一次全部加载到内存中!如何执行此操作显然取决于您需要对文件执行什么操作;但既然我们知道您正在尝试实现霍夫曼编码器,我将为您提供一些具体的提示。
霍夫曼编码器是一种流编码器:字节输入,位输出。每个传入数据单元都将替换为其相应的位模式。编码器不需要立即查看整个文件,因为它实际上每次只处理一个字节。
以下是如何对文件进行霍夫曼压缩而不将其全部加载到内存中;当然,实际的霍夫曼编码器没有显示,因为问题是关于处理大文件,而不是构建实际的编码器。这段代码包括缓冲的输入和输出,并展示了如何将实际的编码器过程链接到它。
(请注意,在浏览器中编写的代码;如果它无法编译,您需要修复它!)
霍夫曼编码器不是一个难以实现的编码器,但正确执行它快速可能是一个挑战。我建议您从一个正确编码器开始,一旦您完成了编码和解码工作,就可以弄清楚如何实现快速编码器。
Answering the question in the title, you need to process the file piece by piece, byte by byte if that's needed: you definitively do not load the file all at once into memory! How you do that obviously depends on what you need to do with the file; But since we know you're trying to implement an Huffman encoder, I'll give you some specific tips.
An Huffman encoder is a stream encoder: Bytes go in and bits go out. Each unit of incoming data is replaced with it's corresponding bit pattern. The encoder doesn't need to see the whole file at once, because it is in fact only working on one byte each time.
Here's how you'd huffman-compress a file without loading it all into memory; Of course, the actual Huffman encoder is not shown, because the question is about working with big files, not about building the actual encoder. This piece of code includes buffered input and output and shows how you'd link an actual encoder procedure to it.
(beware, code written in browser; if it doesn't compile you're expected to fix it!)
The Huffman encoder is not a difficult encoder to implement, but doing it both correctly and fast might be a challenge. I suggest you start with a correct encoder, once you've got both encoding and decoding working figure out how to do a fast encoder.
尝试类似 http://www.explainth.at/en/delphi/mapstream.shtml< /a>
try something like http://www.explainth.at/en/delphi/mapstream.shtml