You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何在bplapply中运行R的NbClust会占用大量内存?

多核心运行NbClust时内存异常飙升问题

我首次在Stack Overflow发帖,若有需要改进之处请告知。在配备Apple M1 Max(10核、64GB内存)的macOS系统中,我尝试在R语言中对一个小型矩阵运行NbClust工具。单核心运行时一切正常,内存占用较低(符合预期),且CPU使用率保持在100%以下(说明NbClust本身没有内部多进程机制,不会与bplapply产生多重并行冲突问题)。但使用fork模式通过多核心并行运行时,内存会迅速攀升至30-60GB。

我已尝试以下排查手段,但问题仍未解决:

  • 用for循环替代sapply
  • 逐一排除不同的index参数
  • 更换聚类方法(如ward.D)

最小复现代码

library(BiocParallel) # Install from Bioconductor
library(NbClust)

# 创建随机正数矩阵
mat <- matrix(runif(30 * 100, min = 0, max = 100),
              nrow = 30, ncol = 100)

indeces <- c("kl", "ch", "hartigan",
             "cindex", "db", "silhouette", "duda", "pseudot2",
             "ratkowsky", "ball", "ptbiserial", "gap",
             "mcclain", "gamma", "gplus", "tau", "dunn",
             "sdindex", "sdbw")

### 单核心运行正常 ##############################################
param <- BiocParallel::MulticoreParam(workers = 1,
                                      progressbar = TRUE)

test <- BiocParallel::bplapply(
  X = 1:10,
  BPPARAM = param,
  function(i) {
    clust_results <- sapply(indeces, function(x) {
      tryCatch({
        NbClust::NbClust(data = mat, min.nc = 1, max.nc = 5,
                         method = "ward.D2", index = x)
      }, error = function(e) {
        return(NULL)
      })
    })
  }
)

### 多核心运行内存占用过高 ##############################
param <- BiocParallel::MulticoreParam(workers = 8,
                                      progressbar = TRUE)

test <- BiocParallel::bplapply(
  X = 1:10,
  BPPARAM = param,
  function(i) {
    clust_results <- sapply(indeces, function(x) {
      tryCatch({
        NbClust::NbClust(data = mat, min.nc = 1, max.nc = 5,
                         method = "ward.D2", index = x)
      }, error = function(e) {
        return(NULL)
      })
    })
  }
)

内容的提问来源于stack exchange,提问作者Christian Halter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 08:03:10