Simple iPhone app uses too much memory (4MB)
Thank you for your Time I'm using:
- XCode 4.0 Build 4A304a
- IOS 4.3.1
- Running on an iPhone 4
I just created a project with the navigation template.
The app does nothing, the table has only one row, and when you tap on this row it checks how much memory is used by the app and writes the result on the detailTextLabel
.
This app uses 4MB. Is this normal?
Here's the code I use to get the memory info:
- (float) reportUsedMemoryInBytes {
struct task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t kerr = task_info(mach_task_self(),
TASK_BASIC_INFO,
(task_info_t)&info,
&size);
if( kerr == KERN_SUCCESS ) {
return info.resident_size;
} else {
NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
return -1.0;
}
}
The label is filled with
cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f Mb",
开发者_如何学编程 [self reportUsedMemoryInBytes] / 1024.0 / 1024.0];
When using Instrument with memory monitor if i scroll down i see my process which is named MemTest.
- In the Real Memory Column stays 4.58 Mb
- In the Virtual Memory Column stays 75.78 Mb
However if i go with Allocations and check the first row all Allocations then
- In the Live Bytes Column stays 631.51 Kb
- In the Overall Bytes Column stays 1.26 Mb
What is now the right amount of Memory this simple App uses ? I want to know only how much memory is used by my App without the stuff that is used by Frameworks and IOS Librarys.
Thanks to every Body for some help.
UIKit
and Foundation
most likely account for most of that size. The RSS reported likely includes shared libraries/frameworks such as these. For discussion of this point, see "Understanding Memory Usage on Linux":
Why ps is "wrong"
Depending on how you look at it, ps is not reporting the real memory usage of processes. What it is really doing is showing how much real memory each process would take up if it were the only process running. Of course, a typical Linux machine has several dozen processes running at any given time, which means that the VSZ and RSS numbers reported by ps are almost definitely "wrong". In order to understand why, it is necessary to learn how Linux handles shared libraries in programs.
Most major programs on Linux use shared libraries to facilitate certain functionality. For example, a KDE text editing program will use several KDE shared libraries (to allow for interaction with other KDE components), several X libraries (to allow it to display images and copy and pasting), and several general system libraries (to allow it to perform basic operations). Many of these shared libraries, especially commonly used ones like libc, are used by many of the programs running on a Linux system. Due to this sharing, Linux is able to use a great trick: it will load a single copy of the shared libraries into memory and use that one copy for every program that references it.
精彩评论