- Home /
Physics.simulate allocates memory each frame
With the profiler I see that each time physics.simulate is executed it causes memory allocation, even when there is nothing in the scene moving. The amount it allocates is about 1~5 KB. Is it normal?
Thanks.
Updated: Sorry for wrong amount of allocated memory. It is about 1~5 KB, not 100~150KB.
Physics still needs to run, even if the rigidbodies are stationary. 100~150$$anonymous$$B is really not a lot of memory, so I wouldn't worry.
I would like to make memory allocations as few as possible thus reduce the frequency of garbage collection. If that amount of memory allocated by physics.simulate cannot be avoided, then I should focus on allocations caused by other modules. Thanks.
Have you deter$$anonymous$$ed that garbage collection is the bottleneck of your application?
If so, you must be allocating a large amount of memory. '100~150`$$anonymous$$B is a really small amount of memory, when you consider even most mobile phones now have at least 1 Gig of memory. 1~5
$$anonymous$$B is absolutely nothing. Are you sure you're not just looking for optimizations without a cause? Why do you want to reduce memory allocations?
I'm pretty sure both Box2D and PhysX use manual memory management, so no actual garbage collection is run for either. The same goes for the underlying Unity engine, which is written in C++.
This means that if garbage collection is your bottleneck, it is probably in your scripts, or anything else in the mono-runtime-environment.
Thanks for reply. I know 1~5kb is not a big large amount, but it is allocated each frame. I have eli$$anonymous$$ated or reduced almost all other allocation in each frame caused by my scrips, and the total amount allocated by them each frame is less than 1$$anonymous$$B. But there are still frame hiccups during the game play.
The project is a PS3 game, so the memory capacity is not so sufficient. Any unnecessary memory allocation each frame may cause the garbage collection happens more frequently, that's why I want to reduce them as much as possible.
Anyway, if the allocation is necessary, I will try to find other possible causes leading to the frame hiccups. There should be something I missed.
Thank you very much.
There are some pretty good threads on the same issue you are having. Following those suggestions usually solves the "hiccups". I also had this problem once, but some code-reformatting and other optimizations fixed it.