内存中的文件是否与文件系统中的文件大小相同?

Are files in memory the same size as they are in the file system?

我一直在 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.)

不要将不需要处理或显示的任何数据加载到内存中。生成平均值或总和所需的任何内容都可以临时加载并添加到 运行 总数中,然后可以丢弃。

你只得到你所要求的。如果你压缩它,它就会被压缩。 大多数情况下,内存中的大小和磁盘上的大小会略有不同。但这只是因为磁盘(扇区)上的存储单元更大。即使是 1 字节的文件,你大部分时间使用的也比磁盘上的多,因为 OS 为它保留一个扇区,这将取决于 OSes,你将主要找到 512、2048 的扇区或 4096 字节。