.NET Micro Framework,在内存有限的设备上读取文件
在 ChipworkX 设备上,我们将使用以下方式读取文件:
File.ReadAllBytes(filename);
但如果我们在内存量小得多的 NetDuino Plus 上尝试这样做,
我们只是得到一个 OutOfMemoryException 异常。
这些文件不是那么大,但我想在这种情况下这都是相对的(最大 1.5kb)。
在这样的设备上读取文件的正确方法是什么?
On a ChipworkX device we would read files using:
File.ReadAllBytes(filename);
But if we try that on a NetDuino Plus which has a much smaller amount of memory,
we simply get an OutOfMemoryException.
The files are not that big, but I guess that's all relative in this case (1.5kb max).
What's the correct way to read files on a device like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
FileStream
编写您自己的
处理
方法。 1024 的块长度只是一个示例,一次读取尽可能大的块。您可以根据数据进行更改。Use a
FileStream
write your own
Process
method. The block length of 1024 is just an example, read as big chunks as you can process at a time. You can vary that depending on the data.我假设您认为应该有足够的内存。如果是这样,我怀疑内部默认缓冲区大小会造成问题。尝试在打开文件时明确指定缓冲区大小,以使其紧贴实际文件长度:
I am assuming that you believe that there should be sufficient memory. If this is so, I suspect that internal default buffer sizes are blowing things. Try explicitly stating buffer sizes when opening the file to keep it tight to the actual file length:
当您使用内存有限的设备时,最好使用扇区大小的缓冲区。你所做的就是用速度换取内存。当你的内存很少时,你必须放慢速度,而扇区是你可以使用的、有意义的最小单位。
我建议使用 512 字节的缓冲区。
When you are using a device with limited memory, it is a good idea to use a buffer that is the size of a sector. What you are doing is trading speed for memory. When you have little memory, you must do things more slowly and a sector is the smallest unit you can use that makes any sense.
I suggest a buffer of 512 bytes.