One of the problems I am seeing for Java and game development is the lack of the ability to create objects on the stack. You may have functions that necessarily create temporary objects and if you are calling those functions frequently, e.g. in a game loop, then there will be a large number of heap allocations which can't be good.
e.g.
- Code: Select all
double GetTorqueMagnitude(Vector a, Vector b)
{
Vector torque = new Vector();
a.Cross(b,torque); //Result is returned by reference in torque.
return torque.magnitude();
}
In C++ you can avoid the new and just create your Vector class on the stack and in C# you can implement the Vector as a Struct which will then go on the stack as well. In Java though, there seems to be no such option.
I've read that the latest JVM's use "Escape Analysis" where short lived object are automatically allocated on the stack rather than the heap, but it's not clear to me whether Dalvik does this. So in the possible absence of "Escape Analysis", what is the solution? You could implement your own object caching system, but that could quickly get messy and it really feels like something the language should provide.
Hopefully I'm missing something obvious.




