1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ebitenでのガベージコレクタ処理のライブラリ化

Last updated at Posted at 2021-12-10

 星はじめさんによる 任天堂switchでのガベージコレクタの処理 をライブラリ化してみました

package rtsGarbageCollection

import (
	"runtime"
	"runtime/debug"
)

// 強制ガベージコレクタの実行
func GC() {
	runtime.GC()
}

// golangのガベージコレクタを動かさない
func GcOff() {
	GC()
	debug.SetGCPercent(-1)
}

// golangのガベージコレクタを動かす
func GcOn() {
	GC()
	debug.SetGCPercent(100)
}

// 削除されたデータが500MBならガベージコレクタを
func GcTimer(){
	var mem runtime.MemStats
	
	runtime.ReadMemStats(&mem)
	
	if mem.HeapAlloc > 500 << 20 { // 500MB
		GC()
	}
}

 rtsGarbageCollection.GcOff() でgolang本体のガベージコレクタを動かさなくして、60fpsのループで60カウントしてタイマー処理を行って、 rtsGarbageCollection.GcTimer() を実行

 削除したデータが500MB以上なら強制ガベージコレクタが動きます

 golangのガベージコレクタが削除されたメモリをため込んでからガベージコレクタを動かすのでガベージコレクタが遅くなる。なので、少しずつガベージコレクタを発動して、ガベージコレクタの処理時間を減らすって事なのだろうな

 とりあえずの実装で、実際のプログラムで動かしてますが、間違っていたら教えてください

参考URL:
https://qiita.com/ueshita/items/e6264508a4dc9738a7cc

https://making.pusher.com/golangs-real-time-gc-in-theory-and-practice/

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?