Android:加载文本文件时出现内存不足错误。如何在加载文件之前获取可用内存
我想在文字处理应用程序中加载文本文件。但是当文件太大时,我会出现内存不足错误。我最终确定最多可以加载 1 MB 的文件。但有时即使是 1MB 的文件也会出现内存不足的情况。但在加载之前我必须先说一下是否可以加载文件。
除了 freeMemory() 之外,我尝试寻找最大可能可用内存的解决方案, 即空闲内存 + (最大内存 - 总内存),它将给出应用程序可能的总可用内存。 (假设大约为 18MB 到 20MB)。但是在完全利用堆后我遇到了内存不足错误。例如(24 MB)。
我的问题实际上是(18MB 到 20 mb)“最大可能可用内存”在加载 1MB 文件时用于分配。
应有多少内存可用于加载 1MB 文件。
我怎样才能粗略地计算它。
有没有办法摆脱PSS,Privatedirty。我对 PSS 不太了解。但我无法在文章“如何发现 Android 中我的应用程序的内存使用情况”中得到太多关于总结的信息
谢谢
I want to load a text file in a wordprocessing apps. But I get out of memory error when files are of too big. I finalized that I can load upto 1 MB files. But sometimes I get out of memory even for 1MB files. But I have to say before loading whether I can load files or not.
I tried for solution of finding maximum possible available memory, apart from freeMemory(),
that is freememory + (maxmemory - totalmemory) which will give the total possible available memory for the application. (Say it will be around 18MB to 20 MB). But I get outofMemory error after completely utilizing the heap. say for example(24 MB).
My question is really that (18MB to 20 mb) of "maximum possible available memory" is utilized for allocation when loading 1MB file.
How much of memory should be available to load 1MB file.
How can I roughly compute it.
Is there any way out of PSS, Privatedirty. I couldn't understand much about PSS. But I couldn't get much info regarding of summing up in article "How to discover memory usage of my application in Android"
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请记住,将文件存储在变量中的方式非常重要。手动使用 char 数组是最节省内存的方法之一,但您仍然需要考虑占用 16 位或 2 个字节的每个字符。因此,如果您有使用某种 8 位编码的文本文件并将其加载到 char 数组中,则需要两倍的空间。
Remember, the way you store the files in variables matters quite a lot. Using char array manually is one of the most memory-efficient ways, but you still need to account for every character taking 16 bits, or 2 bytes. So, if you have text file using some 8-bit encoding and you load it into char array, it takes twice as much space.