Jump to content

Understanding memory leaks


Recommended Posts

Just trying to get it clear.....

GLOBAL.x = SpawnPrefab("flower")GLOBAL.x:Remove()

​So prefab was created and then was deleted. But variable x still exists and points to that object. Is it memory leak in my mod?

 

Unless you are constantly creating objects in a global variable you shouldn't be creating a memory leak, however, you can check by monitoring the memory usage after you have ran your mod a while. If you are constantly getting increases in memory usage without decreases in memory usage then yes, you have a memory leak. Otherwise you have no memory leak.

Link to comment
Share on other sites

  • Developer

Just trying to get it clear.....

GLOBAL.x = SpawnPrefab("flower")GLOBAL.x:Remove()

​So prefab was created and then was deleted. But variable x still exists and points to that object. Is it memory leak in my mod?

Yes, it is. If I remember correctly, LUA have a garbage collector when the value become unreachable. But here, x still references it. So it's not garbage collectable.

Add a GLOBAL.x = nil could help :)

Link to comment
Share on other sites

Yes, it is. If I remember correctly, LUA have a garbage collector when the value become unreachable. But here, x still references it. So it's not garbage collectable.

Add a GLOBAL.x = nil could help :-)

 

To add to what Diabu said:

 

A memory leak only occurs if you are constantly creating variables without releasing the resources, however, if you use the same global variable over and over it will just rewrite the memory section of the program with the new information.

 

Example:

while true doGlobal.x = SpawnPrefab("flower")Global.x:Remove()end

In this type of situation you aren't actually creating a memory leak because you are constantly overwriting the information stored in Global.x and you're removing the prefab which was spawned (making it nil by using :Remove())

 

 

Here is a quote of what I'm specifically talking about:

 

 

many people that use GC'd languages think that memory leaks are impossible, that garbage collection is a panacea.

 

The reality is that memory leaks still happen, but rather than the explicit leaks that you get with C/C++/Pascal applications you get implicit leaks (*) which happen when you forget to release a reference to an object you no longer need. These unwanted objects loiter in the memory space, using memory and referencing other objects that could also be freed.

 

Reference: http://lua-users.org/lists/lua-l/2007-04/msg00520.html

 

However, given the information you have provided we cannot fully say that it is indeed a memory leak; we can only say that you have not released the resources which could result in a memory leak.

Edited by Kzisor
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...