Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to know who holds a reference to an object in Go?

I am currently trying to debug a nasty memory leak in our Go code.

What I know:

  • where memory is going (pprof with -base flag)
  • why new memory is being allocated ("reconnect" feature in our code)
  • number of goroutines is not growing (runtime.NumGoroutine())
  • if I do object = nil, memory will be garbage collected (good! but now I have data races with other go-routines that are using this object)

What I don't know:

  • why new memory is not being garbage collected. for that I need to know who holds a reference(s) to an object.

Thank you for your time and any advice!

like image 762
melekes Avatar asked Jan 29 '18 10:01

melekes


People also ask

What is a reference to an object?

What is a reference to an object? It is the address of variable only -- not the method of an object. It is a shallow pointer that contains address of an object. It is the physical address of an object. It is the address where the variables and methods of an object are stored.

Why is it important to understand references?

As you work with objects, it's important to understand references. A reference is an address that indicates where an object's variables and methods are stored. You aren't actually using objects when you assign an object to a variable or pass an object to a method as an argument.

What is a reference in JavaScript?

A reference is an address that indicates where an object's variables and methods are stored. You aren't actually using objects when you assign an object to a variable or pass an object to a method as an argument.

Does go have pass-by-reference?

This post is a response to those debates. To be clear, Go does not have reference variables, so Go does not have pass-by-reference function call semantics. What is a reference variable? In languages like C++ you can declare an alias, or an alternate name to an existing variable.


1 Answers

I can suggest two tools.

Use Go Guru, to see who pointsto or referrers to a pointer. It is integrated with the vim-go plugin I use, I did a post on that here.

Valgrind is a tool for C/C++ but found an article about using it with Go.

like image 186
John Scharber Avatar answered Sep 20 '22 17:09

John Scharber