Delete local : good practice

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Delete local : good practice

Post by bangkok » Fri Jul 15, 2011 11:35 pm

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 ?

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Delete local : good practice

Post by jmburnod » Sat Jul 16, 2011 9:02 am

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
https://alternatic.ch

Klaus
Posts: 14208
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Delete local : good practice

Post by Klaus » Sat Jul 16, 2011 10:33 am

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

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Delete local : good practice

Post by bangkok » Sat Jul 16, 2011 11:34 am

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.

Klaus
Posts: 14208
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Delete local : good practice

Post by Klaus » Sat Jul 16, 2011 11:42 am

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

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Delete local : good practice

Post by jmburnod » Sat Jul 16, 2011 4:04 pm

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
https://alternatic.ch

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7394
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Delete local : good practice

Post by jacque » Sun Jul 17, 2011 4:09 am

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Delete local : good practice

Post by bangkok » Sun Jul 17, 2011 8:42 am

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
Attachments
TEST.zip
(5.91 KiB) Downloaded 251 times

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Delete local : good practice

Post by jmburnod » Sun Jul 17, 2011 9:31 am

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
https://alternatic.ch

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Delete local : good practice

Post by jmburnod » Sun Jul 17, 2011 11:33 am

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
https://alternatic.ch

Post Reply