• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Top level garbage strike
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Top level garbage strike


  • Subject: Top level garbage strike
  • From: Steven Angier <email@hidden>
  • Date: Sat, 11 Jan 2003 00:38:34 +1100

Baffled by the diminishing performance of a script application I worked on
recently, I uncovered what I thought was an interesting garbage-collection
problem.

It seems that some types of top-level objects persist on the stack or heap
indefinitely. Take the following example:

script Something
property someProperty : {}
end script

on InitSomething()
local theInstance
copy Something to theInstance
return theInstance
end InitSomething

StartTicks()
set theTickList to {}
repeat 10 times
set theInstance to InitSomething()
set theInstance to InitSomething()
set theInstance to InitSomething()
set theInstance to InitSomething()
set theInstance to InitSomething()
set theInstance to InitSomething()
set theInstance to InitSomething()
set theInstance to InitSomething()
set theInstance to InitSomething()
set theInstance to InitSomething()
set the end of theTickList to ResetTicks()
end repeat

return theTickList

--> {
2,
6,
12,
15,
20,
26,
31,
37,
44,
51
}

Run it again (without recompiling) and the times continue to grow:

--> {
60,
66,
68,
75,
86,
93,
103,
117,
120,
128
}

Though if you wrap the implicit run handler in a function, no problem:

on PerformLocalisedTest()
StartTicks()
set theTickList to {}
repeat 10 times
set theInstance to InitSomething()
set theInstance to InitSomething()
set theInstance to InitSomething()
set theInstance to InitSomething()
set theInstance to InitSomething()
set theInstance to InitSomething()
set theInstance to InitSomething()
set theInstance to InitSomething()
set theInstance to InitSomething()
set theInstance to InitSomething()
set the end of theTickList to ResetTicks()
end repeat

return theTickList

end PerformLocalisedTest

return PerformLocalisedTest()

--> {
0,
0,
0,
0,
0,
1,
0,
1,
0,
0
}

I presume that the increasing times are due to some internal runtime array
which is not cleared, and that AppleScript has to find/create space at the
end of the array when another object is created.

All this begs the question: when is top-level garbage ever collected? This
sort of thing can bog down a script very quickly. I know from experience
that in the scope of a tight repeat loop contained in a function,
AppleScript generally doesn't collect its garbage until the repeat loop is
finished -- although you can force (or allow) AppleScript to collect garbage
by calling another function from within the repeat loop. Clearly, this
scheme doesn't work at the top-level.

I'm not sure if this is a bug or known and accepted behaviour. Any insights?

AppleScript 1.9.1 / Mac OS X 10.2.3


Steven Angier
Macscript.com
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Top level garbage strike
      • From: Axel Luttgens <email@hidden>
  • Prev by Date: Re: Script to get default browser?
  • Next by Date: Re: Localization
  • Previous by thread: Re: Script to get default browser?
  • Next by thread: Re: Top level garbage strike
  • Index(es):
    • Date
    • Thread