[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,463
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,463
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,710
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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top