Android : Android Custom View Bitmap Memory Leak
Nov 16, 2010
I've got a custom view in which I need to draw two bitmaps, one is a background, representing the image of a map and one is a pin which will be drawn on top/left position in canvas.
The both images are drawn onDraw and remain the same during the live of the activity that contains the view. After a while I get a OutOfMemoryError: bitmap size exceeds
VM budget
This means that I have a leak and the bitmaps don't get garbage collected. I asked this question before, but now the situation changed a little. I made a init method where I set the bitmaps I want to use but this still isn't a good approach, the error appears later, but still there.
Here is the code:
public class MyMapView extends View
private int xPos = 0;
private int yPos = 0;
private int space = 0;
private Bitmap resizedBitmap;
private Bitmap position;
private Bitmap mapBitmap;
public void destroy()
resizedBitmap.recycle();
resizedBitmap=null;
position.recycle();
position=null;
mapBitmap.recycle();
mapBitmap=null;
I call the setMapBitmap() and init() on onCreate of the activity that contains the view. In there I know what bitmap will be used as map. onPause of the activity I call the destroy() of the view. I still get errors.
I've read this this but I don't know how to adapt it on my case
I AM OPEN TO ANY OTHER SOLUTION. Please note that I need to resize the map bitmap (based on the height of the layout that contains it in mai activity) and still be able to draw the position on correct place.
View 1 Replies
Jul 21, 2010
I have a very large image and I only want to display a section the size of the display (no scaling), and the section should just be the center of the image. Because the image is very large I cannot read the entire image into memory and then crop it. This is what I have so far but it will give OutOfMemory for large images. Also I don't think inSampleSize applies because I want to crop the image, not lower the resolution.
Uri data = getIntent().getData();
Input Stream is = getContentResolver().openInputStream(data);
Bitmap bitmap = BitmapFactory.decodeStream(is, null, null);
Any help would be great?
View 3 Replies
View Related
Jun 27, 2009
If I create and release multiple AudioTrack objects, the GREF count continually increases which eventually causes an application crash. Even with a forced garbage collection, I end up with the same high GREF count at the end of the loop.In my application, I continuously create AudioTracks from variable lenght buffers.I guess I can try and use a fixed size pool of AudioTracks each with a buffer large enough to fit my longest sound, and try and reuse them.Is there a better/correct way to completely clear resources used by an AudioTrack?
View 5 Replies
View Related
Jan 10, 2010
I have been banging away at this one for weeks and I feel like I have exhausted my research capabilities. I am hoping that someone will see my error in the code posted below. The code is completely functional doing everything I need, but there is a major memory leak. When I try to track memory in the DDMS, the VM Heap tells me that my object count is relatively stable and the used memory is also relatively stable (it comes back down to a similar value after each GC after panning).
When I look at the memory pie chart, the free memory loses over 1meg of capacity with each map pan (overlay reload) and the Unknown memory grows. I have not found anything with the allocation tracker. Functional Summary: I load overlays onto a map based on data I get from polling my server. When I pan the map far enough I clear the overlays and load new ones.
MapFrontEnd.java (snippets): //header info private List<Overlay> overlays;
//in onCreate overlays = mapView.getOverlays();
// here I attempt to fully clean up my old Overlays for (Overlay i : overlays) {
if (i instanceof ParcelOverlay) ((ParcelOverlay) i).cleanUp(); }
View 4 Replies
View Related
Dec 4, 2009
In my app, I'm fetching data via HTTP and use the response string to create an ArrayList of objects. So what I'm doing once I get the response is:
String[] fields = response.split("<field>");
and use the field[] values like this:
ArrayList<Movie> movies = new ArrayList<Movie>(); for (int row = 1; row < fields.length; row += 9) { movies.add(new Movie(fields[row], fields[row + 1], ..., fields[row + 8]);
}
View 5 Replies
View Related
Oct 14, 2010
I have an application that uses a Service and some list activities. When the activities are opened, I can see the heap usage increase in DDMS. When the activities are closed, the heap usage decreases slightly. The service is still running in the background at this point. If the activity is started again by re-running the application and then closed, the heap usage increases again then decreases, but never returns to the original level before the activity was first opened. If it repeatedly (10-15 times) open the activity then close the activity, the heap size (both MB and # Objects) balloons!
I'd expect ListActivity's onDestroy to take care of itself when it gets destroyed. What am I missing with this? Am I using ListActivity incorrectly?
If I examine the heap using MAT, I see 10-15 ListView objects. Unfortunately, I've not got much experience with MAT, but I think it means that the instances are still hanging around rather than being garbage collected. Can anyone explain what's going on?
A test app similar to my real code is below. Create a new android application, add this to the manifest:
CODE:................
View 3 Replies
View Related
Nov 24, 2009
I am experiencing a memory / resource leak on a T-Mobile G1 device with my application. I installed the "Task Manager" application from Android market and my memory usage is not monotonically increasing. It stays relatively flat over time. Furthermore, none of the other processes are chewing up tons of memory either (really, I can get into this state with just my app running). I am not experiencing this problem on any of the other Android phones (including Eris, Hero, and Droid).
The interesting thing is that if I kill my application, the phone is *still* very slow and sluggish. The only thing that seems to be able to get me out of this situation is a battery pull. If I run my application for about 3 hours, the phone starts to become very sluggish. Even simple operations like hitting the "home" button take many seconds. I'm not sure what to do at this point and am wondering where I can go from here.
View 3 Replies
View Related
Sep 24, 2009
In my app activity A creates activity B, however when the back key is pressed it goes back to activity A. This is a simplified version of my application. I would expect memory to be reclaimed when going back to Activity A. Going to activity B allocates more memory. I am using dumpsys meminfo to get the allocated kbs. How can I easily tell what memory is creeping? I do not keep any references to activity B. I know about the ddms allocation tracker, but wondering if there is a more better tool to figure this out.
View 2 Replies
View Related
Jan 6, 2010
I'm parsing a document with large text field. In my characters section I am using StringBuilder
currentStory.append(ch, start, length);
then in my endElement i'm assigning it to the appropriate field on my object.
if (name.equals(tagDesc)) { inDesc = false;
if (currentItem != null ) { currentItem.setSummaryText(currentStory.toString());
} currentStory.setLength(0);
} setSummary is public void setSummaryText(String text) { Story = text;
}
And I am running out of memory. If I change setSummaryText to something completely weird like this:
public void setSummaryText(String text) { char[] local = text.toString(
) Story = new String(local);
}
I am fine. I just cant figure out where I am holding that reference?
Story is a member var of this object initialized with "";
Note - assigning to local String variable instead of char[] - fails as well.
View 1 Replies
View Related
Aug 10, 2009
I know that's a subject which was discuss a lot of time and I try to find a solution since more than 6 day for the memory leak which appears in my application.
I've read this page: http://www.curious-creature.org/2008/12/18/avoid-memory-leaks-on-andr...
So, I don't use static member fields, I always use recycle() and I don't use inner class.
My application use a gallery and some bitmap. I use finalize to monitor garbage collection in each class of my application: protected void finalize() throws Throwable {try {Log.d("FINALIZE", this.toString());} finally {super.finalize();}
View 2 Replies
View Related
Oct 6, 2010
After several hours of searching for the cause of a Out Of Memory Error, I found a wierd problem with Strings. To keep it simple, trimmed it down to something like this:
ArrayList<String> someStringList = new ArrayList<String>(1000);
for (int i=0; i<1000; i++) {
String someBigString = new String(new char[100000]);
String someSmallString = someBigString.substring(0,1);
someStringList.add(someSmallString);
}
View 16 Replies
View Related
Sep 8, 2009
There appears to be a leak when using PhoneStateListeners. The following code simply reigsters in onResume and unregisters in onPause a PhoneStateListener. Repeatedly launch then press BACK (so the app is finished & onDestory is called before the next launch) and the number of activities reported by meminfo will equal the number of launches. For instance, here is the output I get after launching (and finishing) 11 times in sequence:
dumpsys meminfo com.example.leak Currently running services: meminfo
DUMP OF SERVICE meminfo: Applications Memory Usage (kB): Uptime: 12204322 Realtime: 12204322
** MEMINFO in pid 1358 [com.example.leak] ** native dalvik other total size:
2648 3079 N/A 5727
allocated: 2604 2254 N/A 4858
free: 43 825 N/A 868
(Pss): 913 1305 1475 3693
(shared dirty): 1080 3864 568 5512
(priv dirty): 808 936 1056 2800
Objects Views: 77
ViewRoots: AppContexts: 12
Activities: 11
Assets: 2
AssetManagers: 2
Local Binders: 36
Proxy Binders: 21
Death Recipients: 0
OpenSSL Sockets: 0
SQL heap: 0
dbFiles: 0
numPagers: 0
inactivePageKB: 0
activePageKB: 0 #
Without the PhoneStateListener, the number of activities is always 1 no matter how many times the app is launched & finished. So, am I doing anything wrong here? Or is this a bug in the TelephonyManager?
Here's the code package com.example.leak;
import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log;
public class LeakExample extends Activity {
private class MyPhoneStateListener extends PhoneStateListener {
@Override public void onCallStateChanged(int state, String incomingNumber) {
if ((state == TelephonyManager.CALL_STATE_RINGING)
|| (state == TelephonyManager.CALL_STATE_OFFHOOK)) {
LeakExample.this.finish();
} } }
MyPhoneStateListener phone_listener = new MyPhoneStateListener();
TelephonyManager telMgr ;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
telMgr = (TelephonyManager) getSystemService (Context.TELEPHONY_SERVICE);
} @Override protected void onPause() { super.onPause();
telMgr.listen(phone_listener, PhoneStateListener.LISTEN_NONE);
} @Override protected void onResume() { super.onResume();
telMgr.listen(phone_listener, PhoneStateListener.LISTEN_CALL_STATE);
} @Override protected void onDestroy() { super.onDestroy();
Log.v("LEAK EXAMPLE", "onDestory");
}
View 3 Replies
View Related
Apr 14, 2010
I am using a simple frame animation and when I exit and reenter my activity the activity is still referenced in memory.
//This is a sample activity I created to simulate the problem public class MemoryLeakActivity extends Activity {
@Override public void onCreate(final Bundle savedInstanceState) super.onCreate(savedInstanceState);
} @Override public boolean onKeyDown(final int keyCode, final KeyEvent event) {
final Intent i = new Intent(MemoryLeakActivity.this, LeakyActivy.class);
startActivity(i); return super.onKeyDown(keyCode, event);
} }
View 3 Replies
View Related
Oct 15, 2009
I start a Service from an Activity, the user presses back, the Activity goes away and the Service stays running in the background. However the activity (and it's views) still stay in memory. I tested this with cupcake and G1. To reproduce I've made a simple program.
The activity:
package com.android.service_test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class StartService extends Activity {
/** Called when the activity is first created. */
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startService(new Intent(getApplication(), RunningService.class));
} }
View 20 Replies
View Related
Sep 27, 2010
Is there any way to pre allocate the memory required for a bitmap. Android's memory model is making it very difficult for me to cache images I need to cache.
It would seem the problem is because the VM heap rarely shrinks itself, so I would like to just allocate memory for bitmaps ahead of time? but I can not figure out how to do this?
View 4 Replies
View Related
Feb 26, 2009
Ok I am at the end of my rope. I am doing some image processing. I have a large image file which I open and create a smaller bitmap from. At the end of processing I call recycle on everything. I null everything. I run GC manually.
I then try edit another image and I get an out of VM memory error. Bitmap exceeds etc etc.
I am looking at the heap and the secone edit doesn't seem to cause it to increase at all.
What else can I do. Surely google cannot possibly be suggesting that we can open one large bitmap per session and thats it?
View 16 Replies
View Related
May 13, 2009
Part of my application allows the user to color-correct images. When i open an image for editing, i'd like to know what, more or less, the available bitmap memory is for the device that the app is running on.
For the G1, the limit is use is about 6MByte (about a full-size image that can be taken by the phone (3MPixels) with 16 bit color ==> 6MByte). If i try to open anything larger, i often get an out-of- memory exception. Limiting it to 6MByte reduces these OOM exceptions by a great deal. However, i can see future devices having more memory available because (as one of the reasons) they can take larger pics (e.g. 5MPixel or something).
Right now, my app just uses a hard-coded limit of 6*1024*1024. Is there a way to make this variable, to future-proof this by examine the device my app is running on. If so, how?
View 10 Replies
View Related
Aug 10, 2010
I have a list view that display about 25 images with size 60x60 pixels. I dowload this images from internet and I store in an arraylist as Bitmap. I need to save it in a arraylist because a listview will recycle views and so I need, wenn the user scroll, to display the image in the new view WITHOUT downloading again from internet
But after some time I got
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
It seams that I got this exception about after loading 20 images ...
Reading in forum I have read about memory heap.
I try to check the memory heap usage with: Debug.getNativeHeapAllocatedSize and Debug.getNativeHeapSize()
When the exception occours both are about 4M.
But android has more than 4M?
When the application start the values are a little less to 4M so it seams is not a big memory usage or memory leak.
To be sure I want to call : ActivityManager.getMemoryClass() but this method is and instance method and I can't find the object to invoke!
But this method say, that al min should an application have 16M!
Any help? I have seen many topics in google ... but all speak about recycle. But if I recycle a bitmat ... then should I download again from internet?
Should I store the image on flash memory or sd card instead an arraylist in memory?
View 6 Replies
View Related
Jan 15, 2010
My game - Tower Raiders - is continually floating fairly close to running out of bitmap memory. Unfortunately, I haven't found good information on profiling bitmap memory usage so that I can optimize / make informed decisions / add new content if it allows / etc.
I'm aware that bitmap memory is not heap memory, as it's allocated natively. But that it is accounted for somehow in the VM and influences garbage collection. Maybe it's an oversimplification of how bitmap memory works - but I'd like to find a tool etc or some way of determining I as the game is running right now, there are no more than X bytes of usable bitmap memory remaining.
So, if anyone has any advice etc that would help me in these respects I would greatly like to hear about a workflow for this.
View 2 Replies
View Related
Jun 4, 2010
I've read a lot of previous messages on this topic but couldn't find universal agreement on the whole. I can't reproduce this in house, but a customer got an OutOfMemoryError when using an icon chooser dialog.
This dialog is much like the Grid1 example - only it's in a dialog.
The bitmap memory seems to run out after running the dialog a few times for a customer, resulting in java.lang.OutOfMemoryError: bitmap size exceeds VM budget. Should I try to call recycle on the bitmap from an ImageView when I reuse the view? Or would the ImageView do that anyway?
But even if I do that, that's only a small portion of the bitmaps because most of them are seen without scrolling. The Adapter and the GridView are both local variables and therefore shouldn't be referenced after the dialog is dismissed.
I do believe that it should be possible to use many bitmaps in an app - Google Maps does it, Picture Gallery does it. But maybe I need to learn more rules. Some say you should call bitmap.recycle, some say you shouldn't have to. Many say calling gc.collect will make it worse, etc.
View 2 Replies
View Related
Sep 2, 2010
I've been trying to diagnose a memory leak in an Android application I'm writing. I got a heap dump loaded into Eclipse, but the results I'm seeing are very curious. There are some 20,000 instances of an exception (specifically, LDAPException from the UnboundID LDAP library) in the heap with no inbound references.
That is, they show up at the root of the dominator tree. The OQL SELECT objects e FROM com.unboundid.ldap.sdk.LDAPException e WHERE (inbounds(e).length = 0) returns over 20,000 results, totalling to nearly all of the heap. And yet, the GC runs before the heap dump and I can see that it's running in the console, repeatedly, during the execution of the leaky code. If these instances have no inbound refs, what could be keeping them alive?
I also tried doing a "shortest paths to GC" query. It shows one LDAPConnectionReader row retaining 2 instances, and ~20k LDAPException @ <addr> unknown rows with various hex addresses.
Update: I haven't had time to further diagnose this since posting it, and the bounty I posted is ending before I likely will. I'm awarding it as best I can now, lest the points go to waste. Thanks to everyone who looked into this! I will come back later and update again with the results of further diagnosis, when life is a little less hectic.
View 3 Replies
View Related
Jul 24, 2010
I have a large bitmap that i would like to keep in memory when the qwerty keyboard is visible. My app is always in landscape, so this is the only configuration change that will cause a reDraw. I heard this is what onRetainNonConfigurationinstance() is for...but cant figure it out. Can someone point me in the right direction?
View 1 Replies
View Related
May 26, 2009
When I pass a Bitmap using the Binder Remote service mechanism, does the internal implementation of writeToParcel/createFromParcel of the Bitmap class use shared memory?
View 5 Replies
View Related
Jul 22, 2010
I'm developing an image editor for Android. When I do some operations with small pictures (600x600 - 1024x768), the program work well. But when I do some operations with big pictures, I always obtain an OutOfMemory Error. I read read many post trying to have the image in memory. Also I looked at the Android's Gallery, trying to simulate the effect of loading the image with lower quality and get the complete quality after a while. But I doesn't get to work with large images.
I thought of using the parameter inSampleSize but I raised the problem of having to perform operations on the image and save it with the original size, so it no longer serves me. The only thing I've done is use the parameter inJustDecodeBounds and display an error to the user if the image exceeds the available memory.
Is there any possibility of working with large images with no memory problems? I need urgent help.
View 19 Replies
View Related
Sep 12, 2010
I would like to know is there any tool in android to find memory leak and code optimization.
View 8 Replies
View Related
Mar 24, 2010
I have a custom view which is drawing some bitmaps on screen. I want to scale the bitmaps depending on some sensor data. Can anyone suggest to me the best way (performance wise) to scale the bitmaps.
Right now I'm creating the bitmaps in the constructor of the view but if I start to scale it in the onDraw method I believe I'll just be re-drawing the bitmaps (Which will be a memory / cpu hog).
View 1 Replies
View Related
Sep 15, 2010
I am New to this Android platform. In one application, there is a memory leak. But I couldn't able to find in which activity or class the memory leak is happening. I am using MAT to get the information that the memory leak is there or not. But not able to find which activity is leaking. Please tell me how to find the which activity is leaking.
View 2 Replies
View Related
Dec 23, 2009
I've been doing a lot of searching and I know a lot of other people are experiencing the same OOM memory problems with BitmapFactory. My app only shows a total memory available of 4MB using Runtime. getRuntime ().totalMemory(). If the limit is 16MB, then why doesn't the total memory grow to make room for the bitmap? Instead it throws an error.............
View 1 Replies
View Related
Apr 8, 2010
I'm writing game with a large amount of .png pictures. All worked fine. Than I added new activity with WebView and got memory shortage. After that I made some experiment - replace game .png images with ones that just fully filled with some color. As result memory shortage had gone.
But I suppose that Bitmap internally hold each pixel separately so such changes should have no effect. Maybe this because of initial images have alpha channel and my test images have not it?
But actually question is: Will decreasing .png images files sizes make some effect on decreasing usage of VM application heap or not?
View 3 Replies
View Related
Oct 20, 2009
I use advanced task killer to keep things under control, and handcent in favor of the stock msg app. I notice that I have upwards of 80mb free memory after killing tasks after a reboot. Over the course of the day, with only my ignored apps running, that number shrinks to 48-50mb. Does that indicate a leak somewhere?
View 6 Replies
View Related