Bitmaps in Android

I have a few questions regarding Bitmap objects and memory and their general taxonomy.

  1. What is an in-memory or native bitmap?
  2. How is Bitmap memory different from Heap memory?


Asked by: Haris321 | Posted: 20-01-2022






Answer 1

The memory that backs a Bitmap object is allocated using native code (malloc()), rather than the Java new keyword. This means that the memory is managed directly by the OS, rather than by Dalvik.

The only real difference between the native heap and Dalvik's heap is that Dalvik's heap is garbage collected, and the native one isn't.

For these purposes though, here's not much difference. When your Bitmap object gets garbage collected, it's destructor will recycle the associated memory in the native heap.

Source:

Answered by: Sarah625 | Posted: 21-02-2022



Answer 2

There is an important subtlety here: though Bitmap pixels are allocated in the native heap, some special tricks in Dalvik cause it to be accounted against the Java heap. This is done for two reasons:

(1) To control the amount of memory an application allocates this. Without the accounting, an application could allocate a huge amount of memory (since the Bitmap object itself is very small yet can hold on to an arbitrarily large amount of native memory), extending beyond the 16MB or 24MB heap limit.

(2) To help determine when to GC. Without the accounting, you could allocate and release references on say 100 Bitmap objects; the GC wouldn't run, because these objects are tiny, but they could in fact represent a large number of megabytes of actual allocations that is now not being GCed in a timely manner. By accounting these allocations against the Java heap, the garbage collector will run as it thinks memory is being used.

Note that in many ways this is an implementation detail; it is very likely that it could change in the future, though this basic behavior would remain in some form since these are both important characteristics for managing bitmap allocations.

Answered by: Maria188 | Posted: 21-02-2022



Answer 3

From deployments in the wild, I've found the following devices:

  • Devices that limit to 16 MiB of java heap (bitmaps are nearly unlimited).
  • Devices that limit to 16 MiB of (java heap + native bitmap storage)
  • Devices that limit to 24 MiB of java heap (bitmaps are nearly unlimited).
  • Devices that limit to 24 MiB of (java heap + native bitmap storage)

24 MiB tends to be the high res devices, and can be detected with Runtime.getRuntime().maxMemory(). There's also 32MiB devices now, and some of the rooted phones have 64MiB by default. Previously, I confused my self several times trying to work out what was happening. I think all devices count bitmaps into the heap limit. But it's wildly difficult to make any sweeping generalizations about the android fleet.

This is a VERY nasty issue on Android, and very confusing. This limit and it's behaviors are poorly documented, complicated, and extremely non-intuitive. They also vary across devices and OS versions, and have several known bugs. Part of the problem is that the limits are not precise - due to heap fragmentation, you will hit OOM well before the actual limit and must conservatively leave a meg or two of buffer. Even worse, I've got several devices where there is a native segfault (100% a bug in Android itself) that occurs before you get the java OOM exception, making it doubly important to never reach the limit since you can't even catch the native crash. For more details on my investigations, check out this post. In the same post, I explain how to measure usage against the limit and avoid crashes.

The size of the java heap is Runtime.getRuntime().totalMemory().

There is no easy way to measure the size of the native bitmap storage. The overall native heap can be measure with Debug.getNativeHeapAllocatedSize(), but only the bitmaps count toward the limit (i think).

Answered by: David286 | Posted: 21-02-2022



Answer 4

We can increase the heap size by using android:largeheap="true" in your manifest file. This will solve your some problem.

Answered by: Julia507 | Posted: 21-02-2022



Similar questions

java - How to XOR the pixels values of two bitmaps together?

I have some bitmap A which I modify to produce bitmap B. I want to efficiently produce a bitmap D by XORing the pixels values of A and B together such that D XOR A produces bitmap B and D XOR B produces bitmap A. I want to store the differences between the bitmaps so I can reverse changes that I've made. So far I've tried: Drawing A onto B with a paint object that I've called .setXfermode(new Porte...


canvas - Print Bitmaps onto other bitmap android

I am trying to make my game a bit easier on the phone, so I am trying to figure out a way to print a bunch of bitmaps onto another big one, so I can just do it once, rather than every time the screen is redrawn. So, is there any way to do this? I know there is a way to print everything that is printed to the canvas to a bitmap, but I can't seem to get that to work. If that is the only way can someone explain how to do that...


Android make .Gif out of Bitmaps

I have an app where the user saves a bunch of bitmaps in a folder on the SD card. I would like to add a feature where the user can export said bitmaps as a .gif file that is viewable say, in a text message. How can I accomplish this?


image - Android - how to avoid memory over load using bitmaps?

My application is using bitmaps and every time the user come to the specific activity where it shows an image the second time it stops working. Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"//Pics/"Image.jpg"); I have tried using things like... BitmapFactory.Options options = new BitmapFactory.Options(); options.inTempStorage = new byt...


Is it possible to create a bitmap from two bitmaps in android?

HI friends. Consider am having two bitmaps a rectangle bitmap and a text bitmap. Now i want to create another bitmap using those two bitmap ( Rectangle and Text ). Is it possible to create a new bitmap using some other bitmaps. If so please advise me or give other some valuable ideas regarding this. Thanks in advance.


android - How to scroll bitmaps?

I have one image view (assigned one bitmap to it) and another bitmap (Transparent) for painting. How it is possible to scroll this bitmap and background bitmap at the same time to be painted at different places.


java - android saving 3 bitmaps as 1

I've an app that takes a picture. It creates 2 subset bitmaps, processes these bitmaps with a distortion, then places the subsets over the original as overlays. Is there a way to save all three bitmaps as one (as it appears on the phone's screen)? I'd like to save what the user sees on the screen as a fourth bitmap on the sdcard? I've a feeling I've done this wrong. Thanks [update1] @Overrid...


android - Bit depth issues on bitmaps and screens (white is not white)

I have a transparent PNG image representing a bluetooth icon with a blue glow, exported from photoshop: On a HTC Desire, a simple imageview is created, and the PNG is used as a bitmap. If the background arround the imageview is white, there are differences between nuances. If the background is black, than the differences are ...


android - Out of Memory Error while using many bitmaps

I want use many bitmap for my game canvas. I can't Recycle bitmaps, because I have to use bitmaps for the whole section. Do mention some guidelines to optimize the code to handle many bitmap also faster performance for my canvas based Game Application. Currently i am using the following code to get bitmap from drawable resource, BitmapFactory.Options bfOptions=new Bi...


The best way to use bitmaps in android ;)

I must create android application for tablets, app will be shows new magazines and his pages. Every magazine has about 70 pages, and every page have cover as image which weighs about 700 000 bytes. Main page of app show big image and small gallery (Gallery View) with images. I work on emulator with andrid 3.2. When I add images to gallery and I try slide it, it does't work smoothly. Sometimes does't load all images and Log...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top