advice
advice

Reputation: 6008

LibGDX - 1-Byte Array Size Varies On Different Resolution

I'm currently developing a game using libGDX; however, when running it on my two different devices, there is a huge difference in memory usage.

I'm currently using a HTC One X+, and a Nexus 7 for testing. While testing, I've noticed that the Nexus 7 has a larger heap size and is also using more of that heap. While looking into it with the Android Device Monitor, I have noticed that the 1-Byte Array is completely different between devices( 6 MB vs 48 KB ) while everything else is fairly similar.

I believe the issue is with textures and the different resolutions between the devices ( 1280x720 vs 1920×1200 ) and how libGDX stores the information. Although, unlike normal Android programming, there is no way to have different sets of textures ( drawable-hdpi vs drawable-xxhdpi ), as all of them go within the assets folder.

How can I improve on the memory that this is currently using?

Below is some of the data I've been looking into. This is just sitting at the main menu with 2 textures being displayed, and nothing being updated.

HTC One X+ Heap - 62.70% Used ( 1280 x 720 )

HTC One X+ Heap

Nexus 7 Heap - 94.53% Used ( 1920 x 1200 )

Nexus 7 Heap

Data Compared

    Nexus 7 (vs)    HTC One X+  

Heap : 9.6 MB (vs) 4.8 MB
Allocated : 9.1 MB (vs) 3.0 MB
Free : 537 KB (vs) 1.8 MB
Used : 94.53% (vs) 62.70%
Objects : 48,024 (vs) 46,012

free : 402 KB (vs) 1.8 MB

data : 914 KB (vs) 860 KB
class : 1 MB (vs) 1.1 MB
1-byte : 6 MB (vs) 48.6 KB
2-byte : 705 KB (vs) 633 KB
4-byte : 399 KB (vs) 405 KB
8-byte : 9.3 KB (vs) 6 KB
non-Java : 70 KB (vs) 6 KB

Logcat (Garbage collector being called fairly often.)

07-19 05:39:09.651 6453-6469/com.mygdx.game.android D/dalvikvm﹕ GC_FOR_ALLOC freed 524K, 7% free 9442K/10128K, paused 19ms, total 20ms
07-19 05:39:12.644 6453-6469/com.mygdx.game.android D/dalvikvm﹕ GC_FOR_ALLOC freed 512K, 7% free 9442K/10128K, paused 18ms, total 18ms
07-19 05:39:15.657 6453-6469/com.mygdx.game.android D/dalvikvm﹕ GC_FOR_ALLOC freed 512K, 7% free 9442K/10128K, paused 24ms, total 25ms
07-19 05:39:18.660 6453-6469/com.mygdx.game.android D/dalvikvm﹕ GC_FOR_ALLOC freed 511K, 7% free 9442K/10128K, paused 24ms, total 24ms

Edit:

I created a new project using the LibGDX Setup App within Android Studio (Beta) 0.8.0. The only changes to code was to build.gradle:

classpath 'com.android.tools.build:gradle:0.12.+'

instead of

classpath 'com.android.tools.build:gradle:0.10+'

It still is currently using 96% of my heap space on the Nexus 7.

public class myGame extends ApplicationAdapter {
    SpriteBatch batch;
    Texture img;

    @Override
    public void create () {
        batch = new SpriteBatch();
        img = new Texture("badlogic.jpg");
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        batch.draw(img, 0, 0);
        batch.end();
    }
}

Upvotes: 0

Views: 269

Answers (1)

P.T.
P.T.

Reputation: 25177

You can use the same tools to find out why memory is being allocated. You can get a backtrace to the offending code. That should help explain where your 1-byte arrays are coming from. (And as Xoppa says, the GC doesn't run if you're not allocating stuff, so you must be directly or indirectly triggering allocations, as a rule you don't want any allocations in frequently run methods like render).

Just use the next tab over ("Allocation Tracker"). Get your app into a steady state (allocations during startup aren't that interesting yet). Go to the tab, click "Start Tracking", wait 5-10 seconds, click "Get Allocations", and you should see very precise answers as to who is allocating memory and why. See http://developer.android.com/tools/debugging/debugging-memory.html

(The Nexus 7 has a larger heap, so it may just let more stuff accumulate on the heap before running a GC, vs. the smaller heap which needs to be collected more frequently, so less stuff gets to accumulate.)

Upvotes: 1

Related Questions