在检查目录属性时,conda 环境的磁盘使用量似乎大于实际情况?

Disk usage of conda environments appears greater than it really is when cheking directory properties?

首先,我是虚拟环境的新手,而且我没有软件背景(英语也不是我的第一语言,所以请宽恕)。我确信 conda 环境以某种方式优化为不重复磁盘上的包,而是以某种方式使用链接。但是当检查使用过的硬盘 space(通过右键单击 -> 属性,在 Linux Mint 上)时,它看起来非常高:超过 2 GB(env 有 python、numpy 和 pandas).

谁能告诉我(或指明方向)这是如何工作的?

2 GB 对于该软件包列表来说似乎太高了。我刚刚做了一个测试。在 Linux,这样的环境占用 1.2 GB。在 Mac 上,它只需要 271 MB。 (我不完全确定是什么导致了两者之间的差异,但可能与不同的文件系统有关。)

您是在检查单个环境的大小,还是在检查整个 anaconda 目录树的大小?

关于 conda 中的磁盘节省技巧:你是对的,conda 使用 hard links(如果可能)来避免在磁盘上复制文件。这有助于节省磁盘空间 space,因为否则同一个文件将在多个环境中以及在 conda 包缓存 (pkgs) 中重复。不幸的是,conda 无法为某些文件创建硬 links(出于技术原因),因此它必须复制这些文件。

du 工具可以告诉您 space 有多少磁盘被特定目录(或目录列表)占用。它知道 hard-links,因此如果同一个文件由于 hard-link 而出现两次,它会避免重复计算文件大小。 (我不知道 "properties" 菜单项在 Linux Mint 中的行为是否相同。)

例如,我将创建两个相同的 conda 环境并独立检查它们的磁盘使用情况:

$ conda create -n test-1 -y python numpy pandas
$ conda create -n test-2 -y python numpy pandas

$ du -h -s $(conda info --base)/envs/test-1
1.2G    /opt/miniconda/envs/test-1

$ du -h -s $(conda info --base)/envs/test-2
1.2G    /opt/miniconda/envs/test-2

但是如果我要求 du 同时考虑它们,它会注意到 test-2 中的一些文件已经在test-1,所以它不会再计算它们的大小:

$ du -h -s $(conda info --base)/envs/test-?
1.2G    /opt/miniconda/envs/test-1
268M    /opt/miniconda/envs/test-2

如果您想知道哪些文件是硬 linked,请查看 ls -l 的输出:

$ ls -l $(conda info --base)/envs/test-1/lib/libz.so.1.2.11
-rwxrwxr-x 15 bergs flyem 109272 Sep  9  2019 /opt/miniconda/envs/test-1/lib/libz.so.1.2.11
           ^
            `-- This file has 15 different names,
                i.e. it can be found in 15 different places on disk,
                due to hard-links.

$ ls -l $(conda info --base)/envs/test-1/lib/libpython3.8.so.1.0
-rwxrwxr-x 1 bergs flyemdev 14786920 Jun 16 12:44 /opt/miniconda/envs/test-1/lib/libpython3.8.so.1.0
           ^
            `-- This file has only 1 name on disk,
                i.e. there are no other hard-links to this file.

如果您担心用完磁盘 space,您可以 运行 此命令清理 conda 用于设置您的环境的所有临时包、zip 文件等。

conda clean --all

这些文件会保留下来,随着时间的推移会使您的磁盘变得杂乱无章。

我经常使用它,每次都获得超过几千兆字节的回馈。