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

[Question] How free memory after return Render Download? #1917

Open
badstorm opened this issue Mar 3, 2020 · 2 comments
Open

[Question] How free memory after return Render Download? #1917

badstorm opened this issue Mar 3, 2020 · 2 comments

Comments

@badstorm
Copy link
Contributor

@badstorm badstorm commented Mar 3, 2020

I noticed that when I download a file using Render Download the ram memory usage increase a lot and some time the app crashs.
When I start a simple buffalo app the ram usage is about 7MB. After i call my function the ram is about 850MB. My demo file to be downloaded is about 300 MB.
How I can release memory after the download is finish? I noticed that after some minutes the RAM amount start to shrink in automatic.

Here my example:

  1. Create a new buffalo app:
    buffalo new myapp
  2. Add a UPLOAD_PATH variable in .env file:
    UPLOAD_PATH="/path/to/my-file/folder"
  3. Add this function:
func DownloadFile(c buffalo.Context) error {
	envy.Load()
	privatePath, err := envy.MustGet("UPLOAD_PATH")
	if err != nil {
		return nil
	}
	src := filepath.Join(privatePath, "demo.iso")
	reader, _ := os.Open(src)
	return c.Render(200, r.Download(c, "demo.iso", reader))
}
  1. Add path to function to app.go:
    app.GET("/download", DownloadFile)
  2. Try to download the file with your browser:
    http://localhost:3000/download

Here my RAM status
Before:
before
After:
after

@badstorm

This comment has been minimized.

Copy link
Contributor Author

@badstorm badstorm commented Mar 4, 2020

I'm trying to understand which variable allocates all this amount of space. In the mean time I found this workaround to force reduction memory and prevent app crash. However this solution is not good for production:

...
src := filepath.Join(privatePath, "demo.iso")
reader, _ := os.Open(src)
response := c.Render(200, r.Download(c, "demo.iso", reader)) 

debug.FreeOSMemory()

return response
...
@ironsalsa

This comment has been minimized.

Copy link

@ironsalsa ironsalsa commented Mar 25, 2020

Try adding this after you open the file:

defer reader.Close()

Currently the ioReader isn't closed, which may be causing your memory issues.

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

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.