Code Monkey home page Code Monkey logo

Comments (3)

keks avatar keks commented on June 12, 2024 1

I see, thank you for looking into this. I am pretty sure that I've seen this kind of code produce memory leaks in the past, but that was a while ago and since then the GC has been replaced. I agree this is a non-issue.

from bandchain.

keks avatar keks commented on June 12, 2024

k.SetRollingSeed(ctx, append(rollingSeed[1:], req.GetHash()[0]))

from bandchain.

sorawit avatar sorawit commented on June 12, 2024

I don't think this is an issue. With the way the code is written, rollingSeed's length is always 32. So the slice's cap should never go beyond ~64, and when a new array is allocated, the old array should get freed. Wrote a small POC program to ensure that allocated memory stays constant (despite total alloc keeps growing).

package main

import (
	"fmt"
	"runtime"
	"time"
)

func main() {
	slice := make([]byte, 32)
	for i := 0; i < 1000000; i++ {
		slice = append(slice[1:], byte(42))
		if i%100000 == 0 {
			runtime.GC()
			PrintMemUsage()
			time.Sleep(time.Second)
		}
	}
}

func PrintMemUsage() {
	var m runtime.MemStats
	runtime.ReadMemStats(&m)
	// For info on each, see: https://golang.org/pkg/runtime/#MemStats
	fmt.Printf("Alloc = %v kB", bToKb(m.Alloc))
	fmt.Printf("\tTotalAlloc = %v kB", bToKb(m.TotalAlloc))
	fmt.Printf("\tSys = %v kB", bToKb(m.Sys))
	fmt.Printf("\tNumGC = %v\n", m.NumGC)
}

func bToKb(b uint64) uint64 {
	return b / 1024
}

Output:

Alloc = 66 kB	TotalAlloc = 79 kB	Sys = 68610 kB	NumGC = 1
Alloc = 67 kB	TotalAlloc = 270 kB	Sys = 68674 kB	NumGC = 2
Alloc = 68 kB	TotalAlloc = 461 kB	Sys = 68674 kB	NumGC = 3
Alloc = 68 kB	TotalAlloc = 651 kB	Sys = 68674 kB	NumGC = 4
Alloc = 68 kB	TotalAlloc = 841 kB	Sys = 68674 kB	NumGC = 5
Alloc = 68 kB	TotalAlloc = 1031 kB	Sys = 68674 kB	NumGC = 6
Alloc = 68 kB	TotalAlloc = 1221 kB	Sys = 68674 kB	NumGC = 7
Alloc = 68 kB	TotalAlloc = 1411 kB	Sys = 68674 kB	NumGC = 8
Alloc = 68 kB	TotalAlloc = 1601 kB	Sys = 68674 kB	NumGC = 9
Alloc = 68 kB	TotalAlloc = 1791 kB	Sys = 68674 kB	NumGC = 10

from bandchain.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.