CPU resource consumption causes and solutions

CPU resource consumption causes and solutions

Object creation

* Lightweight objects instead of weight objects

* Controls that do not need to respond to touch events: CALayer display

* Object does not involve UI operation, try to put into the background thread creation

* Controls containing CALayer can only be created and manipulated in the main thread

* When you create a view object through Storyboard, its resource consumption will be much larger than creating objects directly through code. In a performance-sensitive interface, storyboard is not a good technology choice.

* Try to postpone the creation of the object as much as possible, and spread the creation of the object to multiple tasks.

* The object's multiplexing cost is less than the release, creating new objects, such objects should be reused in a buffer pool

Object adjustment

* CALayer: CALayer does not have a property inside. When the property method is called, it internally adds a method to the object through the runtime resolveInstanceMethod, and saves the corresponding property value to an internal Dictionary, and also informs the delegate to create an animation. And so on, it is very resource consuming.

* UIView's display-related attributes (frame/bound/transform) are actually CALayer attribute mappings, so when adjusting these UIView properties, the resources consumed are much larger than the normal ones, so try to reduce Unnecessary property modification

* When the view hierarchy is adjusted, there will be many method calls and notifications between UIView and CALayer. Therefore, you should avoid adjusting the view hierarchy and adding and removing views.

Destruction of objects

The object is captured in the block, and then thrown into the background queue to send a random message to avoid compiler warnings, the object can be destroyed in the background thread.

Layout calculation

* Calculation of view layout is the most common place to consume CPU resources in App

* The background thread calculates the view layout in advance and caches the view layout

* Layout of views with any technique will eventually fall on the adjustment of UIView.frame/bounds/center and other attributes. It is very resource intensive, so try to calculate the layout in advance and adjust the corresponding attributes once when needed. Instead of doing it frequently, calculate and adjust these attributes.

Autolayout

Do not manually adjust the frame and other attributes, you can use the common shortcut properties: left/right/top/bottom/width/height, or use ComponentKit, AsyncDisplayKit and other frameworks

Text rendering

* All text content controls are laid out at the bottom by CoreText and plotted as Bitmaps.

* Common text controls (UILabel, UITextView) whose layout and drawing are performed on the main thread. When displaying a large amount of text, the CPU's pressure will be very large.

* Solution: custom text control, asynchronous drawing of text with TextKit or underlying CoreText

* After the CoreText object is created, it can directly obtain the width and height information of the text, avoiding multiple calculations (counting the UILabel once it is resized, and UILabel drawing it internally again). The CoreText object occupies less memory and can be cached for later use. After multiple renderings.

Decoding of pictures

* When creating a picture using UIImage or CGImageSource, the picture data will not be decoded immediately. The image is set to UIImageView or CALayer.contents, and the CALayer will be decoded before the data is submitted to the GPU. _ occurs in the main thread, unavoidable.

* The background thread first puts the picture in the CGBitmapContext and then creates the picture directly from the Bitmap.

Drawing of the image

* Image Draw: Draws the image into the canvas in a method that starts with CG, then creates an image from the canvas and displays: [UIView drawRect:]

* The CoreGraphic method is usually thread-safe, and the drawing of the image can be placed in a background thread

GPU resource consumption causes and solutions

GPU: Receives the submitted textures and vertex descriptions (triangles), applies transforms, blends and renders them, and outputs them to the screen.

What you see: textures and shapes (triangle simulated vector graphics)

Texture rendering

* All Bitmaps, including images, text, and rasterized content, are ultimately submitted to memory from memory, bound to GPU Texture.

* The process of submitting to the video memory, GPU adjustment and rendering the Texture process, will consume a lot of GPU resources

* When displaying a large number of pictures in a relatively short period of time (TableView has a lot of pictures and slides quickly), the CPU occupancy is very low, the GPU occupies a very high level, and the interface still drops frames.

* Minimize the display of large numbers of pictures in a short period of time. Combine as many pictures as possible for display.

* When the picture is too large and exceeds the maximum texture size of the GPU, the picture needs to be preprocessed by the CPU first, which will bring additional resource consumption to the CPU and the GPU. iPhone4S and above models, texture size limit 4096*4096

Mix of views

* When multiple views (CALayer) are displayed together, the GPU will first mix them together. If the view structure is too complex, the hybrid process will also consume a lot of GPU resources.

* Applications should minimize the number and level of views, and mark opaque attributes in opaque views to avoid useless alpha channel synthesis.

* Pre-render multiple views into one picture to display.

Generation of graphics

* CALayer's borders, rounded corners, shadows, masks, and CASharpLayer vector graphics typically trigger offscreen rendering, while off-screen rendering smoothness occurs on the GPU.

* When a large number of filleted CALayer appears in a list view and slide quickly, it can be observed that the GPU resource is full and the CPU resource consumption is low. The interface can still slide normally, but the average number of frames will drop to very low

* To avoid this situation, you can try to open the CALayer.shouldRasterize (rasterization) property, but this will pass the original off-screen rendering operation to the CPU.

* Fillet image occlusion

* Paint the graphics to be displayed in the background thread as images, avoiding the use of rounded corners, shadows, masks and other attributes.

Perkins 201-400KW Diesel Generator

201-400KW Diesel Generator,Perkins Soundproof Generator,Perkins Super Silent Type Diesel Generator,Perkins Super Silent Power Generator

Shanghai Kosta Electric Co., Ltd. , https://www.ksdpower.com