In my app, I have an Activity that acts as a camera preview. I use a SurfaceView stretched across the entire screen, along with a bunch of items overlaid on top of it but nothing too complicated. This Activity can launch other Activities which will then return back to the camera preview.
I've been very careful to clean up resources, recycle bitmaps, avoid memory leaks, etc. I can run this app and test it like crazy, but when my phone has been on for a while and other apps are in memory, I get silent shut downs when resuming or creating the Activity that holds the camera preview. A common test case to reproduce the crash is use the app, snap photos (which triggers processing), launch sub activities, etc over and over again. Back out of the app, fire up something resource/graphics heavy, and then resume my app.
Here is some logcat output at the time of the crash:
03-29 14:20:02.109: ERROR/dalvikvm(6368): externalAllocPossible(): footprint 2756592 + extAlloc 15831356 + n 8640000 >= max 22409232 (space for 3821284)
03-29 14:20:02.109: ERROR/dalvikvm-heap(6368): 8640000-byte external allocation too large for this process.
03-29 14:20:02.109: ERROR/dalvikvm(6368): Out of memory: Heap Size=3835KB, Allocated=2835KB, Bitmap Size=15460KB, Limit=21884KB
03-29 14:20:02.109: ERROR/dalvikvm(6368): Trim info: Footprint=5383KB, Allowed Footprint=5383KB, Trimmed=1548KB
03-29 14:20:02.109: ERROR/GraphicsJNI(6368): VM won't let us allocate 8640000 bytes
My activity is logging at every step, so this happens in Activity.onCreate in between calling super.onCreate and setting the context view to my xml layout. My first thought was that the process of acquiring the SurfaceHolder or whatever happens in the SurfaceHolder methods might be too much in a tight memory situation, but this is way before that happens. It seems that it's occurring in setContentView while parsing my xml layout and building the View objects.
My camera preview code is taken from examples I've found in books and articles, so I'm wondering if there is any additional cleanup I need to do in surfaceDestroyed? Should I try and trigger a garbage collection at that point? The reason for this thinking is that the system has enough memory for the app to run in conditions where less apps are in memory. It either has to do with my own app not cleaning up after itself enough, or the system not being able to reclaim memo开发者_如何学JAVAry fast enough for my app. What I don't understand is why during setContentView is it attempting to allocate so much new memory.
Here is my surface callback code and a paraphrase of what goes on in the activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_preview);
// crash occurs here
// ...other stuff
initControls();
}
private void initControls()
{
previewHolder = preview.getHolder();
previewHolder.addCallback(surfaceCallback);
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// ...other stuff
}
SurfaceHolder.Callback surfaceCallback = new Callback() {
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d(ApplicationEx.LogTag, "surfaceDestroyed");
camera.stopPreview();
camera.release();
camera = null;
isPreviewRunning = false;
}
public void surfaceCreated(SurfaceHolder holder) {
Log.d(ApplicationEx.LogTag, "surfaceCreated");
camera = Camera.open();
try
{
camera.setPreviewDisplay(previewHolder);
}
catch(Throwable t)
{
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
Log.d(ApplicationEx.LogTag, "surfaceChanged");
if (isPreviewRunning)
{
Log.d(ApplicationEx.LogTag, "preview is running, stop preview");
camera.stopPreview();
isPreviewRunning = false;
}
Camera.Parameters parameters = camera.getParameters();
setPreviewAndPictureSize(parameters, width, height);
parameters.setPictureFormat(PixelFormat.JPEG);
parameters.setJpegQuality(85);
camera.setParameters(parameters);
camera.startPreview();
isPreviewRunning = true;
Log.d(ApplicationEx.LogTag, "end surfaceChanged");
}
};
Stop the preview and release the camera in onPause()
, and acquire it in onResume()
. Regardless of anything else, right now, you are going to tie up the camera and prevent other applications from using it when the user presses HOME.
(BTW, I had this wrong for a long time and corrected it in my books over the past few months)
It is possible that this will help with your memory situation, though I am not certain. If you could narrow down what exactly is asking for a 8640000-byte allocation (which is really really big), that might help. Perhaps some background image in your layout?
精彩评论