# Question about GC.@preserve

**URL:** https://discourse.julialang.org/t/question-about-gc-preserve/131999
**Category:** General Usage
**Tags:** question, garbage-collection
**Created:** [September 1, 2025, 6:07am UTC](https://discourse.julialang.org/t/question-about-gc-preserve/131999 "2025-09-01T06:07:03Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)
#### Post date: [September 1, 2025, 6:07am UTC](https://discourse.julialang.org/t/question-about-gc-preserve/131999/1 "2025-09-01T06:07:03Z")

</div>

Consider the following two patterns:

```julia
v = Vector(...)
GC.@preserve v begin
   # unsafe stuff using pointer(v) 
end

```

and

```julia
v = Vector(...)
# unsafe stuff using pointer(v) 
GC.@preserve v

```

Are there situations where the second pattern might cause trouble?

---

<div class="post-metadata">

### Author: ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)
#### Post date: [September 1, 2025, 9:31am UTC](https://discourse.julialang.org/t/question-about-gc-preserve/131999/2 "2025-09-01T09:31:30Z")

</div>

Yes. The second pattern is incorrect.

For an example that doesn’t depend on reasoning about compilation: Imagine a future version of Julia with a moving GC. (Objects can be moved to make better use of memory) Then the address of an object at any given time is not stable, if you need to observe and use the address, you must therefore use GC preserve to ensure that the object has a stable address while you are using the pointer.

To be clear this future version of Julia doesn’t exist right now, but folks are working towards that.

But even today that version is not correct, due to optimizations.
