When client sends a request with SendRequest, SendRecv, Post, ... functions, then it receives request data structure as an interface. Somewhere a byte slice is created for sending. When that slice is created, the source structure is not needed any longer. Still, it is kept till the function returns. It cannot be freed, because there are references to the struct memory area. So the GC won't free that.
A possible solution is to check whether the received request interface is a pointer to a pointer. If that is the case, then the data can be freed, actually.
req := f() // Pointer to struct.
client.Post(ctx, target, &req, &resp) // Pointer to pointer passed.
Somewhere in the depths of sending functions req could be nilled.
It is important to see that moving the data from stack to heap may be counterproductive. Function f() in the above example may better rely on escape analysis.
When client sends a request with
SendRequest
,SendRecv
,Post
, ... functions, then it receives request data structure as an interface. Somewhere a byte slice is created for sending. When that slice is created, the source structure is not needed any longer. Still, it is kept till the function returns. It cannot be freed, because there are references to the struct memory area. So the GC won't free that.A possible solution is to check whether the received request interface is a pointer to a pointer. If that is the case, then the data can be freed, actually.
Somewhere in the depths of sending functions
req
could be nilled.It is important to see that moving the data from stack to heap may be counterproductive. Function
f()
in the above example may better rely on escape analysis.