In iOS, is there a way/ a tool to know how much memory an object uses when it is created while the app is running? Using the console for example.
Instruments, included with Xcode Tools, can show you a lot of interesting information about your running application.
Choose Run->Run with Performance Tool-> Object Allocations.
(Or whichever thing you want to monitor)
Related
This question is a follow up question to the answer of this: instruments-leaks-and-allocations-tvos
The initial question relates to a memory leak I had in a tvOS app, but I think the answer to this question is relevant to iOS/Xcode/Instruments development in general.
The issue was solved by removing a closure inside another closure keeping a reference to a variable in the first closure. This caused a retain cycle.
Question
I really want to understand why I didn't find the issue earlier, let me explain.
How it turned out, I used Instruments the wrong way. My workflow while profiling was the following:
From Xcode, I Run the app on the Apple TV device
Start up Instruments
Attach Instruments it to the launched app
The following was visible in Instruments:
There were some leaks, but nothing that special. The graph didn’t really increase, not even after an hour. I used this method for a long time, and never thought something could be wrong here.
While reading about leaks, profiling etc. I came across some random article where someone was using the Profile button from Xcode (instead of Run). I just thought this was doing the same I was doing: Running the app, starting Instruments and attaching the process. But, magically, this graph appeared:
Clearly there is a memory leak here and using this data, finding the leak was pretty easy to do.
So I really want to know what is the difference between these 2 approaches, but I can't make anything out of it. Anyone :) ?
PS: The screenshots were made 2 minutes after each other with exactly the same codebase, just a different way of connecting Instruments to the process.
The difference between the two approaches is when you choose Profile from Xcode, Instruments records all the memory allocations from the start. When you run the app in Xcode, launch Instruments, and attach the app, you're going to miss all the memory allocations that occurred before you attached the app to Instruments. Choosing Profile from Xcode is the better option if you want to record every memory allocation your app makes because it starts recording from app launch.
To answer your question about why Instruments has different data when you choose Run and Profile in Xcode, I suspect it's because they run different builds of your app. Xcode is initially set to use the Debug build configuration when you choose Run in Xcode and set to use the Release build configuration when you choose Profile. Instruments can show different memory usage for debug and release builds. You can edit your scheme to see the build configurations used for running and profiling.
I am having trouble in getting memory usage of a particular iOS application . Also i want to make sure that what ever output result i am getting is for particular running app on iPhone.
Please check the answer on this thread,
https://stackoverflow.com/a/787535/4030948
here is the way to get used memory bytes for your app.
And for the CPU usage of your application,
please check the answer on this thread,
https://stackoverflow.com/a/8382889/4030948
The quickest way is to select the 6th icon along, on the left panel in XCode.
Make sure you have selected to run your project on the iphone device you are looking for, and keep an eye on these usage monitors as you use the app. A more thorough analysis is available using Instruments, but this is the quickest way.
I recently profiled my app using Xcode VM tracker instrument.I found that app has lot of dirty memory especially performance tool data. So i want to know what are the reasons of the huge dirty memory and performance tool data.
Any help would be appreciated.
Your app takes 51MB to store, when it is suspended. The performance tool itself adds an overhead of 30MB. Which leaves 20MB for your app.
From the listed items, it looks like your app is graphics heavy. In fact, it looks very similar to this post. Which makes me wonder if these objects are still processing or waiting to be released, when the app is suspended.
Alternatively, I wonder if you could free a lot of those animations and images when entering background, and reconstruct them when entering foreground.
Finally, note that Apple recommends removing strong references to images, data from disk and media to reduce dirty memory.
Since I just had the same problem, here is what I found:
The "Performance tool data" entries were from libBacktraceRecording.dylib.
You can disable backtrace recording in the scheme editor.
See the related question Memory leak with “libBacktraceRecording.dylib” in React Native ios application.
I've just kind of been 'winging' it with long tests (for hours) with no crashes, and eyeballing my code for quite a while and made sure that everything looks pretty kosher as far as memory leaks. But should I be using instruments... is it mandatory to do this before uploading to app store?
I think that using Instruments is not only good practice, it's strongly recommended by the iOS development community as a whole. Even if your app seems to run fine, you still may have leaks in other use cases. Test your app thoroughly with Instruments before pushing to the App Store or you may be in for a lot of users on older generation devices complaining that the app crashes.
Some of the most crucial tools:
Leaks
Allocations
Time Profiler
Another suggestion alongside using Instruments is to compile with the -pedantic flag.
In addition to what Yuji said, turn on as many warnings as you can in the build settings, by default these are off.
No.
But at least run "Build & Analyze" in the XCode. It tells you what it can find about the memory leaks, just by analyzing the source code statically. It's basically eye-balling the code by the machine. It's infinitely better than doing that yourself. If there're any warnings issued, fix all of them. It's rare for the static analyzer to give false positives.
Also, running your app with Instruments is helpful to see how it really allocates memories. Sometimes it's fun, too.
I would never publish an app without running Instrument's leak tool.
I often miss a release somewhere. And even if I read the code 200 times I would not find it without Instruments.
My application (DotNET) runs as a plug-in inside a C++ standalone app that exposes a C++/CLI SDK.
It is very easy for my users to generate large amounts of data and I'd like to offer an abort option if the memory consumption of my plug-in + the base application reaches -say- 90% of the legal maximum.
How can I measure the total memory consumption (ideally for both the managed and unmanaged code) and how do I know how much memory windows allows for the current application?
The Process class provides most of this information. I think what you're after would be Process.PrivateMemorySize64.
You should be able to do:
var memoryUsage = Process.GetCurrentProcess().PrivateMemorySize64;
GetProcessMemoryInfo and check the PrivateUsage in the PROCESS_MEMORY_COUNTERS_EX.
Update
Obviously I misred the question and though you want the value from the CLI SDK side of the app. In the managed side, you already got the correct answer.
I recommend a profiling tool: dotTrace works really well.