1

I have a NodeJS server application (standard Node, Express, Socket.io combination). I have a constructor function called Player that's used for various things, and when a user signs out, ideally their associated Player object will have all its references removed so it can be garbage collected. However, as these Player object references are passed around to quite a few modules, stored in various lists, etc., I'm not 100% sure they're being cleaned up as completely as they should be.

Is there a way to track the amount of memory used by a certain class/constructor function in NodeJS, so I can make sure they're being freed properly?

I know you can track overall heap usage from the process instance directly, but that doesn't tell me what's using the memory, so it doesn't help me ensure that this particular object is being cleared.

EDIT If it helps, the repo for the project in question can be found here: https://github.com/IceMetalPunk/Graphite-Node . The Player class is defined in Server/entities.js but used in a bunch of other places.

IceMetalPunk
  • 5,476
  • 3
  • 19
  • 26
  • Looks like this could be a copy of [this](https://stackoverflow.com/questions/1248302/how-to-get-the-size-of-a-javascript-object)?... Either way, the V8 engine is pretty good with memory management, once out of scope, it'll run [this algorithm](https://medium.com/@_lrlna/garbage-collection-in-v8-an-illustrated-guide-d24a952ee3b8)... – JO3-W3B-D3V Jan 04 '19 at 08:44
  • 1
    No, that question is about finding the size of an object you know is in memory. I know how garbage collection works, but I'm just not sure whether my code is actually removing all references to the player objects when it should be. So I'm trying to either be able to track references to all instances of a given class/constructor function, or else track the memory being used by all instances without knowing whether any are actually in memory or not. – IceMetalPunk Jan 04 '19 at 09:07
  • Thanks for clarifying that, could you provide some code? I mean I can't imagine anyone would provide a reasonable answer without knowing how any of the code works, or what it does, or how you're managing your references, etc.. – JO3-W3B-D3V Jan 04 '19 at 09:30
  • 1
    That's the problem: these references are passed around in several places, so it'd be hard to include just the relevant areas. I can edit the original question with a link to the entire Github repo, though, but good luck tracking anything down through there :P That's why I was hoping for a more automated way of tracking these references, hence the question. – IceMetalPunk Jan 04 '19 at 11:36
  • I appreciate the GitHub link, and while I can't give you an in depth answer at this moment in time, I don't mean to sound rude, I have more urgent priorities to attend right now, but I'll provide a potential solution later, this is a scenario where functional style programming is pretty great. Even if I just make an answer on here later, probably also contain a link to a GitHub gist/project... That sound good to you? I totally get what you mean now that I've looked through some of the code, i.e. `players` within a `Chat` class and other places, etc. – JO3-W3B-D3V Jan 04 '19 at 12:03
  • 1
    Absolutely; there's no rush on this. The app works fine and I can continue development, this is more of an optimization assurance for my own peace of mind. I appreciate the help! – IceMetalPunk Jan 04 '19 at 12:14
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/186195/discussion-between-jo3-w3b-d3v-and-icemetalpunk). – JO3-W3B-D3V Jan 04 '19 at 15:00

0 Answers0