Need to do anything with memory?

I’ve got an array A that when it’s filled takes up ~6mb of system memory. i then perform functions on it and write the results to an array B (array B takes up ~8mb of system memory). I then nil the contents of array A and the array itself, however the program prints out ive got ~15mb of system memory used. i then have memory leaks of about 30kb every three seconds and after time, the memory resets to about 8-9mb which seems fine. im just wondering why lua’s garbage collector doesn’t clean up straight away after i free A;

maybe i dont need to have an array B and just rewrite to A once im done with one element i.e. after performing functions on A[1], i can nil A[1] and then rewrite to it from temp variables that recorded the results? (would this show 8-9mb instead of 15mb of system memory usage?)

it feels like i dont have to worry as much because it does eventually reset, but im just a bit confused about how the garbage collector works,

cheers, 

boris

This is normal behaviour. If you want to force a garbage collection simply call collectgarbage()

 i.e. after performing functions on A[1], i can nil A[1] and then rewrite to it from temp variables that recorded the results? (would this show 8-9mb instead of 15mb of system memory usage?)

it likely won’t be that “linear” - you’d still be creating garbage in all those temps (unless the nature of your operations would let you reuse a single temp), tho potentially much less of it without the additional table overhead, so it’ll still take a while to collect it afterwards. (assuming you’re doing this all as a single monolithic operation, and assuming you’re not sprinkling in a collectgarbage() every thousand items or so)  hard to say anything more conclusive without knowing what your “perform functions on it” actually does.

@davebollinger,

array A is basically a zipped package with strings that are parsed and then split into multiple fields for elements of array B; i just included collectgarbage() after freeing up array A and it does reset straight away (you’re assumptions are correct also haha)

Lua doesn’t run garbage collection on a fixed schedule by default. It runs it when it feels it needs to. Garbage collection can be a time consuming operation and if you collect garbage too often it could lead to performance problems.

Having memory increase on it’s own isn’t necessarily a “leak”. It’s something you certainly should be aware off and realize that letting that behavior continue unabated will lead to performance problems but it’s not un-natural for apps to grow through usage.

Rob

This is normal behaviour. If you want to force a garbage collection simply call collectgarbage()

 i.e. after performing functions on A[1], i can nil A[1] and then rewrite to it from temp variables that recorded the results? (would this show 8-9mb instead of 15mb of system memory usage?)

it likely won’t be that “linear” - you’d still be creating garbage in all those temps (unless the nature of your operations would let you reuse a single temp), tho potentially much less of it without the additional table overhead, so it’ll still take a while to collect it afterwards. (assuming you’re doing this all as a single monolithic operation, and assuming you’re not sprinkling in a collectgarbage() every thousand items or so)  hard to say anything more conclusive without knowing what your “perform functions on it” actually does.

@davebollinger,

array A is basically a zipped package with strings that are parsed and then split into multiple fields for elements of array B; i just included collectgarbage() after freeing up array A and it does reset straight away (you’re assumptions are correct also haha)

Lua doesn’t run garbage collection on a fixed schedule by default. It runs it when it feels it needs to. Garbage collection can be a time consuming operation and if you collect garbage too often it could lead to performance problems.

Having memory increase on it’s own isn’t necessarily a “leak”. It’s something you certainly should be aware off and realize that letting that behavior continue unabated will lead to performance problems but it’s not un-natural for apps to grow through usage.

Rob