Page 1 of 1

Delete local : good practice

Posted: Fri Jul 15, 2011 11:35 pm
by bangkok
I use LiveCode with a database from which I pull off thousands of records.

I started to look around "delete local", in order to clean up the memory.

What is the best practice ?

To declare :
local myVar
delete local myVar

at the beginning of my handler.

Or at the end, when I don't need the variable anymore ?

Or both ?

Something bothers me. I read in the dictionnary " Local variables that are used within a handler are automatically deleted when the handler in which they are used exits."

However, in the compiled version of my app, I've noticed after doing many queries that the memory used is increasing. That contradicts what is written in the dictionnary, no ?

Re: Delete local : good practice

Posted: Sat Jul 16, 2011 9:02 am
by jmburnod
Hi bangkok,
Something bothers me. I read in the dictionnary " Local variables that are used within a handler are automatically deleted when the handler in which they are used exits."
I think that is for local variable without declaring

Best
Jean-Marc

Re: Delete local : good practice

Posted: Sat Jul 16, 2011 10:33 am
by Klaus
Hi bangkok,

a script <> a handler!

A script can consist of more than one handler and/or function.
So any (un-declared) local variable is being forgotten after the handler has finished:

Code: Select all

on mouseup
  put 10 into tWhatever
  answer tWahtever
end mouseup
## At this point, tWhatever is history :D 
I would simply EMPTY local variables instead of deleting them.

Best

Klaus

Re: Delete local : good practice

Posted: Sat Jul 16, 2011 11:34 am
by bangkok
Klaus wrote: A script can consist of more than one handler and/or function.
So any (un-declared) local variable is being forgotten after the handler has finished:

Code: Select all

on mouseup
  put 10 into tWhatever
  answer tWahtever
end mouseup
## At this point, tWhatever is history :D 
I would simply EMPTY local variables instead of deleting them.
I'm really confused.

In my script, with a mouseup, I read large quantity of data. It goes into a variable. This var is not declared. So following what you say, at the "end mouseup" it would be cleared from memory, right ?

In my compiled application, this is not what I saw (via CTRL ALT DEL). The more i clicked on the button, the more the memory used by the application increased.

This is why, I started to look around about this issue. And then I changed the script with a "local var" followed by a "delete local var" at the beginning of the script. I

I made the test again. The memory used stayed constant.

Re: Delete local : good practice

Posted: Sat Jul 16, 2011 11:42 am
by Klaus
Hi bangkok,
bangkok wrote:...In my script, with a mouseup, I read large quantity of data. It goes into a variable. This var is not declared. So following what you say, at the "end mouseup" it would be cleared from memory, right ?
Yep, this is how it is supposed to be, I never examined this for correctness 8)

If this is definitively not the case, then this should be bug-reported: bugs@runrev.com


Best

Klaus

Re: Delete local : good practice

Posted: Sat Jul 16, 2011 4:04 pm
by jmburnod
Hi bangkok,
...In my script, with a mouseup, I read large quantity of data. It goes into a variable. This var is not declared. So following what you say, at the "end mouseup" it would be cleared from memory, right ?
For me (OSX)

Code: Select all

on mousedown
      put 3 into tWhatever
end mousedown

on mouseUp
   put tWhatever
end mouseUp
tWhatever don't exists after mouseup
and

Code: Select all

local tWhatever
on mousedown
      put 3 into tWhatever
end mousedown

on mouseUp
   put tWhatever
end mouseUp
tWhatever = 3 after mouseup

Best

Jean-Marc

Re: Delete local : good practice

Posted: Sun Jul 17, 2011 4:09 am
by jacque
Local handler variables are cleared after the handler ends. Garbage collection however doesn't occur until the next idle period, so the memory may be in use for a short time until that happens. If you have other handlers running in the background or anything else that is occupying the engine's time, it won't get around to clearing the memory until other pending messages have cleared the queue.

Another possible mistake is using the same variable name for a script-local or global variable. This is unlikely if you have explicit variables turned on in the script editor, but if you don't, then in some cases it can occur. Be sure your local handler variables are uniquely named.

Re: Delete local : good practice

Posted: Sun Jul 17, 2011 8:42 am
by bangkok
Sorry guys... I should have told you before : i think the issue is related to Datagrids.

I'm pulling off data from a DB, and put the data into a Datagrid.

Here is a small stack, so you can test yourself. It generates large text and put it into a DG. Very simple.

Without "local myvar" and "delete local myvar"... the memory used is increasing, each time you press the button.

-create a standalone (I did my tests on Windows)

-Open the task manager in order to check the memory used by the process

-then press a dozen of times or more

Re: Delete local : good practice

Posted: Sun Jul 17, 2011 9:31 am
by jmburnod
Hi bangkok,
Without "local myvar" and "delete local myvar"... the memory used is increasing, each time you press the button.
Is there a way to get the size of a variable in LC (i understand you have a "task manager" for windows)
I'll do a new topic: "get the size of a variable"

Best

Jean-Marc

Re: Delete local : good practice

Posted: Sun Jul 17, 2011 11:33 am
by jmburnod
Hi Bangkok,

If you need know if a variable exists the syntax "there is" work also with variable
It is not in the dictionary but it work for me

Code: Select all

on mousedown
      put 3 into tWhatever
end mousedown

on mouseUp
   if there is tWhatever then
      put tWhatever
   else
      put "tWhatever don't exists"
   end if
end mouseUp
Best

Jean-Marc