Unity performance optimization 2: memory issues

Table of contents

Memory issues caused by policies

GFX memory

texture resource

Compression format

Mipmap

grid resource

Read/Write

vertex data

skeleton

static batch

Shader resources

Reserved Memory

RenderTexture

animation resources

audio resources 

Font resources

Particle system resources

Mono heap memory


Memory issues caused by policies

1. When the Assetbundle is packaged, a single resource is packaged repeatedly, and the dependent resources can be packaged individually, and the resources in the Assetbundle can be viewed through the AssetBundleBrowser. Address: Unity Technologies GitHub

2. The code is used improperly and the resources are not destroyed in time after loading, such as:

            var gos = Resources.Load<GameObject>("Sphere");
            GameObject go = Instantiate(gos);
            Material ma = go.GetComponent<Renderer>().material;
            ma.color = new Color(1, 0.5f, 0.5f);
            
            //Destroy(ma);
            Destroy(go);

 There will still be a material in the memory, because every time you set the color, a new material is created.

3. Improper unloading of Assetbundle leads to resource redundancy. Unload(false) will unload the loaded assetbundle, but the resources loaded from it are still there. Unload(true) will unload them.

GFX memory

GFX memory (Graphics) refers to the dedicated memory of the graphics processor (GPU), which is used to store graphics data and temporary data required for calculations.

texture resource

Compression format

1. When the image is imported into Unity, it will be converted into a format recognized by Unity, such as jpg, png, which are stored on the hard disk, but cannot be read directly without GPU, so it will be converted into ETC and ASTC when importing Format

2. Benefits of image compression:

Reduce memory usage, bandwidth, and loading time

3. If the compression format of the image is not supported on the platform, it will be converted into RGBA format, that is, it will not be compressed.

4. Android supports ETC/ASTC; IOS supports ASTC/PVRTC; PC supports DTX

5. ASTC can adjust the size of the compression block to adjust the compression ratio, because ASTC uses a fixed 128bit to store a block, the more pixels in the block, the greater the compression ratio, because the storage space shared by each pixel The less, such as 4x4, 6x6, 8x8

Mipmap

1. Advantages and Disadvantages of mipmap

Benefit: Reduced bandwidth Disadvantage: Increased memory

2. Principle

The stored image memory is a geometric sequence, 1, 1/4, 1/16..., the memory sum is 4/3 times the original, that is, the memory has increased by 1/3

3. For 2D interfaces, because the camera distance will not change, mipmap must be turned off. For 3D objects or UI, mipmap can be turned on according to the situation.

4. Change the number of Mipmaps added to the memory through Texture Quality, so as to realize machines with different performance and different configurations.

Modified through Edit-Project Settings-Quality--Textures, it only takes effect for textures with Mipmap turned on, and the memory such as FullRes and HalfRes inside is reduced to 1/4 of the original value.

5. Texture Streamming dynamically changes the number of Mipmaps loaded into memory

It is only valid for dynamically loaded Mipmap textures, such as assetbundle loading. If the texture already exists in the scene and is loaded by loading the scene, Texture Streaming will not take effect because the texture already exists and cannot be changed dynamically.

Texture loading will dynamically load the Mipmap in the ab package in real time based on the position of the object and camera.

Validity conditions: Texture turns on texture streaming and generate Mipmap

The priority of MaxLevelReduction (the most cropped level)>Memroy Budget (the maximum memory occupied by mipmap)

grid resource

Read/Write

The CPU and CPU will each occupy a share of memory. If you do not need to modify the mesh in the code, do not enable this option.

vertex data

There are a lot of vertex data in a grid, such as position, color, tangent, etc., but tangents are generally used when calculating lighting. You can set tangent to none under the model column when importing the model, or you can use projectsetting Turn on optimize mesh data inside, it will crop unused vertex attributes and needs to be tested.

skeleton

If the model does not require skeletal data, set the animation type to none in the RIG column of the model import.

static batch

It will increase memory and exchange space for time.

Shader resources

1. The memory occupied by the shader. As long as it is a shader variant, each variant will generate a shader and load it into the memory.

2. When the game is initialized, it is generally necessary to load all the Shaders used for rendering in advance to reduce the lag caused by timely loading and compilation when the game is running. At this time, we can call Shader.WarmupAllShaders to load the current Shaders into the memory. The Shader is compiled once, including all variants.

3. As the rendering effects of the project become richer, there are more and more Shader variants. Roughly calling the full loading interface will cause the game's startup time to become longer and affect the game experience.

4. Later, Unity added the variant collection ShaderVariantCollection to replace the crude full loading interface above to achieve on-demand loading and improve loading speed.

5. Optimization direction: clipping shader variants

Address: https://answer.uwa4d.com/question/5da86670e84db43d6efbda72
 

Reserved Memory

RenderTexture

Turn off anti-aliasing appropriately, or reduce the quality of anti-aliasing , reduce the quality of shadow maps, that is, the resolution, and reduce the number of RT storage bits (set when the code is generated). If HDR does not use the alpha channel, you can modify the format to R11G11B10 , that is, 32 is the storage format

animation resources

1. Check Resample Code. In the animation column of the model import, it is turned on by default. It will reduce the number of key frames.

2. Compress animation

3. Skeleton animation, without scale, eliminate unused scale curves, through the editor code, AnimationUtility

4. Reduce the float precision of animation storage, so that its storage method is constant and reduce memory

5. Select animation and you can see its detailed information in the properties panel.

audio resources 

1. ForceToMono: Combine two-channel audio into mono

2. LoadType: 

Decompress On Load: After loading, decompress and store in memory uncompressed

Compress in memory: stored in memory in a compressed manner and decompressed during playback

Stream: Play while decompressing, but a cache will be added each time it is played.

For long and loud background music that is played infrequently, use stream, for short and frequent music, use the first type (sound-stealing), and for other medium-level music, use the second type.

3. Compression format

The smaller the compression ratio, the smaller the file memory occupied after compression. ADPCM compression format takes up the smallest memory. Use with Compress in memory
PCM: no compression, Mp3: second, Vorbis: second, ADPCM: smallest

Font resources

1. Font slimming: Some fonts are not used and can be cropped. Recommended tools: FontSubsetGUI, FontPruner

2. Font compression: The fonts generated by TMP are too problematic and cannot be changed. The texture is compressed by extracting its texture, then compressing it, and then assigning it.

Particle system resources

1. The memory occupied by particles is related to the number of actually played particles and has nothing to do with the maximum number of particles.

2. If the particles are not played, they will also occupy part of the memory. For example: the particle system is only closed but not deleted.

Mono heap memory

1. The resident memory is too high: such as list, dictionary, array. The memory during initialization should not be too high.

2. Continuously allocate memory: for every 10,000 frames, allocate no more than 50M, by caching variables during initialization






 


 

Guess you like

Origin blog.csdn.net/qq_37672438/article/details/131958138