- #UNITY PC BUILD OUT OF MEMORY HOW TO#
- #UNITY PC BUILD OUT OF MEMORY MANUAL#
- #UNITY PC BUILD OUT OF MEMORY ANDROID#
- #UNITY PC BUILD OUT OF MEMORY SOFTWARE#
- #UNITY PC BUILD OUT OF MEMORY CODE#
Using (UnityWebRequest webRequest = new UnityWebRequest(path)) Protected override void ReceiveContentLength(int contentLength)Īnd to use the above in our coroutine: private IEnumerator loadAsset(string path, string savePath) Protected override void CompleteContent() If (!canceled) fileStream.Write(data, 0, dataLength) Protected override bool ReceiveData(byte data, int dataLength) Public ToFileDownloadHandler(byte buffer, string filepath) : base(buffer)įileStream = new FileStream(filepath, FileMode.Create, FileAccess.Write)
#UNITY PC BUILD OUT OF MEMORY HOW TO#
Unity outlines how to do that here, but below is an expanded example that includes a FileStream: public class ToFileDownloadHandler : DownloadHandlerScript When doing that, we can reuse the same byte array and never have to allocate more space. Streams! Since we plan on immediately saving these large video files in local storage on device to be ready for offline viewing, we need to send the downloaded bytes right into a File as they are received. There is a way to download large files and save them without using unnecessary RAM. In this case, I only properly understood what was happening when I watched it unfold in a recording using the Allocations instrument. Using the Profiler in the Editor is only your first line of defense. I recommend using the Unity Profiler heavily for testing in the Editor and then continuing performance testing on device for each platform. The Allocations instrument captures information about memory allocation for an application. If you didn’t read the docs, and they’re long: I get it, you could have found this memory leak by running the application in Unity while using the Profiler AND by running the application on an iOS device while using a valuable free tool from Apple: Instruments. The OS has other services running plus you very likely have RAM allocated for bitmaps and UI and logic. Now we have used 800MB of RAM just on handling this one file. So, by accessing the data property, Unity allocates an additional 400MB of memory to pass off the byte array into the EventArg. Note: When accessing DownloadHandler.data or DownloadHandler.text on this subclass, a new byte array or string will be allocated each time the property is accessed. One (and give Unity credit here) is in the documentation for DownloadHandlerBuffer. I know what you’re saying, “Steff, why did it crash if we only used 40% of the RAM?” There are two ways to find the answer. That’s 40% of the 1GB that the iPhone 6 has! We’re already in dangerous territory. But, the result is the same it will use up at least 400MB of RAM. There is a chance it allocates space based on the Content-Length header returned with the HTTP Response.
#UNITY PC BUILD OUT OF MEMORY CODE#
Note, I am speculating here because I have not looked at the source code for the DownloadBufferHandler. And because each additional allocation is a guess, it will most likely overshot that amount. In our test case, it was expanding the array until it could hold 400MB. As the bytes come in, it will periodically expand the size of the array. When it is first created, it will start by preallocating memory for a small byte array where it will store all the downloaded bytes. The main focus is on the DownloadHandlerBuffer object. Let’s look at the guts of these components. The iPhone 6, with 1GB of RAM, crashed like a piano dropped from a helicopter. The iPhone 7, with 2GB of RAM, took it like a champ. The first sample files we got for testing were bigger than 400MB. The FileComplete event will fire if there are no errors, sending the entire byte array to the subscribing class. What happens here is that the UnityWebRequest will open up a connection to the given path, download everything into a byte array contained within the DownloadHandlerBuffer. Notice this returns an IEnumerator and utilizes yield statements so it should be run in a Coroutine. These are all off the shelf parts from Unity, with the exception of the FileLoaderCompleteEventArg, but just assume that we use that to pass off the downloaded bytes as an array eg: byte. For example, using the relatively new UnityWebRequest, you can download a file over HTTP like this: private IEnumerator loadAsset(string path)
#UNITY PC BUILD OUT OF MEMORY MANUAL#
One of my big complaints about the Unity manual and many tutorials is they usually just show you how to do something really quickly and don’t always tell you the exact use case or how it can just flat out fail. Recently, when building a Google VR 360 video player, we went over the 1GB of RAM available on older iOS devices pretty quickly.
#UNITY PC BUILD OUT OF MEMORY ANDROID#
Out of memory exceptions are common on both Android and iOS if you’re dealing with large files.
#UNITY PC BUILD OUT OF MEMORY SOFTWARE#
One reason why it is difficult to develop software for mobile devices is that the hardware is not the best compared to deploying to a console or a “real” computer.