# Provide a way to avoid GC safepoint

**URL:** <https://discuss.kotlinlang.org/t/provide-a-way-to-avoid-gc-safepoint/30505>\
**Category:** Native\
**Created:** [June 24, 2025, 7:11am UTC](https://discuss.kotlinlang.org/t/provide-a-way-to-avoid-gc-safepoint/30505 "2025-06-24T07:11:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cpdyj](https://avatars.discourse-cdn.com/v4/letter/c/e68b1a/32.png) [@cpdyj](https://discuss.kotlinlang.org/u/cpdyj)\
**Post date:** [June 24, 2025, 7:11am UTC](https://discuss.kotlinlang.org/t/provide-a-way-to-avoid-gc-safepoint/30505/1 "2025-06-24T07:11:44Z")

</div>

I want to writing a library just like java.lang.ProcessBuilder  
But I found it’s difficult to complete the project in pure Kotlin.  
When I executing the following code, it’s hang…

```kotlin
val pid = fork()
if(pid == 0) GC.collect() else println(pid)

```

The new process(pid != 0) was hang on the GC collection…

Personally I don’t like the fork-exec, I prefer `posix_spawn`. But I found there’s no `posix_spawn_file_actions_addchdir`, which was published in POSIX 2024…

---

<div class="post-metadata">

**Author:** ![jean.chalard](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@jean.chalard](https://discuss.kotlinlang.org/u/jean.chalard)\
**Post date:** [June 28, 2025, 6:21am UTC](https://discuss.kotlinlang.org/t/provide-a-way-to-avoid-gc-safepoint/30505/2 "2025-06-28T06:21:18Z")

</div>

I don’t think it’s really possible to tell what might cause `GC.collect()` to hang from your code ; from what’s on the screen it’s certainly not supposed to. You’d have to investigate more than this.

Also, trivially, why cause `GC.collect()` immediately after `fork()` ? There are not a lot of good reasons to cause a GC at specific points, since it eventually happens when necessary. I don’t think you elucidate why you want to do it at that point, and if the problem is that somehow it hang when you do it at this point and you can’t figure out why, then it becomes a relevant question to ask why do it at all.

---

<div class="post-metadata">

**Author:** ![cpdyj](https://avatars.discourse-cdn.com/v4/letter/c/e68b1a/32.png) [@cpdyj](https://discuss.kotlinlang.org/u/cpdyj)\
**Post date:** [July 4, 2025, 9:46am UTC](https://discuss.kotlinlang.org/t/provide-a-way-to-avoid-gc-safepoint/30505/3 "2025-07-04T09:46:16Z")

</div>

> [@jean.chalard](#):
>
> at specific points, since it eventually happens when necessary. I don’t think you elucidate why you want to do it at that point, and if the problem is that somehow it hang when you do it at this point and you can’t figure out why, then it becomes a relevant question to ask why do it at all.

Because I don’t make sure there’re any unsafe (aka. signal-unsafe) actions during the fork-exec. The `GC.collect()` is just a simple way to reproduce the hang.

---

<div class="post-metadata">

**Author:** ![clovisai](https://avatars.discourse-cdn.com/v4/letter/c/a9adbd/32.png) [@clovisai](https://discuss.kotlinlang.org/u/clovisai)\
**Post date:** [July 5, 2025, 6:12pm UTC](https://discuss.kotlinlang.org/t/provide-a-way-to-avoid-gc-safepoint/30505/4 "2025-07-05T18:12:47Z")

</div>

You may want to report this to [https://kotl.in/issue](https://kotl.in/issue). The forum is meant for community support, not for feature requests.
