Performance Hit on Restart

Everytime I restart my endless runner gamer (either thru menu item or death) the performance takes a major hit. I assume this is due to some memory mishandling of sometime. So a few questions:

If I have a table that I set to nil, are all its children automatically garbage collected?  Or must I iterate thru the table and then set it to nil?  Or can I just set the table to new value e.g 

table={}

table[1]=“asdasd”

table={}

Also what happens I reintialize a display object without removing or nilling it? As in:

car=display.newImage(–blah–)

car=display.newImage(–blah2–)

Will this cause problems?

table={}

table[1]=“asdasd”

table={}

Isn’t really nilling the table.  First don’t use “table” as a filename.  It’s the internal table object and you will overwrite and loose table.insert(), etc.

Secondly the first line above creates a new table and assigns the address of the memory for that table to your variable.  Line 2 adds some things to the table allocating some memory.

The third line, however overwrites the variable with the address of a new empty table.  To see this in action run this code and look at the terminal window:

local t = {}
print(t)
local t = {}
print(t)

See that it prints:

table: 0x7f87cbd05260
table: 0x7f87cbd05300

The addresses are different.  When this happens you have created a memory leak.  Your code no longer holds a reference to the original block of memory.  On iOS with Automatic Reference Counting, it may not leak, but it’s safer to set it to nil to make sure it gets freeded.

car=display.newImage(–blah–)

car=display.newImage(–blah2–)

Is the same problem.  You loose your reference to the first image.  This is a bit worse because the images take up much bigger chunks of memory.

Rob

table={}

table[1]=“asdasd”

table={}

Isn’t really nilling the table.  First don’t use “table” as a filename.  It’s the internal table object and you will overwrite and loose table.insert(), etc.

Secondly the first line above creates a new table and assigns the address of the memory for that table to your variable.  Line 2 adds some things to the table allocating some memory.

The third line, however overwrites the variable with the address of a new empty table.  To see this in action run this code and look at the terminal window:

local t = {}
print(t)
local t = {}
print(t)

See that it prints:

table: 0x7f87cbd05260
table: 0x7f87cbd05300

The addresses are different.  When this happens you have created a memory leak.  Your code no longer holds a reference to the original block of memory.  On iOS with Automatic Reference Counting, it may not leak, but it’s safer to set it to nil to make sure it gets freeded.

car=display.newImage(–blah–)

car=display.newImage(–blah2–)

Is the same problem.  You loose your reference to the first image.  This is a bit worse because the images take up much bigger chunks of memory.

Rob