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

Memory leak in Process(), Convert() #440

Open
rvishrm opened this issue Jan 23, 2023 · 3 comments
Open

Memory leak in Process(), Convert() #440

rvishrm opened this issue Jan 23, 2023 · 3 comments

Comments

@rvishrm
Copy link

rvishrm commented Jan 23, 2023

Hi, the following code is a simplified version of my code. Here, I'm trying to do some image processing on the image buffer.

package main

import	"github.com/h2non/bimg"

func resizeAndConvertImage(image []byte, height int, width int) ([]byte, error) {
		size, err := bimg.NewImage(image).Size()
		if err != nil {
			return nil, err
		}
	
		if width == 0 && height == 0 {
			width = size.Width
			height = size.Height
		}
	
	    options := bimg.Options{
			Width:  width,
			Height: height,
		}
		imgType := bimg.JPEG
		image, _ = bimg.NewImage(image).Process(options)
		image, _ = bimg.NewImage(image).Convert(imgType)
		return image, err
}

func main() {
	newImage, err := resizeAndConvertImage(imageBuffer, height, width)
}

I used a script for hitting so many requests to the server.

I started the script at 11:31 and stopped it at 11:38. It can be clearly seen that around 2GB of memory is stucked somewhere.

Screenshot 2023-01-23 at 12 50 55 PM

Then I tried commenting Process() and Convert() functions one by one.

//  image, _ = bimg.NewImage(image).Process(options)
//  image, _ = bimg.NewImage(image).Convert(imgType)

And graph showed around 1GB of memory leakage. (script start time is 11:53)
Screenshot 2023-01-23 at 12 41 56 PM

Then I commented both the function calls.

//  image, _ = bimg.NewImage(image).Process(options)
//  image, _ = bimg.NewImage(image).Convert(imgType)

And the memory graph shows that there is no memory leakage. Memory used is just in MBs.
Screenshot 2023-01-23 at 12 46 28 PM

Am I missing something obvious here?

I have also tried following options for the fix

   os.Setenv("GODEBUG", "madvdontneed=1")
	bimg.VipsCacheSetMax(0)
	bimg.VipsCacheSetMaxMem(0)
	os.Setenv("MALLOC_ARENA_MAX", "2")
@piyushRazorpay955
Copy link

Facing the same issue in process and convert function.

@fcuenca4
Copy link

have you tried setting MALLOC_ARENA_MAX=2 ?

@DundieWinner
Copy link

Any solution for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants