• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
sheep19

[android] out of memory when loading images

6 posts in this topic

I'm making a game for android. It is 2D and uses the android graphics toolkit (not OpenGL ES).

My game entities consist of several frames that are updated as time passes. Each frame is a different image.

Note that I'm not using the Bitmap class directly, but a wrapper I made called "GreenBitmap". It has a Bitmap inside and uses reference counting to reuse it when needed (so that it will not be loaded more than once). So for example if I have three Actors, the frames for all of them will be loaded only once and reused.

[CODE]
package com.greenbits.RiverCross;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.util.SparseArray;
public class GreenBitmap
{
private static SparseArray<Entry> map = new SparseArray<Entry>();

public Bitmap bitmap;
private int id;

/**
* @param drawableID The ID of the image
* @return The width of the image
*/
public static float getBitmapWidth(Resources res, int drawableID)
{
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;

BitmapFactory.decodeResource(res, drawableID, o);
return o.outWidth;
}

/**
* @param drawableID The ID of the image
* @return The height of the image
*/
public static float getBitmapHeight(Resources res, int drawableID)
{
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;

BitmapFactory.decodeResource(res, drawableID, o);
return o.outHeight;
}

public GreenBitmap(Resources res, int drawableID)
{
id = drawableID;
Entry e = null;

// if the ID doesn't exist (i.e the bitmap isn't stored), load it
if( (e = map.get(drawableID)) == null )
{
bitmap = BitmapFactory.decodeResource(res, drawableID); // load the bitmap
e = new Entry(bitmap, 1); // create a new entry
map.put(drawableID, e); // put it in the map
}
else
{
bitmap = e.bitmap;
++e.count;
}
}

public GreenBitmap(Resources res, int drawableID, int requiredWidth, int requiredHeight)
{
// first use Options with inJustDecodeBounds to get the image's dimensions
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

BitmapFactory.decodeResource(res, drawableID, options);

// calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, requiredWidth, requiredHeight);
options.inJustDecodeBounds = false;

id = drawableID;
Entry e = null;

// if the ID doesn't exist (i.e the bitmap isn't stored), load it
if( (e = map.get(drawableID)) == null )
{
bitmap = BitmapFactory.decodeResource(res, drawableID, options); // load the bitmap
e = new Entry(bitmap, 1); // create a new entry
map.put(drawableID, e); // put it in the map
}
else
{
bitmap = e.bitmap;
++e.count;
}
}

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
{
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth)
{
if (width > height)
inSampleSize = Math.round((float)height / (float)reqHeight);
else
inSampleSize = Math.round((float)width / (float)reqWidth);
}

return inSampleSize;
}

/**
* @return The internal Bitmap object. You can also use the public 'bitmap' member.
*/
public Bitmap get()
{
return bitmap;
}

public void recycle()
{
Entry e = map.get(id);
--e.count;

//Log.e("GreenBitmap", "count of ID " + id + " was lowered to " + e.count);

if( e.count == 0 )
{
e.bitmap.recycle();
map.remove(id);

//Log.e("GreenBitmap", "Bitmap " + id + " was recycled");
}
}

private static class Entry
{
public Bitmap bitmap;
public int count;

public Entry(Bitmap _b, int _c)
{
bitmap = _b;
count = _c;
}
}
}
[/CODE]

I'm also calculating how many samples to use to reduce memory consumption.


Here's the problem. I have a class named "Human" that has 20 frames of animation. The frames are only a few KB big. On the emulator the game runs, but it gives me OutOfMemoryException (or Error, I don't remember) on HTC One X (!).

I've read somewhere that images aren't stored on the garbage collected heap (which is guaranteed to be 16MB) but on a different, native heap that has limited memory.

Does anyone know what I can do to solve this? Why does it run on the emulator and not on a top class phone?
Is there a way to load the images in the garbage collected heap?

Thanks.
0

Share this post


Link to post
Share on other sites
What is your exact error message? Is it "java.lang.OutOfMemoryError: bitmap size exceeds VM budget" ?

If so, there are thousands of other web pages on StackOverflow explaining many different ways to get it and many different ways to handle it.

I'm guessing your code elsewhere is either making unnecessary copies of the resources, or else you are leaking resources elsewhere.


If it isn't that message, are you attempting to load all the images into main memory, or into OpenGL? Graphics memory is generally limited and precious. Generally keep stuff unloaded if reasonable or in main memory if you really need it, and out of graphics memory until it is essential for display.

Those are a few thoughts as to the reason behind it.

Post the exact error message when you get it. That will help a lot.
0

Share this post


Link to post
Share on other sites
[quote name='frob' timestamp='1346716255' post='4976235']
What is your exact error message? Is it "java.lang.OutOfMemoryError: bitmap size exceeds VM budget" ?

If so, there are thousands of other web pages on StackOverflow explaining many different ways to get it and many different ways to handle it.

I'm guessing your code elsewhere is either making unnecessary copies of the resources, or else you are leaking resources elsewhere.


If it isn't that message, are you attempting to load all the images into main memory, or into OpenGL? Graphics memory is generally limited and precious. Generally keep stuff unloaded if reasonable or in main memory if you really need it, and out of graphics memory until it is essential for display.

Those are a few thoughts as to the reason behind it.

Post the exact error message when you get it. That will help a lot.
[/quote]

That is exactly the message.

I do not attemp to load *all* images in main memory - I'm not using openGL.
I attemp to load all images that a certain game level needs - which is logical and natural. I can't be loading images in the update() loop.

I have already searched on google and stack overflow. What people say is:
1) Use lower resolution images.
2) Calculate how many samples are needed (I'm doing it).

By the way I managed to solve the problem! I noticed that some frames of the animations where actually the same picture, so I erased the duplicate ones.
For example if an animation had 3 frames, frame_0.png, frame_1.png, frame_2.png
frame_0.png and frame_1.png sometimes where the same - so I deleted frame_1.png and used frame_0.png twice. That saved me a lot of memory.

Also some images had a very big resolution (700x400 and was drawn at 200x100 for example), so I resized those as well.

I'm also aware that png images consume [b]width x height x 4[/b] bytes when loaded into memory - but I guess I can't do anything about that.


I've got another question:
As I am not using OpenGL, does it mean that the images I load are stored into main memory (RAM) instead of video memory (VRAM)?
If I used OpenGL, would the images be stored in video memory? If yes, I guess that just by using openGL huge (main) memory savings could be made.

What about games that are done by professionals? Some games have a lot of images, 3D models etc and they don't have memory problems (?). Are there techniques I should know?

Thanks
0

Share this post


Link to post
Share on other sites
[quote name='sheep19' timestamp='1346925163' post='4977131']
...

I've got another question:

As I am not using OpenGL, does it mean that the images I load are stored into main memory (RAM) instead of video memory (VRAM)?
If I used OpenGL, would the images be stored in video memory? If yes, I guess that just by using openGL huge (main) memory savings could be made.

What about games that are done by professionals? Some games have a lot of images, 3D models etc and they don't have memory problems (?). Are there techniques I should know?

Thanks
[/quote]




I strongly recommend you switching to OpenGL ES 1.0 or 2.0.

As for your last questions:
Your bitmaps are stored on the RAM, not the VRAM.

Your java code runs as compiled ByteCode on the Dalvik Virtual Machine.

1)Every object and variable is stored on the HEAP, which is the memory that the MOBILE has.

2)The only way you can store stuff on the VRAM, that would be by using the existing interface between the CPU an GPU, and that is by a library such as OpenGL ES.
OpenGL uses buffers which stores vertices information that can be used for several purposes (drawing), and since you are not using OpenGL, you won't ever touch the VRAM.

3)If you use OpenGL, you don't know how much memory you will save until you profile (test) your game on a certain real device.


4)About the professional games, the best answer I can give you right now is, that Images are stored in a PIXEL format on BUFFERS (Array of numbers) sent to OpenGL using a library, it is send as a texture, once that happens, you can delete any references to the recently created BITMAP, freeing memory from the CPU, and leaving the memory usage to the OpenGL GPU or CPU (don't focus on this right now, VBOs/FBOs).


5) OpenGL might seem hard at first, and it is, but you are smart enough to learn it ;).

Just watch some tutorials, and stick to Java.
0

Share this post


Link to post
Share on other sites
[quote name='rockseller' timestamp='1346949775' post='4977262']
[quote name='sheep19' timestamp='1346925163' post='4977131']
...

I've got another question:

As I am not using OpenGL, does it mean that the images I load are stored into main memory (RAM) instead of video memory (VRAM)?
If I used OpenGL, would the images be stored in video memory? If yes, I guess that just by using openGL huge (main) memory savings could be made.

What about games that are done by professionals? Some games have a lot of images, 3D models etc and they don't have memory problems (?). Are there techniques I should know?

Thanks
[/quote]




I strongly recommend you switching to OpenGL ES 1.0 or 2.0.

As for your last questions:
Your bitmaps are stored on the RAM, not the VRAM.

Your java code runs as compiled ByteCode on the Dalvik Virtual Machine.

1)Every object and variable is stored on the HEAP, which is the memory that the MOBILE has.

2)The only way you can store stuff on the VRAM, that would be by using the existing interface between the CPU an GPU, and that is by a library such as OpenGL ES.
OpenGL uses buffers which stores vertices information that can be used for several purposes (drawing), and since you are not using OpenGL, you won't ever touch the VRAM.

3)If you use OpenGL, you don't know how much memory you will save until you profile (test) your game on a certain real device.


4)About the professional games, the best answer I can give you right now is, that Images are stored in a PIXEL format on BUFFERS (Array of numbers) sent to OpenGL using a library, it is send as a texture, once that happens, you can delete any references to the recently created BITMAP, freeing memory from the CPU, and leaving the memory usage to the OpenGL GPU or CPU (don't focus on this right now, VBOs/FBOs).


5) OpenGL might seem hard at first, and it is, but you are smart enough to learn it ;).

Just watch some tutorials, and stick to Java.
[/quote]

1) Memory that the [i]application[/i] has. Each Dalvik activity is reserved a set amount of memory for its heap.

2) Most mobile devices don't have dedicated VRAM - they have unified memory.

4) You appear to have thrown out a large number of unrelated terms. When loading a texture into OpenGL, you are passing a pointer to an array to the OpenGL library which is then stored internally, and you are given a handle to access the Texture Object. However, since most mobile devices have unified memory, VRAM == RAM. VBOs are unrelated and are related to storing vertex information.
0

Share this post


Link to post
Share on other sites
[quote name='sheep19' timestamp='1346925163' post='4977131']
I'm also aware that png images consume width x height x 4 bytes when loaded into memory - but I guess I can't do anything about that.
[/quote]
Sure you can.

Simply put: Don't use PNG images for textures. Major games avoid it.

Move to compressed ETC1 format which should be supported by all GL2.0 devices. It is still RGB8, but much smaller than your uncompressed raw image. Some devices support additional compressed texture formats, like DXT1 and PVRTC, but you can always fall back to ETC1.
0

Share this post


Link to post
Share on other sites
Thanks - I will read some stuff about those you suggested 'cause it's the first time I hear about them.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0