Skip to content

Commit aae96d0

Browse files
committed
Deprecate gc and gc_enable in favor of GC.gc and GC.enable
We document that these functions should not generally be used, and yet they're exported from Base. This moves the two functions into their own submodule, Base.GC, and deprecates the exported functions.
1 parent 86ca591 commit aae96d0

File tree

21 files changed

+63
-32
lines changed

21 files changed

+63
-32
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,8 @@ Deprecated or removed
957957

958958
* `ObjectIdDict` has been deprecated in favor of `IdDict{Any,Any}` ([#25210]).
959959

960+
* `gc` and `gc_enable` have been deprecated in favor of `GC.gc` and `GC.enable` ([#25616]).
961+
960962
Command-line option changes
961963
---------------------------
962964

@@ -1208,3 +1210,4 @@ Command-line option changes
12081210
[#25424]: https://github.com/JuliaLang/julia/issues/25424
12091211
[#25532]: https://github.com/JuliaLang/julia/issues/25532
12101212
[#25545]: https://github.com/JuliaLang/julia/issues/25545
1213+
[#25616]: https://github.com/JuliaLang/julia/issues/25616

base/deprecated.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2794,6 +2794,9 @@ end
27942794
@deprecate findn(x::AbstractMatrix) (I = findall(!iszero, x); (getindex.(I, 1), getindex.(I, 2)))
27952795
@deprecate findn(x::AbstractArray{T, N}) where {T, N} (I = findall(!iszero, x); ntuple(i -> getindex.(I, i), N))
27962796

2797+
@deprecate gc GC.gc
2798+
@deprecate gc_enable GC.enable
2799+
27972800
# issue #9053
27982801
if Sys.iswindows()
27992802
function Filesystem.tempname(uunique::UInt32)

base/exports.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,10 +903,9 @@ export
903903
include_dependency,
904904

905905
# RTS internals
906+
GC,
906907
finalizer,
907908
finalize,
908-
gc,
909-
gc_enable,
910909
precompile,
911910

912911
# misc

base/gcutils.jl

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,30 @@ Immediately run finalizers registered for object `x`.
3838
finalize(@nospecialize(o)) = ccall(:jl_finalize_th, Cvoid, (Ptr{Cvoid}, Any,),
3939
Core.getptls(), o)
4040

41+
module GC
42+
43+
export gc
44+
4145
"""
42-
gc()
46+
GC.gc()
47+
48+
Perform garbage collection.
4349
44-
Perform garbage collection. This should not generally be used.
50+
!!! warning
51+
Excessive use will likely lead to poor performance.
4552
"""
4653
gc(full::Bool=true) = ccall(:jl_gc_collect, Cvoid, (Int32,), full)
4754

4855
"""
49-
gc_enable(on::Bool)
56+
GC.enable(on::Bool)
5057
5158
Control whether garbage collection is enabled using a boolean argument (`true` for enabled,
52-
`false` for disabled). Return previous GC state. Disabling garbage collection should be
53-
used only with extreme caution, as it can cause memory use to grow without bound.
59+
`false` for disabled). Return previous GC state.
60+
61+
!!! warning
62+
Disabling garbage collection should be used only with caution, as it can cause memory
63+
use to grow without bound.
5464
"""
55-
gc_enable(on::Bool) = ccall(:jl_gc_enable, Int32, (Int32,), on) != 0
65+
enable(on::Bool) = ccall(:jl_gc_enable, Int32, (Int32,), on) != 0
66+
67+
end # module GC

doc/src/base/base.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,8 @@ Base.@functionloc
332332
## Internals
333333

334334
```@docs
335-
Base.gc
336-
Base.gc_enable
335+
Base.GC.gc
336+
Base.GC.enable
337337
Meta.lower
338338
Meta.@lower
339339
Meta.parse(::AbstractString, ::Int)

doc/src/manual/faq.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ session (technically, in module `Main`), it is always present.
1010
If memory usage is your concern, you can always replace objects with ones that consume less memory.
1111
For example, if `A` is a gigabyte-sized array that you no longer need, you can free the memory
1212
with `A = nothing`. The memory will be released the next time the garbage collector runs; you can force
13-
this to happen with [`gc()`](@ref). Moreover, an attempt to use `A` will likely result in an error, because most methods are not defined on type `Nothing`.
13+
this to happen with [`gc()`](@ref Base.GC.gc). Moreover, an attempt to use `A` will likely result in an error, because most methods are not defined on type `Nothing`.
1414

1515
### How can I modify the declaration of a type in my session?
1616

stdlib/FileWatching/test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This file is a part of Julia. License is MIT: https://julialang.org/license
22

3-
using Test, FileWatching
3+
using Test, FileWatching, Base.GC
44

55
# This script does the following
66
# Sets up n unix pipes

stdlib/Mmap/test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This file is a part of Julia. License is MIT: https://julialang.org/license
22

3-
using Test, Mmap, Random
3+
using Test, Mmap, Random, Base.GC
44

55
file = tempname()
66
write(file, "Hello World\n")

stdlib/SharedArrays/test/runtests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# This file is a part of Julia. License is MIT: https://julialang.org/license
22

3-
using Test, Distributed, SharedArrays, Random
3+
using Test, Distributed, SharedArrays, Random, Base.GC
44
include(joinpath(Sys.BINDIR, "..", "share", "julia", "test", "testenv.jl"))
55

66
addprocs_with_testenv(4)
77
@test nprocs() == 5
88

9-
@everywhere using Test, SharedArrays
9+
@everywhere using Test, SharedArrays, Base.GC
1010

1111
id_me = myid()
1212
id_other = filter(x -> x != id_me, procs())[rand(1:(nprocs()-1))]

stdlib/SparseArrays/test/sparse.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using Base.LinAlg: mul!, ldiv!, rdiv!
44
using Base.Printf: @printf
5+
using Base.GC
56
using Random
67

78
@testset "issparse" begin

0 commit comments

Comments
 (0)