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

MacOS旧版本R运行cnetplot触发内存限制错误求助

问题描述

环境信息

  • 系统:MacOS 12.7.6
  • R版本:4.4.0
  • RStudio版本:2023.06.1+524
  • 物理内存:16Gb

错误现象

频繁触发vector memory limit of 100.0 Gb reached, see mem.maxVSize()错误,已将R_MAX_VSIZE设置为100Gb(远超物理内存),仅能通过重启R会话、清理内存临时解决。运行特定代码时(执行cnetplot命令)持续崩溃,即使将R_MAX_VSIZE调至50000Gb仍无法解决。怀疑旧版MacOS存在内存泄漏,但设备为2015款Intel笔记本无法升级系统。

复现代码

library(clusterProfiler)
library(org.Hs.eg.db)

data(geneList, package="DOSE")

gene = names(geneList)[abs(geneList) > 2.0]

# Run enrichGO
x = enrichGO(gene = gene,
             ont  = "BP",
             OrgDb = org.Hs.eg.db,
             pvalueCutoff = 0.01,
             pAdjustMethod = "fdr",
             universe = names(geneList),
             minGSSize = 5,
             maxGSSize = 500,
             qvalueCutoff = 0.01,
             readable = FALSE)

cnetplot(x, 
         color.params = list(foldChange = geneList)
)

Session Info

> sessionInfo()
R version 4.4.0 (2024-04-24)
Platform: x86_64-apple-darwin20
Running under: macOS Monterey 12.7.6

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0

locale:
[1] es_ES.UTF-8/es_ES.UTF-8/es_ES.UTF-8/C/es_ES.UTF-8/es_ES.UTF-8

time zone: Europe/Madrid
tzcode source: internal

attached base packages:
[1] stats4    stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
[1] org.Hs.eg.db_3.19.1    AnnotationDbi_1.66.0   IRanges_2.38.1        
[4] S4Vectors_0.42.1       Biobase_2.64.0         BiocGenerics_0.50.0   
[7] clusterProfiler_4.12.6

loaded via a namespace (and not attached):
 [1] DBI_1.2.3               shadowtext_0.1.5        gson_0.1.0             
 [4] gridExtra_2.3           httr2_1.1.2             rlang_1.1.6            
 [7] magrittr_2.0.3          DOSE_3.30.5             compiler_4.4.0         
[10] RSQLite_2.4.1           png_0.1-8               vctrs_0.6.5            
[13] reshape2_1.4.4          stringr_1.5.1           pkgconfig_2.0.3        
[16] crayon_1.5.3            fastmap_1.2.0           XVector_0.44.0         
[19] ggraph_2.2.1            enrichplot_1.24.4       UCSC.utils_1.0.0       
[22] purrr_1.0.4             bit_4.6.0               zlibbioc_1.50.0        
[25] cachem_1.1.0            aplot_0.2.8             GenomeInfoDb_1.40.1    
[28] jsonlite_2.0.0          blob_1.2.4              BiocParallel_1.38.0    
[31] tweenr_2.0.3            parallel_4.4.0          R6_2.6.1               
[34] stringi_1.8.7           RColorBrewer_1.1-3      GOSemSim_2.30.2        
[37] Rcpp_1.0.14             R.utils_2.13.0          Matrix_1.7-3           
[40] splines_4.4.0           igraph_2.1.4            tidyselect_1.2.1       
[43] qvalue_2.36.0           viridis_0.6.5           codetools_0.2-20       
[46] lattice_0.22-7          tibble_3.3.0            plyr_1.8.9             
[49] treeio_1.28.0           withr_3.0.2             KEGGREST_1.44.1        
[52] S7_0.2.0                gridGraphics_0.5-1      scatterpie_0.2.5       
[55] polyclip_1.10-7         Biostrings_2.72.1       pillar_1.10.2          
[58] ggtree_3.12.0           ggfun_0.1.9             generics_0.1.4         
[61] ggplot2_3.5.2           scales_1.4.0            tidytree_0.4.6         
[64] glue_1.8.0              lazyeval_0.2.2          tools_4.4.0            
[67] data.table_1.17.6       fgsea_1.30.0            fs_1.6.6               
[70] graphlayouts_1.2.2      fastmatch_1.1-6         tidygraph_1.3.1        
[73] cowplot_1.1.3           grid_4.4.0              tidyr_1.3.1            
[76] ape_5.8-1               colorspace_2.1-1        nlme_3.1-168           
[79] GenomeInfoDbData_1.2.12 patchwork_1.3.1         ggforce_0.5.0          
[82] cli_3.6.5               rappdirs_0.3.3          viridisLite_0.4.2      
[85] dplyr_1.1.4             gtable_0.3.6            R.methodsS3_1.8.2      
[88] yulab.utils_0.2.0       digest_0.6.37           ggrepel_0.9.6          
[91] ggplotify_0.1.2         farver_2.1.2            memoise_2.0.1          
[94] R.oo_1.27.1             lifecycle_1.0.4         httr_1.4.7             
[97] GO.db_3.19.1            bit64_4.6.0-1           MASS_7.3-65 
问题根源与解决办法

根源分析

  1. 旧版MacOS内存管理Bug:MacOS Monterey 12.x与R 4.4.x存在兼容性问题,部分内存分配逻辑存在泄漏,尤其是处理cnetplot依赖的ggraph、ggplot2图形渲染时,会无限制占用虚拟内存,调高R_MAX_VSIZE也无法阻止,因为系统层面的内存回收机制失效。
  2. cnetplot的内存负载:cnetplot需要构建基因-功能术语的关联网络,当富集结果中的术语和基因数量较多时,图形渲染过程会生成大量中间对象,在内存泄漏环境下这些对象无法被及时回收,最终耗尽虚拟内存。

解决办法

1. 降低图形渲染复杂度

  • 限制cnetplot展示的术语数量:用showCategory参数指定前N个显著术语,减少网络节点数:
    cnetplot(x, 
             showCategory = 10,  # 仅展示前10个最显著的BP术语
             color.params = list(foldChange = geneList)
    )
    
  • 简化图形元素:去掉color.params或简化颜色映射,避免</think_never_used_51bce0c785ca2f68081bfa7d91973934>外内存计算;用circular = FALSE切换为非环形布局,降低渲染负载。

2. 优化R内存配置与回收

  • 调整mem.maxVSize为合理值:设置为物理内存的2倍(如32Gb),避免系统过度交换虚拟内存:
    mem.maxVSize(32 * 1024^3)  # 32Gb
    
  • 手动强制内存回收:运行cnetplot前后执行gc(),主动释放未使用内存:
    gc()
    cnetplot(...)
    gc()
    
  • 禁用图形设备缓存:命令行启动R时添加--no-save --no-restore参数,或临时切换为无交互图形设备:
    pdf(NULL)  # 临时禁用图形输出
    # 运行cnetplot但不输出
    dev.off()  # 恢复设备后重新绘图
    

3. 降级R版本

R 4.4.x对旧版MacOS兼容性较差,降级到R 4.3.3(LTS版本)可避开内存管理相关Bug,该版本在MacOS Monterey上的内存泄漏问题更少。

4. 替代可视化方案

如果上述方法无效,改用内存占用更低的工具:

  • 用dotplot(x)替代cnetplot,仅展示术语的富集显著性和基因计数;
  • 提取富集结果表格数据,用ggplot2手动绘制基因-术语关联。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 19:20:54