Caching and Purgeable Memory (translated)

Source: Internet
Author: User

Caching and purgeable memory are a critical resource for developers, especially when we need to deal with objects that require very large memory and computational time, or when a computer writes data to a disk that causes the application to stall.

I. Overview of Caching

Caching is a collection of objects or data that can significantly improve application performance.

1. Why Use Caching

Developers can use caches to store objects that require frequent access, and these objects contain data that requires a lot of CPU time to calculate. Because these values do not require repeated calculations, reusing these data objects can help provide our application performance. However, for applications, these objects are not critical and can be released when memory is tight. If these objects are freed, then the values it contains are recalculated when needed.

2. Problems that may arise from caching

Although caching can bring great benefits from a performance perspective, it also has some drawbacks. Most importantly, the cache uses a lot of memory space. When we cache a large number of large data objects, it may result in not having enough memory for other applications, at which point the computer may start a page replacement (OSX) or notify other applications to free up memory (IOS) to gain memory space.

3. Solutions

The COCOA framework provides a Nscache object to manage any of our objects that require a cache, which is an object container. Nscache object classes are very similar to Nsdictionary object classes, and they all have key-value pairs. The difference is that the Nscache object is an "active cache", which means that when we have enough memory, he caches enough objects. When there is not enough memory, it automatically cleans up some of the objects to free up memory space for use by other applications. Then, if we need to use these destroyed objects again, the values of these objects will be recalculated.

Nscache provides two useful throttling features: Limit the number of cache entries and limit the total memory consumption of cache entries. setCountLimit:you can limit the number of cache entries by calling. For example, if we try to cache a 11th object into a nscache with only 10 cache entries, Nscache may automatically destroy one of the objects.

When we add a cache entry to the cache, we can setTotalCostLimit: set the maximum amount of memory for all objects that can be cached by specifying a cost value to the cache. When the cache exceeds this limit, he may automatically destroy some of these objects so that the cache value is lower than the maximum memory size limit. This automatic destruction processing mechanism does not guarantee performance, and attempting to manipulate this value sometimes results in a performance overhead, which may not be enough to compensate for the benefits of using caching. Of course, if your cached data is not very useful, pass in 0, of course, you can also use setObject:forKey: this KVC way to set this value. Passing 0 does not bring any overhead.

The number of cached items and the total amount of cache is not an absolute indicator, that is, when the available system resources tend to become tense, it may be possible to prune an object without necessarily deleting it. The order in which the objects are deleted is determined by the specific implementation. This is especially true: it is not a good idea to force the cache to first prune some objects by adjusting the cost values.

Second, the use of purgeable Memory

The Cocoa Framework provides Nspurgeabledata object classes to help applications not use too much memory space. Nspurgeabledata adheres to the Nsdiscardablecontent protocol, and any object class that implements the protocol can allow memory to be freed after other objects have finished using the object instance. We should implement this protocol when we create objects that have a lot of discretionary child controls. In addition, Nspurgeabledata objects do not necessarily have to be used with the Nscache object class. We just need to use it one to get to the purgeable feature.

1, the advantage of using purgeable memory

By using purgeable memory, we can improve performance by quickly recovering the ram when the system needs it. Because page substitution is a time-consuming operation, the memory that is marked as purgeable is not swapped out to disk when it is reclaimed by the virtual memory system (VMS). Instead, the memory data consumed by these objects is destroyed, and is calculated again if needed later. (purgeable memory is not replaced by memory not enough to backing store, only destroyed,)

Note that if we use purgeable memory, it will be locked before it is used. This locking mechanism is necessary to ensure that the memory data that you want to access later is not released because of the auto-recycle mechanism. Similarly, this locking mechanism page ensures that the virtual memory system does not destroy this part of the data. The NSPurgeableData object class uses such a simple locking mechanism to ensure that the data is safe from reading.

2. How to Realize purgeable Memory

  NSPurgeableDataThe use of object classes is simple, and the object class implements only the NSDiscardableContent protocol. For NSDiscardableContent the life cycle of an object, the core idea is the reference count. When an object uses a block of memory, the reference count for this area of memory is >=1. When it is not in use and can be destroyed, its reference count = 0.

When the reference count becomes 0 o'clock, the memory is recycled in case of a tight memory. To free up this area of memory, by invoking the L method of the object discardContentIfPossible , we can release this memory area with a reference count of 0.

By default, when an NSPurgeableData object is initialized, its reference count variable value is 1 and can be accessed safely. In order to access this purgeable memory, only the beginContentAccess method can be called. This method first checks whether the object's data is destroyed. If the data is still there, it will increase the reference count of the memory that the object points to, and return yes. If the object's data has been destroyed, this method returns No. When we have finished accessing the object, the method is called, which endContentAccess reduces the reference count of the memory area, and allows the internal presence to be released. Only when the beginContentAccess method returns Yes can we access the memory space that the object points to.

When the system's available memory is reduced, the system or client object discardContentIfPossible destroys the purgeable data by calling the method, and when the object points to a memory reference count of 0 o'clock, this method simply destroys the data above and then does no other action. If the memory is destroyed, then the isContentDiscarded method returns Yes. Here is an NSPurgeableData example of using an object:

1Nspurgeabledata * data =[[Nspurgeabledata alloc] init];2 3[Data endcontentaccess];//Don ' t necessarily need data right now, so mark as discardable.4 5 //maybe put data into a cache if you anticipate it later to need it.6 ...7 8 if([Data begincontentaccess]) {//YES If data have not been discarded and counter variable have been incremented9 Ten      ... Some operation on the data ... One  A[Data endcontentaccess]//Done with the data, so mark as Discardable -  -}Else { the  -      //data have been discarded, so recreate data and complete operation -  -data = ... +  -[Data endcontentaccess];//Done with data +  A } at  - //Data is able to being discarded at the if memory is tight
View Code

3. Purgeable Memory and Nscache

When an object that implements the NSDiscardableContent protocol is placed in the Nscache object, the cache object maintains a strong reference to the object. Then, if the object's contents have been destroyed (discard), the cached evictsObjectsWithDiscardedContent value will be set to Yes, and the object will be automatically removed from the cache.

4. Some warnings using purgeable memory

It is important to note that purgeable memory is an object that can be used directly by large-capacity objects. The Purgeable API is also designed as a multi-page virtual memory object, which makes it difficult to mark a very small cache element as purgeable. This cache API will make some necessary records to support the cache elements that will be allowed to use purgeable memory as well. Similarly, sometimes it is inappropriate to allocate memory space through the API for these cache elements, when we can easily create an object, or the object is allocated at different levels, and the layer has already cached it. In this case, it is not appropriate to use purgeable memory.

5. When to use purgeable Memory

When the performance cost of erasing this object is less than the performance overhead of page substitution, we can consider that the performance overhead of using purgeable Memory------page substitution is much larger than the performance overhead of recalculating data values when using this object again. Many times caches do not follow the rules above, and some of their cache entries are unlikely to be reused in the future. Similarly, those cache entries that can easily calculate data can consider using purgeable memory, because recalculating the data does not cost the application much performance.

Report:

Summarize:

1. Using purgeable memory allows us to safely use a certain object, which can be used to determine whether the data on the object's memory has been erased.

2, purgeable memory corresponding to the RAM will not be swapped out to the backing store, when the memory is not enough to be directly erased, the freed memory can be used elsewhere.

The 3.purgeable memory definition can be erased directly without the need for page replacement. All objects in iOS are purgeable, but not all objects can know if the memory they are using is erased, and this must be done with objects that implement the Nsdiscardablecontent protocol for more precise control.

Conjecture: The kernel system maintains a purgeable memory area, which maintains some purgeable page lists, and this type of page also has a mapping relationship to the pages in physical memory, when we release the memory pages of one of the objects, The actual physical memory area is reclaimed by the system for use by the application or other applications, but a list is maintained in the Purgeable memory area to indicate that the object's memory page is currently erased.

Practical use:

1, you can use Nspurgeabledata and NSData, if the memory of an object can be discarded as necessary, then you can implement the method defined by the protocol. This means that when the system resources are tight, the memory of the Nspurgeabledata object can be freed. The Nsdiscardablecontent protocol defines a method called iscontentdiscarded, which can be used to query whether the associated memory has been freed.

2. If you need access to a Nspurgeabldata object, you can call its Begincontentaccess method, telling the system that it should not discard its own memory. When you're done, call the Endcontentaccess method and tell the system to discard the memory it occupies when necessary. These calls can be nested, so they are like the methods used to increment and decrement the reference count. Only objects that have a reference count of 0 o'clock can be discarded.

3. If the Nspurgeabledata object is added to the Nscache, it will be automatically removed from the cache when the object is discarded by the system. This feature can be turned on or off by Nscache's Evictsobjectswithdiscardedcontent property. (an iOS system is forcing the application to clean up memory if there is not enough memory).

4, implement the cache should choose Nscache instead of Nsdictionary object. Because Nscache can provide elegant auto-deletion functionality, it is also thread-safe, and unlike dictionaries, it does not copy keys. Copy the key reference directly.

Caching and Purgeable Memory (translated)

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.