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]: Potential memory leak? #2470

Open
3 tasks done
damianham opened this issue May 17, 2023 · 3 comments
Open
3 tasks done

🤗 [Question]: Potential memory leak? #2470

damianham opened this issue May 17, 2023 · 3 comments

Comments

@damianham
Copy link

Question Description

how can I ensure all memory is freed after a request has been served. My API server has a memory leak that ultimately gets the process killed as the server runs out of memory. The memory leak is in fiber.Ctx.JSON

In all my API requests I return a JSON map from the handler, see the example snippet.

Code Snippet (optional)

package main

import "github.com/gofiber/fiber/v2"
import "log"

func main() {
  app := fiber.New()

  // An example to describe the question
// get the businesses for a user ID
	app.Get("/user/:id/businesses", func(c *fiber.Ctx) error {
		id := c.Params("id")

		businesses, err := services.GetBusinessesForUserID(id)

		if err != nil {
			return err
		}

		return c.JSON(fiber.Map{"result": businesses}) // memory leak here
	})
	

  log.Fatal(app.Listen(":3000"))
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my questions prior to opening this one.
  • I understand that improperly formatted questions may be closed without explanation.
@leonklingele leonklingele changed the title 🤗 [Question]: 🤗 [Question]: Potential memory leak? May 18, 2023
@ReneWerner87
Copy link
Member

fasthttp and fiber are built to preserve and reuse the request and response capsules for the next loop using the concept of sync pools

this saves allocations and makes the application faster

how big is the json you want to write away?
maybe you should stream the json to the response if it is too big ?
#1034 (comment)

there is the possibility to make some settings in fasthttp which might reduce the pool or memory usage
https://pkg.go.dev/github.com/valyala/fasthttp#Server
check
ReduceMemoryUsage, Concurrency

@damianham
Copy link
Author

damianham commented May 19, 2023

The issue isn't that I am writing large json data in a single request - the issue is that memory is not being freed over time so the RSS grows to 1.6GB over a period of 8-10 hours. The example I gave is just 1 route that causes a memory leak. I am going to try this and see what happens

`
app.Get("/user/:id/businesses", func(c *fiber.Ctx) error {
id := c.Params("id")

	businesses, err := services.GetBusinessesForUserID(id)

	if err != nil {
		return err
	}
	result := fiber.Map{"result": businesses}
	c.JSON(result)
	result = nil

	return nil
})`

@nickajacks1
Copy link
Member

@damianham I'm repeatedly curling the server in the first code example, and I'm not able to reproduce the issue. Are you still able to reproduce the problem? Do you recall what version of fiber/go you were using at the time?

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

Successfully merging a pull request may close this issue.

3 participants