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

github.com/gogf/gf/v2/util/gvalid: valid binary field,a large amount of memory is consumed #4092

Open
redwolf2019 opened this issue Jan 1, 2025 · 3 comments · May be fixed by #4097
Open
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.

Comments

@redwolf2019
Copy link

Go version

go version go1.23.2 darwin/arm64

GoFrame version

2.7.4

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

package main

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gfile"
	"github.com/gogf/gf/v2/os/gproc"
)

func main() {
	in := &Model{Raw: gfile.GetBytes("/Users/redwolf/Downloads/50MB.zip")}

	if err := g.Validator().Data(in).Run(context.Background()); err != nil {
		g.Log().Fatal(context.Background(), err)
	}

	gproc.Listen()
}

type Model struct {
	Raw []byte `v:"required"`
}

image

When I validate a 50MB []byte field, the actual memory usage approaches 1GB.

What did you see happen?

High memory usage with delayed garbage collection.

What did you expect to see?

It shouldn't consume this much memory.

@redwolf2019 redwolf2019 added the bug It is confirmed a bug, but don't worry, we'll handle it. label Jan 1, 2025
@stardemo
Copy link
Contributor

stardemo commented Jan 1, 2025

debug了一下 看起来是 array = gconv.Interfaces(in.Value)的问题
image

@wlynxg
Copy link
Contributor

wlynxg commented Jan 3, 2025

gvalid 包在对 slice 结构做校验时,会使用 gconv.Interfaces() 函数将 slice 转换为 []interface{} 类型,:

case reflect.Slice, reflect.Array:
var array []interface{}
if gjson.Valid(in.Value) {
array = gconv.Interfaces(gconv.Bytes(in.Value))
} else {
array = gconv.Interfaces(in.Value)
}

gconv.Interface() 在做转换时,对于 []uint8[]byte 会展开转换为 []interface{},由于 []byte 较大,所以在这里会导致内存暴涨:

case []uint8:
if json.Valid(value) {
_ = json.UnmarshalUseNumber(value, &array)
} else {
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
}

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


When the gvalid package verifies the slice structure, it will use the gconv.Interfaces() function to convert slice into []interface{} Type,:

case reflect.Slice, reflect.Array:
var array []interface{}
if gjson.Valid(in.Value) {
array = gconv.Interfaces(gconv.Bytes(in.Value))
} else {
array = gconv.Interfaces(in.Value)
}

When gconv.Interface() performs conversion, []uint8 and []byte will be expanded and converted into []interface{}, because []byte is larger, so it will cause memory explosion here:

case []uint8:
if json.Valid(value) {
_ = json.UnmarshalUseNumber(value, &array)
} else {
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
}

@wln32 wln32 linked a pull request Jan 3, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants