我一直在使用 Java 处理大型日志文件(约 100 MB)并注意到 gzip
可以将它们压缩到约 3MB,使它们小 35 倍。
所以我想知道:现代操作系统会在将文件加载到内存之前压缩文件吗?使用 100 MB 的 RAM 来保存一个实际上只有 3 MB 信息的文件似乎很愚蠢。
还是相反?读取文件(以及处理编码等)的过程是否意味着在磁盘上占用 100MB 的文件实际上在内存中大于 100MB?
* 加分点:有什么建议可以在加载文件之前对文件进行预处理,以减少 JVM
的内存使用量吗? (这些文件的格式与 Apache
服务器日志的格式相同。)
最佳答案
Do modern OSes compress files before loading them into memory? It seems silly to use 100 MB of RAM to hold a file that really only has 3 MB of information.
这取决于所涉及的应用程序。一些应用程序可能会压缩保存在内存中的数据,而另一些则不会。
Or is it the opposite? Does the process of reading a file (and dealing with encodings and whatnot) mean that a file which takes up 100MB on disk is actually bigger than 100MB in memory?
同样,这完全取决于应用程序。
*bonus points: Any recommendations for preprocessing I could do to my files before loading them in order to reduce my JVM's memory usage? (The files have the same format as Apache server logs.)
不要将不需要处理或显示的任何数据加载到内存中。生成平均值或总和所需的任何内容都可以临时加载并添加到运行总计中,然后可以丢弃。
关于java - 内存中的文件是否与文件系统中的文件大小相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31821949/