- Home /
(ROS) App crashes due to memory allocated
Hi everyone,
I'm new to Unity, and I'm currently using the Unity and ROS# to design and program my UI to communicate with the ROS machine.
I'm able to publish and subscribe to data such as sensor data and compressed image data in my UI. But when I hold my UI on the same page for around 10 to 15 mins, my app will crash automatically. At the same time, I can find my memory was being accumulated more than 10GB in the System Monitor on the Ubuntu system.
I tried searching online and find this Caching.ClearCache()
in Unity API to clear the application's cache. And added this in my void FixedUpdate()
May I know am I on the right way to try to solve this kind of problem? or which way should think about it.
Thank you so much.
To release memory allocated by anything that inherits fromI tried searching online and find this Caching.ClearCache()in Unity API to clear the application's cache
UnityEngine.Object
call
UnityEngine.Object.Destroy( thatObject );
Hi Andrew,
Thank you for sharing. Please let me try this method on my code.
And I really appreciated your advice.
Answer by andrew-lukasik · 3 days ago
(...) compressed image data (...)
Are you creating new image every time the data changes?
Are you releasing or reusing that image?
If the answer to the question #1 is "yes" and #2 is "no" - here is the reason for app crashing. Release or reuse.
Hi Andrew, Thank you for your answer. I was using the following function to display video in my UI:
private void ProcessCameraPaintImage(byte[] a_data)
{
if(a_data != null)
{
Texture2D t_texture2D = new Texture2D(1,1);;
t_texture2D.LoadImage(a_data);
m_campaint_rawimage.texture = t_texture2D;
m_campaint_rawimage.color = Color.white;
}
}
and I'm not very sure I'm creating a new image every time.
new Texture2D(1,1);
allocates a small buffer and then t_texture2D.LoadImage(a_data);
allocates a buffer size of a_data.Length
bytes. I see no code related to releasing the memory that was allocated in the previous ProcessCameraPaintImage
call - which is precisely what you need to do here to fix this memory leak.
try this:
void ProcessCameraPaintImage(byte[] a_data)
{
if(a_data != null)
{
if( m_campaint_rawimage.texture!=null )
Destroy( m_campaint_rawimage.texture );
var tex = new Texture2D(1,1);;
tex.LoadImage(a_data);
m_campaint_rawimage.texture = tex;
m_campaint_rawimage.color = Color.white;
}
}
Hi Andrew,
Thank you for your reply and explanation of my question.
I will try it now. Again, thank you so much!!