Monday, September 28, 2020

#1 2010-08-17 07:31:23 am

SuperMacGuy
Member
From:: Amish Country, Lancaster, PA
Registered: 2004-06-23
Posts: 416
Website

Memory leaks in ASOC? Way to handle?

Is there any way to handle memory leaks in an ASOC project? My app, when run using Instruments, seems to have a lot of leaking, and I don't really know how to fix that. In Cocoa I'd look for unreleased objects, I mostly understand how that works. But in Applescript, I never had to worry about it. I just set someVariable to "a value" and later can set someVariable with "a new value" and that was all, I figured AS just did the memory management on its own.

My app has a bunch of bound items, including several text labels that change regularly, a progress spinner, a table view that is updated with new items added as files process, buttons that enable/disable when the app is busy and can't be stopped, etc. The code is over 500 lines so I wasn't going to post it unless someone wanted a specific part (or all). I could also post some pix of my Instruments readout, the entire trace file is +50mb though.

Thanks,
Chris


Scripts for InDesign: http://chris.paveglio.com
ASOC & Cocoa, specializing in InDesign, Photoshop, Ai, FileMaker

Offline

 

#2 2010-08-17 07:43:15 am

Dylan Weber
Member
From:: IL, USA
Registered: 2009-08-07
Posts: 539

Re: Memory leaks in ASOC? Way to handle?

Applescript:

anobject's release()


I am addicted to this site.
Look, over 500 posts! I barley help! :O
My specialty is replacing common AppleScript commands with hard-to-work-with methods.

Offline

 

#3 2010-08-17 07:52:57 am

SuperMacGuy
Member
From:: Amish Country, Lancaster, PA
Registered: 2004-06-23
Posts: 416
Website

Re: Memory leaks in ASOC? Way to handle?

Ah OK, yes that seems so simple.

On a similar problem, what might be happening if, when I start Instruments on my app, I don't even do a single thing and it starts registering leaks after 5 seconds?


Scripts for InDesign: http://chris.paveglio.com
ASOC & Cocoa, specializing in InDesign, Photoshop, Ai, FileMaker

Offline

 

#4 2010-08-17 10:11:58 am

ief2
Member
Registered: 2010-07-23
Posts: 66
Website

Re: Memory leaks in ASOC? Way to handle?

Make check if Garbage Collection is enabled?


Find your magical software at www.wizardsoftware.tk.

Offline

 

#5 2010-08-17 06:14:45 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6460

Re: Memory leaks in ASOC? Way to handle?

If garbage collection is enabled, which it is by default in ASObjC projects, what you see in Instruments is fairly meaningless.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#6 2010-08-27 07:45:53 am

andreb
Member
Registered: 2008-05-22
Posts: 10

Re: Memory leaks in ASOC? Way to handle?

Shane, could you elaborate on that?

Because I am facing the same problem. Just running the ASOC app and not doing anything has the Leaks instrument registering leaks all over the place.

So by "Garbage Collection enabled by default" you mean ASOC code is run garbage collected automatically or that new ASOC projects have the build setting set by default? For my project I think I had to enable it myself.

Also why would the Leaks instruments be meaningless then? Shouldn't it account for garbage collected memory?

The leaks are registerring in +[NSBundle mainBundle] and -[NSBundle(AppleScriptObjectiveC) loadAppleScriptObjectiveCScripts] which highly suggests that the leaks are happing in my ASOC code because main.m calls [[NSBundle mainBundle] loadAppleScriptObjectiveCScripts] before return NSApplicationMain(...)

Switching between GC enabled and non GC in the build settings has the effect that I have leaks in the hundreds (with GC) and about 7 leaks otherwise (without GC).

Offline

 

#7 2010-08-27 04:45:28 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6460

Re: Memory leaks in ASOC? Way to handle?

andreb wrote:

So by "Garbage Collection enabled by default" you mean ASOC code is run garbage collected automatically or that new ASOC projects have the build setting set by default? For my project I think I had to enable it myself.


It's a default setting in the project template. If it's off, you have to do the whole retain/release thing.

Also why would the Leaks instruments be meaningless then? Shouldn't it account for garbage collected memory?


I'm far from an expert on memory management, but as I understand it, in simple terms a garbage-collected app leaks like a sieve and the collector periodically goes around cleaning up the mess. So at any point in time, you might see lots of leaked memory. I would think the real matter of interest would be total memory used over a long-ish period of time.

I suspect that if you want to use Instruments, the GC Monitor would be more useful than Leaks -- it should show the memory being reclaimed.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#8 2010-08-28 04:34:18 pm

andreb
Member
Registered: 2008-05-22
Posts: 10

Re: Memory leaks in ASOC? Way to handle?

Shane Stanley wrote:

It's a default setting in the project template. If it's off, you have to do the whole retain/release thing.


Ah, Ok I originally understood it that way. Just wanted to make sure.

It appears though that with the ASOC template that came with my Xcode 3.2.3 and iOS 4.0.2 SDK, GC is not enabled by default. So you would really have to say anobject's release() at the appropriate times. I just enabled GC myself though.

Shane Stanley wrote:

I'm far from an expert on memory management, but as I understand it, in simple terms a garbage-collected app leaks like a sieve and the collector periodically goes around cleaning up the mess. So at any point in time, you might see lots of leaked memory. I would think the real matter of interest would be total memory used over a long-ish period of time.

I suspect that if you want to use Instruments, the GC Monitor would be more useful than Leaks -- it should show the memory being reclaimed.


That makes a whole lot of sense. I skimmed the Leaks instruments documentation and they didn't mention that bit about GC so I just assumed that it would take care of that automatically. The GC monitor instrument really is favorable in this case. Though I don't really understand how to read the statistics it spits out.

Offline

 

#9 2010-08-28 08:34:22 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6460

Re: Memory leaks in ASOC? Way to handle?

andreb wrote:

It appears though that with the ASOC template that came with my Xcode 3.2.3 and iOS 4.0.2 SDK, GC is not enabled by default.


That's odd, because it is here.


The GC monitor instrument really is favorable in this case. Though I don't really understand how to read the statistics it spits out.


It's a fairly low-level tool. I tend to just leave stuff running, and if there is no problem like ballooning memory requirements, then there is no problem wink


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#10 2010-08-29 03:39:38 pm

andreb
Member
Registered: 2008-05-22
Posts: 10

Re: Memory leaks in ASOC? Way to handle?

Ok I have been playing around a little bit more with Instruments.
This tool is simply awesome!

So there's a GC template... if you go from Xcode via Run > Run with Performance Tool > GC Monitor you will launch your app with that template preselected.

In this template there's a tool called Object Graph - this is the one that you want!
It is basically the same as Leaks just specifically for GC with it's interconnections between objects.
If you select the Object Graph instrument in the timeline, look at the left paramater pane in the mid to bottom corner.

There's a twirl down item there called "Block Filters" with three sub items - "Roots Only", "Exclude Non-Objects" and "User-Defined Only". Tick all three. If you now still see entries in the table view in the middle after all your objects should have been released (say you closed your main window but the app's still running) then you've got leaks.
You need to wait a bit though for the next generational collection to happen (look at the Garbage Collection instrument) before making judgements.

The most important tick marks are "Roots Only" and "User-Defined Only". Because you don't want to have your user-defined root objects hanging around after all your important instances have just been kicked off the heap. If App Delegate stuff is still there somewhere (mind you: not as Root Object!) it should be fine... delegates are not considered strong objects.

Of course, as ASS apps did and ASOC apps initially do tend to go, it's not that big of a problem if you only have one main window and the user needs to quit the app anyway after closing it - the leaked memory will be freed in that case.

Anyway, hope this helps anyone. I can wholeheartedly recommend giving Instruments a whirl.

Offline

 

#11 2010-09-07 08:22:33 am

SuperMacGuy
Member
From:: Amish Country, Lancaster, PA
Registered: 2004-06-23
Posts: 416
Website

Re: Memory leaks in ASOC? Way to handle?

My project did NOT have GC on, so I'll try building it with GC on and see what my users report.

EDIT: Next day, anecdotally, my users report that the GC enabled app now works better than before. smile

Last edited by SuperMacGuy (2010-09-08 07:17:35 am)


Scripts for InDesign: http://chris.paveglio.com
ASOC & Cocoa, specializing in InDesign, Photoshop, Ai, FileMaker

Offline

 

#12 2014-06-25 06:04:40 pm

Pier
Member
Registered: 2014-06-23
Posts: 16

Re: Memory leaks in ASOC? Way to handle?

Hi guys,

I am developing an applescriptobjc app in xcode 5.1 and I am facing memory leaks. In particular the instruments app is registering a leak for [NSBundle(AppleScriptObjectiveC) loadAppleScriptObjectiveCScripts] in the main.m file straight after launch. How can I fix this?? I managed to prevent some other leaks releasing objects created with init, alloc etc in the applescript, but I have no idea how to fix this. What's the problem? A solution? Btw I cannot run Garbage Collector cause I'm using xcode 5.1, so that's not an option for me sad I'm afraid.

Thank you.

Pier

Offline

 

#13 2014-06-25 06:06:52 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6460

Re: Memory leaks in ASOC? Way to handle?

Ignore it. It's not really a leak.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#14 2014-06-26 11:49:35 am

Pier
Member
Registered: 2014-06-23
Posts: 16

Re: Memory leaks in ASOC? Way to handle?

Thank you Shane. But if I may ask, what is all about? I got two other leaks in my agent app, one of them everytime I open the status menu. To be honest I tried to release objects at the end of each handler but when I do it I get a BAD ACCES warning for the main.m file. How to deal with leaks? Thank you in advance.

Btw, my app launches and runs perfectly If I don't try to release anything. It never crushed.

Pier

Last edited by Pier (2014-06-26 11:59:43 am)

Offline

 

#15 2014-06-26 02:08:27 pm

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: Memory leaks in ASOC? Way to handle?

Pier wrote:

To be honest I tried to release objects at the end of each handler but when I do it I get a BAD ACCES warning for the main.m file.


When you're letting ARC handle memory management you may never release an object yourself.

The leak you have discovered happens when the AppleScripts are loaded at launch time, it means it happens once during in the application's entire lifetime. A reason to forget it and it's not directly considered a leak, leaks are generally eating more memory over time. Now it's just some extra unnecessary memory being used where no pointer points to so it can't  be freed later.

Offline

 

#16 2014-06-26 06:12:43 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6460

Re: Memory leaks in ASOC? Way to handle?

DJ's correct, but there's a little bit more to it for ASObjC projects, in that the memory management for ASObjC code (in 10.8+) is all handled by the bridging software. So you could, if you wanted/needed to, use ASObjC in a manually-memory-managed app -- your code would not need to be any different.

Before 10.8, because ASObjC generally relied on garbage collection, the bridging software did no memory management, so using manual memory management would have required the use of retain() and release().


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#17 2014-06-27 07:49:41 am

Pier
Member
Registered: 2014-06-23
Posts: 16

Re: Memory leaks in ASOC? Way to handle?

Thank you guys, but I got a few more questions for you. I understood that I can ignore the initial "leak", but what about the others (two or three more)?? One occurs whenever I open the status menu (why?) and the other when I perfom a selector/action. What can I do if I cannot release objects?? And last questions, how can I decrease the amount of memory allocated at launch? Now it allocates 15MB of memory, but I really don't get why since it's an agent app with one menu and submenu only. Thank you.

Pier

Offline

 

#18 2014-06-27 08:44:13 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: Memory leaks in ASOC? Way to handle?

Pier wrote:

What can I do if I cannot release objects??


Not much. I'm not sure if the AppleScriptObjC framework takes more attention to an object if you make it either empty, null or missing value. It's a long shot but maybe Shane can tell you more about this.


Pier wrote:

And last questions, how can I decrease the amount of memory allocated at launch? Now it allocates 15MB of memory, but I really don't get why since it's an agent app with one menu and submenu only.


Do you have a lot of memory installed? The initial memory of a process depends on the amount of memory installed and the amount of maximum processes that are allowed to run simultaneous. So even for small processes the initial memory can feel like overkill.

And are you sure you haven't included preserved memory or/and virtual memory for the proces? Clear first all the preserved memory before builing/launching the application.

Do you run the process in debug mode? Debug uses much more entry points for debugging like break points, this makes the executable bigger than in release mode.

Do you compile the AppleScript files as read-only? Those are smaller as well (remember, when the application launches the whole script is loaded into the memory).

Do you make use of static libraries? Static libraries, unlike dynamic libraries, are included into you executable. This is a great when you're not sure if the library is installed on other machines but your executable will grow exponentially and eventually using more memory.

Last edited by DJ Bazzie Wazzie (2014-06-27 08:45:50 am)

Offline

 

#19 2014-06-27 05:16:33 pm

Pier
Member
Registered: 2014-06-23
Posts: 16

Re: Memory leaks in ASOC? Way to handle?

The app is execute-only and it's allocating the same memory even when I don't launch it in debug mode.

And are you sure you haven't included preserved memory or/and virtual memory for the proces? Clear first all the preserved memory before builing/launching the application.


I have no idea how to check for this. Can you tell me more? Cause in the leak app I can see there is some VM allocated at some point.

Pier

Offline

 

#20 2014-06-27 06:00:29 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6460

Re: Memory leaks in ASOC? Way to handle?

I was always told not to worry about things that are beyond my control, and I think that's good advice in this sort of situation. 15MB is peanuts, and if you're just talking the bald memory figure, it's pretty meaningless.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#21 2020-03-12 11:15:27 am

maro
Member
From:: Nerima, Tokyo, Japan
Registered: 2004-05-30
Posts: 128
Website

Re: Memory leaks in ASOC? Way to handle?

My new application can share document image via AirDrop, Copy canvas image as background transparent PDF and generate document preview as PNG.

And each of them consume about 50MB. 20 times operation consume 1GB and hold it on.

Is there any way to release them? Is 1GB a peanut or a piece of cake?


I wrote thousands of AppleScript to realize my idea. Natural language interface, voice recognition commander and so on. Though my mother toungue is strange language, Japanese, my most frequently write language is AppleScript. I believe it is for making things easy and powerful.


Filed under: memory management

Offline

 

#22 2020-03-12 04:59:24 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6460

Re: Memory leaks in ASOC? Way to handle?

maro wrote:

Is there any way to release them?



None that I'm aware of. This is a weakness of AppleScriptObjC -- AppleScript releases the memory only when it does its own garbage collection (which in such cases can cause its own problems).

So 1GB is no peanut, but it's the price of admission.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#23 2020-03-12 05:58:48 pm

maro
Member
From:: Nerima, Tokyo, Japan
Registered: 2004-05-30
Posts: 128
Website

Re: Memory leaks in ASOC? Way to handle?

Thank you, Shane. I'll write it to Q&A.


I wrote thousands of AppleScript to realize my idea. Natural language interface, voice recognition commander and so on. Though my mother toungue is strange language, Japanese, my most frequently write language is AppleScript. I believe it is for making things easy and powerful.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)