Memory error and garbage collection (Circuitscape)

Been working for weeks with the same code to run batches of models in Circuitscape. The machine has a LOT of memory and available storage, yet I am now getting an out of memory error. I don’t understand the Stacktrace or how to clear temporary data that might be eating up memory? Can someone please explain what the code below indicates and/or how to clear temp data? Does GC.gc() do this?

Super new to Julia!

ERROR: OutOfMemoryError()
Stacktrace:
  [1] Array
    @ .\boot.jl:457 [inlined]
  [2] spmatmul(A::SparseArrays.SparseMatrixCSC{Float64, Int64}, B::SparseArrays.SparseMatrixCSC{Float64, Int64})
    @ SparseArrays C:\Users\Imogene\AppData\Local\Programs\Julia-1.7.3\share\julia\stdlib\v1.7\SparseArrays\src\linalg.jl:196
  [3] *
    @ C:\Users\Imogene\AppData\Local\Programs\Julia-1.7.3\share\julia\stdlib\v1.7\SparseArrays\src\linalg.jl:166 [inlined]
  [4] _tri_matmul(A::LinearAlgebra.Adjoint{Float64, SparseArrays.SparseMatrixCSC{Float64, Int64}}, B::SparseArrays.SparseMatrixCSC{Float64, Int64}, C::SparseArrays.SparseMatrixCSC{Float64, Int64}, δ::Nothing)
    @ LinearAlgebra C:\Users\Imogene\AppData\Local\Programs\Julia-1.7.3\share\julia\stdlib\v1.7\LinearAlgebra\src\matmul.jl:1132
  [5] _tri_matmul
    @ C:\Users\Imogene\AppData\Local\Programs\Julia-1.7.3\share\julia\stdlib\v1.7\LinearAlgebra\src\matmul.jl:1124 [inlined]
  [6] *
    @ C:\Users\Imogene\AppData\Local\Programs\Julia-1.7.3\share\julia\stdlib\v1.7\LinearAlgebra\src\matmul.jl:1120 [inlined]
  [7] extend_hierarchy!(levels::Vector{AlgebraicMultigrid.Level{SparseArrays.SparseMatrixCSC{Float64, Int64}, SparseArrays.SparseMatrixCSC{Float64, Int64}, LinearAlgebra.Adjoint{Float64, SparseArrays.SparseMatrixCSC{Float64, Int64}}}}, strength::AlgebraicMultigrid.SymmetricStrength{Float64}, aggregate::AlgebraicMultigrid.StandardAggregation, smooth::AlgebraicMultigrid.JacobiProlongation{Float64}, improve_candidates::AlgebraicMultigrid.GaussSeidel{AlgebraicMultigrid.SymmetricSweep}, diagonal_dominance::Bool, keep::Bool, A::SparseArrays.SparseMatrixCSC{Float64, Int64}, B::Vector{Float64}, symmetry::AlgebraicMultigrid.HermitianSymmetry, bsr_flag::Bool)
    @ AlgebraicMultigrid C:\Users\Imogene\.julia\packages\AlgebraicMultigrid\ASpK7\src\aggregation.jl:81
  [8] smoothed_aggregation(A::SparseArrays.SparseMatrixCSC{Float64, Int64}, ::Type{Val{1}}; symmetry::AlgebraicMultigrid.HermitianSymmetry, strength::AlgebraicMultigrid.SymmetricStrength{Float64}, aggregate::AlgebraicMultigrid.StandardAggregation, smooth::AlgebraicMultigrid.JacobiProlongation{Float64}, presmoother::AlgebraicMultigrid.GaussSeidel{AlgebraicMultigrid.SymmetricSweep}, postsmoother::AlgebraicMultigrid.GaussSeidel{AlgebraicMultigrid.SymmetricSweep}, improve_candidates::AlgebraicMultigrid.GaussSeidel{AlgebraicMultigrid.SymmetricSweep}, max_levels::Int64, max_coarse::Int64, diagonal_dominance::Bool, keep::Bool, coarse_solver::Type, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ AlgebraicMultigrid C:\Users\Imogene\.julia\packages\AlgebraicMultigrid\ASpK7\src\aggregation.jl:37
  [9] smoothed_aggregation (repeats 2 times)
    @ C:\Users\Imogene\.julia\packages\AlgebraicMultigrid\ASpK7\src\aggregation.jl:16 [inlined]
 [10] macro expansion
    @ .\timing.jl:299 [inlined]
 [11] solve(prob::Circuitscape.GraphProblem{Float64, Int64, Circuitscape.AMGSolver}, #unused#::Circuitscape.AMGSolver, flags::Circuitscape.RasterFlags, cfg::Dict{String, String}, log::Bool)
    @ Circuitscape C:\Users\Imogene\.julia\packages\Circuitscape\XpftG\src\core.jl:155
 [12] single_ground_all_pairs (repeats 2 times)
    @ C:\Users\Imogene\.julia\packages\Circuitscape\XpftG\src\core.jl:66 [inlined]
 [13] _pt_file_no_polygons_path(rasterdata::Circuitscape.RasterData{Float64, Int64}, flags::Circuitscape.RasterFlags, cfg::Dict{String, String})
    @ Circuitscape C:\Users\Imogene\.julia\packages\Circuitscape\XpftG\src\raster\pairwise.jl:63
 [14] raster_pairwise(T::Type, V::Type, cfg::Dict{String, String})
    @ Circuitscape C:\Users\Imogene\.julia\packages\Circuitscape\XpftG\src\raster\pairwise.jl:29
 [15] _compute(T::Type, V::Type, cfg::Dict{String, String})
    @ Circuitscape C:\Users\Imogene\.julia\packages\Circuitscape\XpftG\src\run.jl:43
 [16] macro expansion
    @ .\timing.jl:299 [inlined]
 [17] compute(path::String)
    @ Circuitscape C:\Users\Imogene\.julia\packages\Circuitscape\XpftG\src\run.jl:31
 [18] top-level scope
    @ REPL[1]:1

What version of Julia? Also, you should profile to see what is using the memory.

1.7.3, judging by the dir filename.

How much?

Yes, but you shouldn’t ever have to call it from the REPL.

Have you been using the same REPL session for weeks? This may seem like a stupid question, but you provided very little info.

EDIT: Oscar’s gave a good suggestion, profiling may help with the diagnosis. Here’s the relevant part of the manual: Profiling · The Julia Language

Version 1.7.3

Not a stupid question, I’m a very new user! I have been running models for several weeks, but have intermittently shut down Julia and the machine. I am not sure how to clear REPL (again, amateaur hour over here, learning as I go). Edit: I’ve used Ctrl + L but I’m still getting the outofmemory error.

Thank you both will try

The latest Julia release, 1.8, has some relevant new features that may be useful to you, so you may wish to try it out now. For example, Profile.Allocs isn’t available with 1.7.3.

1 Like

Ctrl+L as far as I know, just clears the REPL output screen, and doesn’t do anything with memory.

As long as a reference to a variable exists, that memory won’t be freed, you can accumulate a lot of these variables over time so it’s probably best to restart every so often. If you want to get rid of a variable, you can just assign it to nothing. E.g.

big_variable=nothing

and that should free up some memory.

If you use VS Code with the Julia extension (which I highly recommend), it has a section which lists the variables that you are currently using - very helpful!

2 Likes

You can use varinfo() in the REPL to get info on current variables.

2 Likes

Thanks! I’m not sure if this applies to my problem however, as I am not coding models in Julia. I have done all of this in R and am using the computer function in the CIrcuitscape package to generate resistance distances. The instructions for Circuitscape are comprised in a .ini file that details the location of the files used to create the matrices. Are there variables created in this process? I have downloaded the new version of Julia and restarted the machine and program, but no luck.

EDIT: I have 429 GB of storage available and 31 GB usable RAM.

Maybe you could post the issue also here to get help: Issues · Circuitscape/Circuitscape.jl · GitHub

It could be that the issue is Circuitscape specific or that it depends on the inputs you use.
(More information or a “minimal working example” would be useful either way, also for posting an issue on Circuitscape. The error log only shows that during the solve command somehow too much memory is needed, that could be for many different reasons.)

1 Like

Can you share full code?

The code is very short, I call compute(“Pathway_to_file”) where the .ini files have all details for Circuitscape. Example of ini file contents/directions:

(This has been working for weeks)

Options for advanced mode]
ground_file_is_resistances = True
remove_src_or_gnd = keepall
ground_file = (Browse for a ground point file)
use_unit_currents = False
source_file = (Browse for a current source file)
use_direct_grounds = False

[Mask file]
mask_file = C:\ImogeneTemp\RWork\elevmask150m.asc
use_mask = True
[Calculation options]
low_memory_mode = False
parallelize = False
solver = cg+amg
print_timings = False
preemptive_memory_release = False
print_rusages = False
max_parallel = 1

[Short circuit regions (aka polygons)]
polygon_file = (Browse for a short-circuit region file)
use_polygons = False

[Options for one-to-all and all-to-one modes]
use_variable_source_strengths = False
variable_source_file = None

[Output options]
set_null_currents_to_nodata = False
set_focal_node_currents_to_zero = False
set_null_voltages_to_nodata = False
compress_grids = False
write_cur_maps = False
write_volt_maps = False
output_file = C:\ImogeneTemp\RWork\Precip\PrecipOutput\precip8pairwise.out
write_cum_cur_map_only = False
log_transform_maps = False
write_max_cur_maps = False

[Version]
version = 4.0.5

[Options for reclassification of habitat data]
reclass_file = (Browse for file with reclassification data)
use_reclass_table = False

[Logging Options]
log_level = INFO
log_file = C:\ImogeneTemp\RWork\Precip\precip8PairCircuit.log
profiler_log_file = None
screenprint_log = False

[Options for pairwise and one-to-all and all-to-one modes]
included_pairs_file = (Browse for a file with pairs to include or exclude)
use_included_pairs = False
point_file = C:\ImogeneTemp\RWork\Precip\sitesprecip1.asc

[Connection scheme for raster habitat data]
connect_using_avg_resistances = False
connect_four_neighbors_only = False

[Habitat raster or graph]
habitat_map_is_resistances = True
habitat_file = C:\ImogeneTemp\RWork\Precip\precipcost8.asc

[Circuitscape mode]
data_type = raster
scenario = pairwise
use_64bit_indexing = True

Pasted the ini file contents below in another comment, will post issue where suggested as well.

Are you tried use another scenario? For example from this page. On Solvers and Computation Time · Circuitscape.jl Documentation
Also you have low_memory_mode = False try change on True.

1 Like

Did my last answer help you?