How to know if my have a memory leak

Memory leaks can happen for many reasons but commonly happen due to poorly managed connections and instances that aren’t disconnected/destroyed when unneeded, generally due to oversights when implementing and using OOP or repeatedly creating connections (eg. while true do loops, creating connections inside of connections etc and never disconnecting), or even parenting many instances to nil and never destroying them. But really, it’s difficult to tell whether you have a memory leak unless you’re playing a game for some time and memory usage becomes very high without a reason. If you’re using object oriented programming in your scripts and these objects use userdatas (such as connections/signals/instances) and you aren’t properly disposing of them, it’s possible. But really, it’s hard to tell without examining your scripts. Also, wrapping your code in do-end blocks is good, it should discard any redundant references, however that being said, ensure you aren’t skipping over any connections that you need referenced, and that you aren’t losing reference to nil instances because they’ll always be in memory and will never be garbage collected unless destroyed.

You should read up on memory leaks and how to identify them, there are far more in-depth and well-written tutorials than what I’m giving you:

If you’re working with connections, I recommend looking into managing them. There are many solutions but the most “famous” one is the maid object by Quenty, it’s easily manageable and object oriented to make life even easier. Also, and I can’t express this enough, if you’re using bindable events, you should really switch to a custom signal class.

That’s not to say you do have a memory leak, an 8000x8000 map is also very large and can be expensive depending on how detailed it is which could be another reason you see an influx in memory, I recommend looking into solutions such as streaming enabled or a proximity-based selective replication system which you can create on your own or look at the ones created by the community.

1 Like