[Java (Android)] Thread safety vs. GC

camelCase

The Case of the Mysterious Camel.
Reaction score
362
I'm taking this module which forces us to use the Android SDK, which, "yay", is in Java. I was working on a game halfway through and realized that the frame rate was dropping as I progressed, so I ran the app through a profiler and saw that it was my Vector2 math class that was causing it.

Because I have so much math going on, I also allocate a lot of Vector2 instances.. And, because Java is Java.. It's all allocated on the heap.. Which takes up a lot of memory.. And GC causes the frame rate drop..

So, I've decided to re-write my Vector2 class so that it never creates a new Vector2 instance in any of its calculations. That's half the battle.

The second half is this:
Code:
public class Vector2 {
    public static final Vector2 TmpA = new Vector2();
    public static final Vector2 TmpB = new Vector2();
    public static final Vector2 TmpC = new Vector2();
    public static final Vector2 TmpD = new Vector2();
    public static final Vector2 TmpE = new Vector2();
}

Because I have code everywhere that needs to use vectors temporarily, I decided to have some "tmp" vector2 instances allocated. Now, all of this just means that any code using TmpX will not be thread-safe. Luckily, I don't need thread-safety.

But, say, in the future, I DO need thread-safety. Is it okay to assume that this will suffice?
Code:
public synchronized void methodThatUsesTmpX () {
    Vector2 someV = Vector2.TmpA;
    //More code
}

If so, is thread-safety (by stuffing the synchronized keyword in there) or GC more efficient/better for frame-rate?
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
Just to make sure: Do you know for a fact that it's the GC that kills your frame rate?

I think I remember that Java takes extra caution when allocating small objects such as a Vector2.

Making often-used function synchronized isn't such a good diea, I think. I would guess it's worse for performance than the GC (and it's certainly worse for debugging).

How about you pre-allocate a static array of a good number of Vector2s and use it as some sort of allocation pool? More memory waste, but sounds like hell of a lot better than synchronization.

But still.. is it really the allocation/dealloc of the Vec2's that kills performance?
What do those performance hot spots look like? Are you vector methods efficient? Can you cut down on allocations simply by re-defining some methods to be self-modifying instead of returning a new Vec2?
 

Accname

2D-Graphics enthusiast
Reaction score
1,462
I agree with serius, synchronising those vectors would not be very effective.
I would suggest creating a vector pool for every thread. So every thread will have its own pool of vectors which you can dynamically increase or decrease in size when needed. Threads will never use the vectors of another thread, except maybe for copying data from one to another.
The copy method would need to be synchronized then.
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
Strangely enough, I decided on a pool instead of synchronizing before reading the replies =x
Well, I THOUGHT it was, but it doesn't seem to be because the framerate is still not improving and all my Vector2 operations are about as efficient as, well, the original equations themselves =P

And pooling didn't work, guess it isn't better than GC.

I think I just read the profiler reading wrong. I'm not used to the Android Development Tool's way of showing profiler results =/ My draw calls seem to be taking 70% of the processing, though.

THAT is really weird because I specifically made it so that I wouldn't do any heavy processing in the draw calls. Update, 30%. Draw, 70%. Wtf. I'm using Canvas but I'm only drawing.. About 30 bitmaps and some debug texts..
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
Okay, so I reviewed the profiler readings and it IS the Vector2 class. Shifted a bit more of that code and added AABB tests for the collider like s3rius recommended (what do you know, it really only took a few minutes).

My frame rate is now a good 35FPS.. Which isn't good enough. It isn't good enough because the current "game" is just three buttons, some clouds (really beautiful clouds, I assure you), a bouncing ball and platforms that appear and disappear.

And there are more things to implement. I want to keep it at 32FPS minimum when I have the entire game up and running but having 3FPS left to play around with seems.. A bit little.

I looked for the next bottleneck and..
My Camera.drawSprite() method was taking up 64.5% of the CPU time when getting 35FPS.
Looking deeper, 39.6% of that 64.5% (or 25.542% of the total CPU time) is going to Canvas.drawBitmap(), an Android call.

Looking at Canvas.drawBitmap(), Canvas.nativeDrawBitMapMatrix() is 91.5% of it. Which makes sense because I call Canvas.drawBitmap(Bitmap, Matrix, Paint);

My game's update() takes up only 14.2% of the total CPU time (that is, every single operation in the update call, which isn't a lot at the moment).
My game's draw() takes up 84.9% of the total CPU time.

So, I guess that's the thing to optimize =/

Anyone got resources for optimizing draws for Android using the SDK?
 

Accname

2D-Graphics enthusiast
Reaction score
1,462
Isnt there any alternative graphics libraries for Android?
Or perhaps forums which are for android developers where you can ask people who know this stuff much better?
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
Hmm..
I guess you're right =x

Thanks.
[EDIT]
Nevermind..
I just realized what was wrong =/
It had nothing to do with my code (or, at least, very little).

It's just.. My phone. My phone is always laggy.. And..
I just restarted my phone (for an unrelated issue) and noticed that my frame rate is now 63 per second..

Fuck. I stressed out so much over it when my it was my phone's hardware all along.
"Have you tried turning it off and turning it back on again?"
 

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Reaction score
1,669
Editing the thread title to reflect on the main subject of this entire thread. If you have any questions, please be welcomed to start a conversation with me. :)
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • The Helper The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/
  • The Helper The Helper:
    Here is another comfort food favorite - Million Dollar Casserole - https://www.thehelper.net/threads/recipe-million-dollar-casserole.193614/

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top