<span id="Label3"></p><p style="text-align: center;"><p style="text-align: center;"><span style="font-size: 18pt;">Memory management and multithreading in the interview</span></p></p><p style="text-align: center;"><p style="text-align: center;"></p></p><p align="left"><p align="left"><span style="color: #ff6600;">Briefly describes the memory management mechanism in OC. The method used for pairing with retain is Dealloc or release, why? How do I use the Dealloc or release method to pair with alloc? readwrite,readonly,assign,retain,copy,nonatomic, atomic, strong, Weak properties? and distinguish between strong (strong reference), weak (weak reference)? What situations use copy,assign, and retain?</span></p></p><p align="left"><p align="left">Management mechanism: a mechanism called reference counting is used to manage objects in Memory. Each object in OC corresponds to their own reference count, which can be interpreted as an integer counter, and the holding count is automatically set to 1 when the object is created using the Alloc method. When you send a retain message to an object, the hold count value increases by 1. conversely, when you send a release message like an object, the hold count value is reduced by 1. When the holding count of an object becomes 0, the object frees up the memory it Occupies.</p></p> <ul> <ul> <li>Retain (reference count plus 1)->release (reference count minus 1)</li> <li>Alloc (request memory space)->dealloc (free Memory Space)</li> <li>Readwrite: indicates both getter and setter (default)</li> <li>Readonly: indicates only getter, no setter</li> <li>Nonatomic: do not consider thread safety</li> <li>Atomic: Thread Operation Security (default)</li> </ul> </ul><p align="left"><p align="left">Setter and getter in Thread-safe situations:</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span>-(nsstring*<span style="color: #000000;"><span style="color: #000000;">) Value {</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #000000;"><span style="color: #000000;">@synchronized (self) {</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">[[_value retain] autorelease]; </span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #000000;"><span style="color: #000000;">}</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) SetValue: (nsstring*<span style="color: #000000;"><span style="color: #000000;">) Avalue {</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #000000;"><span style="color: #000000;">@synchronized (self) {</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #000000;"><span style="color: #000000;">[avalue retain]; </span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #000000;"><span style="color: #000000;">[_value release]; </span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span>_value =<span style="color: #000000;"><span style="color: #000000;">avalue; </span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #000000;"><span style="color: #000000;"> } </span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span>}</pre><p><p></p></p> <ul> <ul> <li>Retain:release the old object, assigns the value of the old object to the input object, and then increases the index count of the input object to 1</li> <li>Assign: Simple assignment, does not change index count (default)</li> <li>Copy: actually create an identical object with different address (retain: pointer copy copy: content Copy)</li> <li>Strong: (under Arc) and (MRC) retain (default)</li> <li>Weak: (under Arc) and (MRC) assign, weak automatically nil when the pointing memory is released, preventing the wild pointer</li> <li>Unsafe_unretained declares a weak application, but does not automatically nil, that is, if the area of memory pointed to is released, the pointer is a wild pointer.</li> <li>Autoreleasing is used to modify the parameters of a function, which is automatically released when the function returns.</li> </ul> </ul><p align="left"><p align="left"><span style="color: #ff6600;">1. @protected of class variables, @private, @public, @package, What are the meanings of the declarations?</span></p></p><p align="left"><p align="left">@private: the scope of action can only be in its own class</p></p><p align="left"><p align="left">@protected: scope in itself class and inherit its own subclass (default)</p></p><pre><pre>@public: the scope is the largest and can be accessed from Anywhere.</pre></pre><p align="left"><p align="left">@package: This type is most commonly used as an instance variable of a framework class, which can be used within the same package and cannot be accessed across packets</p></p><p align="left"><p align="left"><span style="color: #ff6600;">2. What is a thread? What is the process? What are the differences and connections?</span></p></p><p align="left"><p align="left">A program has at least one process, and a process has at least one thread:</p></p><p align="left"><p align="left">Process: one run of a program, with independent memory units during execution, and multiple threads sharing a piece of memory</p></p><p align="left"><p align="left">Thread: a thread refers to an execution unit within a process.</p></p><p align="left"><p align="left">Contact: a thread is the basic constituent unit of a process</p></p><pre><pre>Difference: (1) scheduling: A thread as the basic unit of dispatch and allocation, a process as the basic unit of resources<br>(2) concurrency: not only can concurrent execution between processes, but also concurrent execution between multiple threads of the same process<br>(3) owning a resource: a process is an independent unit that owns resources, and threads do not own system resources, but they can access resources that belong to the Process.<br>(4) overhead: When you create or revoke a process, the overhead of the system is significantly greater than the cost of creating or revoking a thread, because the system allocates and reclaims resources for it.</pre></pre><p align="left"><p align="left">Example: the operating system has multiple software running (qq, office, music, etc.), These are a process, and each process has a lot of threads (such as qq, you can chat at the same time, send files, etc.)</p></p><p align="left"><p align="left"><span style="color: #ff6600;">3, talk about your understanding of multithreading development? Are there several ways to implement multithreading in ios?</span></p></p><p align="left"><p align="left">Benefits: 1. Using threads can put tasks in a program that takes up a long time to work in the background</p></p><p align="left"><p align="left">2. The user interface can be more attractive, such as the user clicked a button to trigger the processing of certain events, you can pop up a progress bar to show the progress of processing</p></p><p align="left"><p align="left">3. The operating efficiency of the program may increase</p></p><p align="left"><p align="left">4 · Threads are useful for tasks such as user input, file read and write, and network send and receive Data.</p></p><p align="left"><p align="left">Cons: 1. If you have a large number of threads, it can affect performance because the operating system needs to switch between Them.</p></p><p align="left"><p align="left">2. More threads require more memory space.</p></p><p align="left"><p align="left">3. The termination of a thread requires consideration of its impact on the Program's Operation.</p></p><p align="left"><p align="left">4. Usually block model data is shared across multiple threads, and it is necessary to prevent thread deadlock situations from Occurring.</p></p><p align="left"><p align="left">Ways to implement Multithreading:</p></p> <ol> <ol> <li>NSObject class Method Nsthreadnsoperationgcd</li> </ol> </ol><p align="left"><p align="left"><span style="color: #ff6600;">4, multi-threaded synchronous and asynchronous difference? How does multi-threaded synchronization work in ios?</span></p></p><p align="left"><p align="left">Async: A simple example is a game in which the game will have images and background music</p></p><p align="left"><p align="left">Synchronization: refers to a thread that waits for the last thread to execute before it starts executing the current thread, toilet</p></p><p align="left"><p align="left">Keyword @synchronized ()</p></p><p align="left"><p align="left">Nsoperationqueue:maxcurrentcount</p></p><p align="left"><p align="left">Nsconditionlock</p></p><p><p>iOS Development--interview (part i)</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service