Root questions U3D-view Unity memory management from Profile, u3d --- unity

Source: Internet
Author: User

Root questions U3D-view Unity memory management from Profile, u3d --- unity

What does this article contain?

This article starts with the Profile component of Unity and explores some differences between memory usage of Unity in the development environment and formal environment,

And provides the best way to control the memory (I think you already know... Prefab), and the reason.

 

Articles to be read in advance

Before or after reading this article, I suggest reading the following articles:

 

Yu Song's

Practical http://www.xuanyusong.com/archives/2405/ of Assetbundle in Unity3D Research Institute

Principle http://www.xuanyusong.com/archives/2373 of Assetbundle in Unity3D Research Institute

 

Wang Wei's

Memory http://onevcat.com/2012/11/memory-in-unity3d/ in Unity 3D

 

Stardust (not sure if it was the original author)

Unity3D memory usage too large solution http://www.cnblogs.com/88999660/archive/2013/03/15/2961663.html

 

Starting from AltasPacker of NGUI

The reason is that I was learning to use NGUI (just a few days before getting started with Unity ...), When I checked the tutorial, I found that it was okay...

When creating the UI for NGUI, you must first create Altas. Do you want to delete the created scatter chart in the project? Will non-deletion affect performance?

Google did not find a satisfactory answer, so I had to Profile myself, so I had the following experiment:

 

1. First, create an empty scenario and run it. Open Profile. Texture (Texture) and use 2.5 MB of memory.

 

2. Add an image without any reference in the scenario. The size of the image after being expanded is 1.5 MB. Run the image and then open the Profile.

 

3. A miracle happened. The memory size occupied by the texture is directly changed to 5.8 MB.

 

5.8 MB-2.5 MB = 3.3 MB a strange number, so I suspect three things at the same time

 

1. Even if the materials are not used at all, if there is an import in the project, the materials will be packaged.

2. Even if the materials are not used in the current scenario, Unity will still load (all the materials will be loaded at Unity startup ??! )

3. Unity reserves a reference for the clip in the memory, 3.3 MB ~ = 1.5MBx2 (??! )

 

If one of the above three points is true (All are wrong), Then Unity will undoubtedly become useless firewood ....

As a result, I began to explain myself infinitely ....

I have found a breakthrough in a post.

Http://answers.unity3d.com/questions/57909/find-unused-assets-in-project.html

This article clearly states that Unity will automatically filter out all unreferenced resources during release, and the entire package resources can be viewed through Log

Find the Log based on the position pointed out in the article, test the above project, and output the Android package (Log in/Users/eran/Library/Logs/Unity/Editor on Mac. log, eran for your username)

 

In this case, the first knot is solved. It seems that Unity is really not so silly... you don't have to worry about whether to delete the thumbnail when using NGUI to create Atlas in the future.

Now that you know that you can only perform the test on the real machine device, you need to repeat the test, but this time it is on the real machine.

 

1. First, create an empty scenario and run it. Open Profile. Texture (Texture) and use the memory of 153.0KB.

 

It can be noted that the memory usage is significantly reduced when running on the real machine, and the overhead is very flat, and there will be no fluctuation like the editing environment.

 

2. Add an image and place it on the scene

Although it is not 1.5 MB as expected, since it is less than 3 MB, Unity3D certainly cannot have a backup of memory (DRAM is a copy of VRAM, DRAM is used to re-upload the GPU when processing LostConext ).

There is actually an episode:

On Android, Rendering shows that the video memory used is 0, which is inconsistent with the 3D Rendering principle I understand and has been confusing for a long time. Then I suddenly thought, is it because the mobile phone is shared with the video?

Sure enough, when I release the project as a PC, I run the Profile again, and there will be a number for the memory usage. in addition, the number of memory occupied has a relationship with the dynamic elimination mentioned later, indicating that the memory has swap, And this is irrelevant to the things mentioned, if you have a good understanding of this piece, I hope you can give me some advice.

 

OK, continue with the dynamic elimination mentioned above. I have no intention of discovering this,

I added an Animation to the figure above to let it move left and right. When I tested the image on a real machine, the DrawCall would be reduced by 1 when it was removed from the screen, that is, as long as the screen does not see anything, Unity will automatically remove it for you,

Reduce DrawCall. In fact, it is quite normal to think about it, because Unity is a complete 3D engine, which is why the Unit is not included in the Unity, without the screen width and height, you can only adjust the camera's field of view.

Compared with Starling and Cocos, although the underlying layer is also using GPU for rendering, his overall engine architecture is based on 2D, therefore, it is naturally impossible to achieve this kind of automatic elimination and memory swap behavior at the underlying layer. in contrast, Unity is much better.

To solve this major puzzle of Unity, I have come to the following two conclusions based on the experiment above:

 

Conclusion: even if there are many unused images in the project, as long as they are not placed in a special folder (Resource, StreamingAssets) and are not referenced by Prefab, they will not be packaged during export, it does not occupy the video memory, but it will be used in the development phase.

 

Conclusion: Unity removes the objects removed from the scene to reduce DrawCall.

 

The best way to manage memory (memory) in Prefab

My root question is not over here. Now that we know how Unity loads materials, when will it unload it?

I did the following experiment again:

 

Create two scenarios: SceneA and SceneB. Load a texture in SceneA and provide a button to jump to SceneB.

Click the button to jump to SceneB. SceneB is an empty scenario.

It is expected that the memory occupied by SceneA will be released when switching to SceneB, but the result is disappointing... it remains unchanged.

Even if I call GC in SceneB, it is useless (in fact, a friend who has read the GC introduction should also know that it should be useless to call GC there)

 

Last, I had a Google call, but this time I was not as lucky as I did last time. This is why I turned to study Prefab later, but I still want to finish it here.

In another unexpected test, I found that when I create another SceneC, the video memory in SceneA will be released when SceneA-> SceneB-> SceneC. I also sent a Question.

A friend confirmed his project that Unity was so http://ask.unitymanual.com/question/36097.

 

Since the memory automatically managed by Unity needs to be eliminated in two scenarios, is there any way we can control it by ourselves? The method is to use Prefab.

The two articles on how to create and use Prefab songyu have already been very clear, so I will not repeat them.

 

Conclusion: The Display memory of Unity must be recycled across two scenarios. If Prefab is used, the display memory can be released when assetBundle. Unload (true) is called.

 

The above are some of my experience through experiments over the past few days, hoping to help you. If anything is wrong, please point it out

Thank you.

 

Best

Eran

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.