虚拟机是否需要交换分区?

Do virtual machines need swap partitions?

我在我的物理机上 运行ning Ubuntu;和 VirtualBox 到 运行 各种虚拟的。

大部分时间,我都在做 "straight forward" 安装;但今天我想成为 "smart" 并检查了 fedora 或 ubuntu 安装程序将在我的虚拟磁盘上创建的分区。

当然,在使用默认设置时,我的虚拟磁盘的一些 GB 将用作 "swap space"。

现在我想知道:假设我有足够的物理内存(因此我可以为 VM 分配 6 或 8 GB)- 虚拟机的专用交换 space 有什么意义吗?

交换分区用于在物理内存用完时释放您的物理内存 space。在具有大量内存的现代机器中,这取决于您要 运行ning 的应用程序类型。如果您打算 运行 此类内存密集型程序,如视频编辑器、高端游戏或类似的东西,虚拟内存或交换 space 是一项资产。但如果不是这种情况,那么只要您有足够的内存,就可以安全地避免交换 space。但是有后备是安全的。

这取决于您在主机系统上 运行 与虚拟机一起使用的程序,或者您在虚拟机中 运行 使用的程序。

软件可以消耗的内存的唯一上限是它可用的总内存(物理或虚拟)。有很多程序在正常运行时需要大量内存,并且有很多情况会导致程序消耗大量内存(例如加载输入文件)。还有很多无意中占用大量内存的错误程序。

您通常可以通过检查 运行 程序的要求或建议(例如内存和驱动器 space)来获得灵感。如果失败,请尝试一下。

这是answered at ServerFault:

TL;DR: 使用交换:1. 避免内存不足错误,2. guest OS 更擅长内存管理

Ignoring the fact that people are dealing with OS specific reasons I have two reasons why it's a bad idea to not run with a swap partition/file.

  1. If you have 1.5 GB of RAM allocated to a VM with no space file/partition and it wants to use 1.5 GB + 1 MB it will report an out of memory error. With the swap space it will be able to swap data out of active memory and onto the disk.

  2. The guest OS does a much better job of memory management than the host. This is why technology like memory ballooning exists because the Host can make educated guesses on what memory isn't needed right now but the guest knows at a much more intelligent level (this keeps OS memory from being swapped out which could kill your performance).