Skip to content

Commit 88dd337

Browse files
ericphansonararslan
authored andcommitted
Make global variables constant (#286)
* Make global variables constant * Add test for `clearmemory`
1 parent 03d9764 commit 88dd337

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

src/Convex.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@ include("utilities/broadcast.jl")
8484

8585
#Temporary workaround for memory leak (https://github.com/JuliaOpt/Convex.jl/issues/83)
8686
function clearmemory()
87-
global id_to_variables = Dict{UInt64, Variable}()
88-
global var_to_ranges = Dict{UInt64, Tuple{Int, Int}}()
89-
global conic_constr_to_constr = Dict{ConicConstr, Constraint}()
87+
global id_to_variables
88+
empty!(id_to_variables)
89+
global conic_constr_to_constr
90+
empty!(conic_constr_to_constr)
9091
GC.gc()
9192
end
9293

src/constraints/constraints.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Base.==, Base.<=, Base.>=, Base.<, Base.>
22
export EqConstraint, LtConstraint, GtConstraint
33
export ==, <=, >=
44

5-
conic_constr_to_constr = Dict{ConicConstr, Constraint}()
5+
const conic_constr_to_constr = Dict{ConicConstr, Constraint}()
66

77
### Linear equality constraint
88
mutable struct EqConstraint <: Constraint

src/variable.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ end
5757
# the expression tree will only utilize variable ids during construction
5858
# full information of the variables will be needed during stuffing
5959
# and after solving to populate the variables with values
60-
id_to_variables = Dict{UInt64, Variable}()
60+
const id_to_variables = Dict{UInt64, Variable}()
6161

6262
function vexity(x::Variable)
6363
return x.vexity

test/test_utilities.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
@testset "Utilities" begin
22

3+
@testset "clearmemory" begin
4+
# solve a problem to populate globals
5+
x = Variable()
6+
p = minimize(-x, [x <= 0])
7+
@test vexity(p) == AffineVexity()
8+
solve!(p, solvers[1])
9+
10+
@test !isempty(Convex.id_to_variables)
11+
@test !isempty(Convex.conic_constr_to_constr)
12+
13+
Convex.clearmemory()
14+
15+
# check they are cleared
16+
@test isempty(Convex.id_to_variables)
17+
@test isempty(Convex.conic_constr_to_constr)
18+
end
19+
320
@testset "ConicObj" for T = [UInt32, UInt64]
421
c = ConicObj()
522
z = zero(T)

0 commit comments

Comments
 (0)