Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[venus-messager] 使用内存逐渐增加 #5331

Closed
1 of 11 tasks
Tracked by #5332
simlecode opened this issue Sep 30, 2022 · 6 comments · Fixed by ipfs-force-community/sophon-messager#272
Closed
1 of 11 tasks
Tracked by #5332

[venus-messager] 使用内存逐渐增加 #5331

simlecode opened this issue Sep 30, 2022 · 6 comments · Fixed by ipfs-force-community/sophon-messager#272
Labels
C-bug Category: This is a bug P1 High - we should be working on this now or in the immediate future

Comments

@simlecode
Copy link
Collaborator

链服务模块 / Chain Service Components

  • venus
  • venus-auth
  • venus-gateway
  • venus-messager
  • venus-miner
  • 文档 / docs

订单服务模块 / Deal Service Components

  • venus-market
  • 文档 / docs

算力服务模块 / Storage Power Service Components

  • venus-sector-manager
  • venus-worker
  • 文档 / docs

版本 / Version

venus-messager -v
venus message version v1.7.0+git.8dd0f4f

描述 / Describe the Bug

使用的内存逐渐增加,直至程序被kill

日志 / Logging Information

重现步骤 / Repo Steps

抓取的proof:
pprof.venus-messager.alloc_objects.alloc_space.inuse_objects.inuse_space.002.pb.gz

@simlecode simlecode added C-bug Category: This is a bug P1 High - we should be working on this now or in the immediate future labels Sep 30, 2022
@simlecode
Copy link
Collaborator Author

image

@simlecode simlecode mentioned this issue Oct 8, 2022
45 tasks
@LinZexiao LinZexiao self-assigned this Oct 8, 2022
@LinZexiao
Copy link
Collaborator

可以确定内存泄露发生在messager获取tipset更新本地数据的过程中,推测有可能是获取的tipset没有释放导致的,审阅相关代码,目前还不能确定具体位置

@LinZexiao LinZexiao removed their assignment Oct 9, 2022
@diwufeiwen
Copy link
Contributor

大概率是与MessageState中的messageCache *cache.Cache有关,master分支删了这个,就没有一直增长的现象了

@diwufeiwen
Copy link
Contributor

type cache struct {
defaultExpiration time.Duration
items map[string]Item
mu sync.RWMutex
onEvicted func(string, interface{})
janitor *janitor
}
type Item struct {
Object interface{}
Expiration int64
}

这里的: items map[string]Item,value是struct,在delete时并不会回收内存。见下面示例:

package main

import (
	"log"
	"runtime"
)

type Test struct {
	Name interface{}
	Sex  bool
}

var lastTotalFreed uint64
var testMap map[int]Test
var cnt = 100000

func main() {
	printMemStats()

	initMap()
	runtime.GC()
	printMemStats()
	log.Println(len(testMap))

	for i := 0; i < cnt; i++ {
		delete(testMap, i)
	}
	runtime.GC()
	printMemStats()
        log.Println(len(testMap))

	testMap = nil
	runtime.GC()
	printMemStats()
}

func initMap() {
	testMap = make(map[int]Test, cnt)

	for i := 0; i < cnt; i++ {
		testMap[i] = Test{
			Name: "name",
		}
	}
}

func printMemStats() {
	var m runtime.MemStats
	runtime.ReadMemStats(&m)
	log.Printf("Alloc = %v TotalAlloc = %v  Just Freed = %v Sys = %v NumGC = %v\n",
		m.Alloc/1024, m.TotalAlloc/1024, ((m.TotalAlloc-m.Alloc)-lastTotalFreed)/1024, m.Sys/1024, m.NumGC)

	lastTotalFreed = m.TotalAlloc - m.Alloc
}

go run result:
2022/10/13 11:51:50 Alloc = 146 TotalAlloc = 146 Just Freed = 0 Sys = 6548 NumGC = 0
2022/10/13 11:51:51 Alloc = 5244 TotalAlloc = 5305 Just Freed = 61 Sys = 15644 NumGC = 2
2022/10/13 11:51:51 100000
2022/10/13 11:51:51 Alloc = 5245 TotalAlloc = 5306 Just Freed = 0 Sys = 15644 NumGC = 3
2022/10/13 11:51:51 0
2022/10/13 11:51:51 Alloc = 143 TotalAlloc = 5308 Just Freed = 5103 Sys = 15644 NumGC = 4

@diwufeiwen diwufeiwen self-assigned this Oct 13, 2022
@diwufeiwen diwufeiwen moved this to Reviewed in Venus Project Oct 13, 2022
@diwufeiwen
Copy link
Contributor

删除map要想马上被gc内存,需要用替换的方式,创建个新的map,把保留元素加进去,然后赋值给map变量

@diwufeiwen diwufeiwen removed their assignment Oct 13, 2022
@diwufeiwen diwufeiwen moved this from Reviewed to In Review in Venus Project Oct 13, 2022
@diwufeiwen
Copy link
Contributor

据运维反馈,最近master已没有了

Repository owner moved this from In Review to Done in Venus Project Oct 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug P1 High - we should be working on this now or in the immediate future
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants