MacOS下Quarto+Julia出现栈大小超出错误的原因及解决
Quarto在MacOS下绘图触发栈溢出错误的问题
最小可复现示例(MWE)
--- title: "Plots Demo" author: "ITA" format: html: code-fold: true jupyter: julia-1.9 --- ## Test plots ```{julia} #| label: fig-errors #| fig-cap: "Plotting causes stack exceeded error - MacOS" using Plots plotly() dat = rand(1200, 68) plot(dat)
## 报错情况 运行`quarto preview test.qmd`时触发如下错误:
ERROR: RangeError: Maximum call stack size exceeded
使用环境:Quarto 1.3.361 + macOS Ventura 13.4 ## 问题原因 这个错误属于**栈空间耗尽**,并非内存不足。内存不足会触发Out-of-Memory类错误,而`RangeError: Maximum call stack size exceeded`是由于函数递归调用层级过深,超出了Julia进程的默认栈大小限制。 具体触发点:直接用`plot(dat)`绘制1200×68的矩阵时,Plots.jl的plotly后端在处理多达68个数据系列时,内部递归逻辑的调用层级超出了栈的承载上限。 ## 解决方案 - **更换绘图后端**:改用GR后端(Plots.jl默认后端之一),它的内存和栈使用效率更高,大概率能避免这个问题: ```julia using Plots gr() # 替换plotly()为gr() dat = rand(1200, 68) plot(dat)
- 增大Julia栈大小:通过环境变量调整Julia的栈上限,在终端中先执行以下命令再启动Quarto:
可根据实际情况调整数值,但过大可能导致系统资源占用过高。export JULIA_STACK_SIZE=8388608 # 设置为8MB,单位是字节,需为2的幂 quarto preview test.qmd - 优化绘图逻辑:避免一次性绘制过多系列,比如拆分数据分块展示,或关闭图例减少渲染压力:
using Plots plotly() dat = rand(1200, 68) # 分多块布局展示,关闭图例 plot(dat, layout=(4, 17), legend=false)
内容的提问来源于stack exchange,提问作者ITA
相关产品推荐
相关产品推荐

