Stack Allocation / Escape Analysis

Put your problem here if it does not fit any of the other categories.

Stack Allocation / Escape Analysis

Postby SteveTheCode » Fri Mar 02, 2012 1:16 am

I've been a developer for a long time (coming from a C++ and C#) background, but I am new to Java and Android.

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.
SteveTheCode
Freshman
Freshman
 
Posts: 2
Joined: Thu Mar 01, 2012 10:07 pm

Top

Re: Stack Allocation / Escape Analysis

Postby Phyll » Fri Mar 02, 2012 2:07 pm

Hi SteveTheCode,

I think the consensus at the moment is to avoid anything that causes garbage collection during a game. Since a lot of GC can cause the game to stutter if its a moving game. Generally, things such as your example might be done with a global variable to avoid it, a function itself may even be intolerable if speed is a concern but I don't think at the moment you have a lot of control over where in memory the OS actually puts your variable. I know that's not as elegant but there is a price to pay for that in Android.

Another thing that's unclear about your example. There is a comment that explains that the result is returned by reference and just to be clear a double should be returned as a value. Although the whole value reference argument surrounding how Java does things is pretty confusing. But you can't pass things into or back from a function by reference as you would with a pointer in C or a var in Pascal. Nor can you operate on an argument of that type like you can in those languages. Please excuse me if that's not exactly what you meant.

There's a place in Google where you can make suggestions. Maybe that ones already been made, I don't know but you could make a suggestion that they do something about local variables being on the methods local stack. I think its kind of a Java thing but you never know. I'm used to Pascal which does most everything that way. Java seems to rely on the garbage collector to figure out when to return the memory from a method.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Stack Allocation / Escape Analysis

Postby SteveTheCode » Sat Mar 03, 2012 12:17 am

Since a lot of GC can cause the game to stutter if its a moving game. Generally, things such as your example might be done with a global variable to avoid it


Hmmm. Maybe not a global variable as such, but in the past, in rare circumstances (in non-game projects) I have sometimes written an object stack. You pop one off when you need it and push it back on when you are done in an effort to avoid new memory allocation.

e.g. Something like

Code: Select all
double GetTorqueMagnitude(Vector a, Vector b)
{
        double t;
        Vector torque = Vector.Pop(); //Get a new one from a cache

        a.Cross(b,torque);   //Result is returned by reference in torque.

        t = torque.magnitude();
        Vector.Push(torque); //Push it back so it can be used again.
 
        return t;
}


You have to be very careful with that sort of thing of course. I may end up doing it though.

Code: Select all
There is a comment that explains that the result is returned by reference and just to be clear a double should be returned as a value.


Well, the reference to the torque object is being passed by value, so I suppose technically the answer is not being returned by reference. Incidentally, doing it that way is an effort to avoid creating a new object, since the semantically nicer way to do a cross product function is..

Code: Select all
Vector torque = a.Cross(b);


Where Cross returns a new torque object that it creates internally.

Code: Select all
There's a place in Google where you can make suggestions. Maybe that ones already been made


I think quite a few people have been asking for it, and I think newer Java VM's do it, but it's not clear if Dalvik is.

I'm used to Pascal which does most everything that way


Back in the 80s, Turbo Pascal was my first language :)
SteveTheCode
Freshman
Freshman
 
Posts: 2
Joined: Thu Mar 01, 2012 10:07 pm

Re: Stack Allocation / Escape Analysis

Postby Phyll » Sat Mar 03, 2012 12:30 am

Hi SteveTheCode,

Yes, if you remember, BASIC had garbage collection. What a pain for real time programming. Most of my efforts over the years have been in machines talking to machines or controlling machines in processes and garbage collection was never my friend. That is why Turbo Pascal and now Delphi have always been better at that kind of thing than BASIC without resorting to C. I don't really like C too many files to keep track of and that case sensitivty stuff and those licenses and things Borland decided were just so much crap. Anyway, Android is rooted in Linux and Unix and Java. That's kind of what we have to work with at the moment. Oh and I left out "from C". You really can't miss the contribution of C to Java.

I have learned to work with it but I can still rant a little bit.

Thanks,

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 14 guests