user2718135
user2718135

Reputation:

Android - Getting OutOfMemory while filling ScrollView

I'm writing a "very-long-scroll" game. The game's main element is ScrollView, which I filling with "blocks".

One "block" looks like this:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/sec3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/_40" />
</LinearLayout>

The Image's (.jpg, 720x6000) size is 800-900kb. I'm filling my ScrollView with following code (for one type of images):


    View block_3 = inflater.inflate(R.layout.block_3, null);

    for (int i = 0; i != Blocks.block_20; i++) { //Filling the LinearLayout in ListView with 20 blocks
        LinLayout_in_ScrollView.addView(block_3, layout_params);
    }

But my app crashes with OutOfMemory error. I've tried to use the ListView instead of ScrollView, but I failed. I thought about dynamical loading items in ScrollView, but it will be very hard to do. So how I can avoid the OutOfMemoryException?

P.S. Excuse for my English, please.

Upvotes: 0

Views: 687

Answers (3)

Hampus
Hampus

Reputation: 637

The problem here is that all the images need to fit into memory at the same time. It's easy to believe that the images only use as much memory as the jpg files do, but they are decoded when loaded into memory and will probably use around 4 MiB of memory each in your case (if my quick calculations are correct).

I would recommend you to not use a ScrollView. Either draw your graphics using a Canvas or using OpenGL. It's very likely that you will need to use OpenGL to get good performance in a real-time side-scroller game (even if it's "only" 2D). If you need a lot of images, then you'll have to be very careful about not loading too many into memory at once (without freeing the memory for other images first). It can be useful to do the image handling and rendering in native code in some cases using JNI and the Android NDK, because it's sometimes hard to get good real-time performance in Java on Android without garbage collection freezes and similar when loading many images.

OpenGL takes some work to learn (if you don't know it), but I would recommend you to look into it. Have a look here for an overview about OpenGL ES on Android. It will enable you to get great performance on a wide range of devices. It's very much worth the effort to learn it in my opinion.

Upvotes: 0

Ben Pearson
Ben Pearson

Reputation: 7762

The approach that you're taking has the potential to introduce other problems as well as OOM exceptions. I would strongly suggest you take a look at the JetBoy example provided in the Android SDK. As a start, you probably want to look at using a SurfaceView instead of a ScrollView

Upvotes: 0

user2511882
user2511882

Reputation: 9152

Out of Memory Exception is one of the most common and the most trickiest to handle when dealing with images. You might want to take a look at this: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap and http://developer.android.com/training/displaying-bitmaps/index.html. The tutorials teach how to load bitmaps/images efficiently and there are some samples as well. You might want to see this as well: Out of Memory Exception while scroll Listview?

Upvotes: 0

Related Questions