Reduce Garbage
umresh opened this issue · 7 comments
Hi,
Awesome plugin. We are trying to develop an app where we decode multiple webp images(2k) and it creates too much garbage since we are using mid-end device it impacts performance. Is there any solution or can we decode in c# threads?
There is optimizable point.
instead of that using new byte
or new Texture2D
, make a function which passing texture2d as argument to prevent new allocation.
I updated code to using byte array as buffer. Could you check that?
Sure I'll check and update. Thanks.
Hi, I've used your sample project to load a single image which is a 4096 x 2048 image and I have attached the profiler snap and the image file. We are creating an app for an Android TV device which has low memory and high garbage sometimes crashes the app. Reducing texture res in such images is not possible because we are targeting for TV and reducing size pixelates images. We decided to use WebP to reduce download time. Another profiler snap
It needs to reuse bytearraypool. and you can reuse texture2d instance if image size is fixed.
and turn off mipmap.
and use Task
to background load.
void LoadWebpUsingPool(RawImage image)
{
byte[] bytePool = new byte[1024 * 1024 * 50];
for (int i = 0; i < 100; ++i)
{
var textasset = Resources.Load<TextAsset>("webp_1");
var webpBytes = textasset.bytes;
Texture2DExt.GetWebPDimensions(webpBytes, out int width, out int height);
Texture2D texture = new Texture2D(width, height, TextureFormat.RGBA32, mipChain: false, linear: true);
image.texture = texture;
int numBytesRequired = Texture2DExt.GetRequireByteSize(width, height, isUseMipmap: false);
Debug.Assert(bytePool.Length >= numBytesRequired);
Texture2DExt.LoadTexture2DFromWebP(webpBytes, texture, lMipmaps: false, lLinear: true, bytePool, numBytesRequired);
}
}
void LoadWebp(RawImage image)
{
for (int i = 0; i < 100; ++i)
{
var textasset = Resources.Load<TextAsset>("webp_1");
var bytes = textasset.bytes;
Texture2D texture = Texture2DExt.CreateTexture2DFromWebP(bytes, lMipmaps: false, lLinear: true, lError: out Error lError);
if (lError == Error.Success)
{
image.texture = texture;
}
else
{
Debug.LogError("Webp Load Error : " + lError.ToString());
}
}
}
Hi, garbage is reduced on multiple image loading. Thanks. Is there any other optimization we can do to reduce load time(CPU usage/time) or move to BackgroundWorker were we can have callback after decoding for keeping track of the decoded image and assigning it to the correct object?