Reputation: 5263
If I comment this block out, then the GC spam goes away. What's causing the spam? If I comment out the section "// Cleanup" then the spam is still there.
public void updatePixels()
{
// Fill the bitmap with black.
mBitmap.eraseColor(Color.BLACK);
// Pass bitmap to be rendered by native function.
if(!mNativeHelper.render(mBitmap)) return;
// If FroyVisuals has text to display, then use a canvas and paint brush to display it.
String text = mActivity.mTextDisplay;
if(text != null)
{
// Give the bitmap a canvas so we can draw on it.
mCanvas.setBitmap(mBitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setTextSize(10);
paint.setTypeface(Typeface.create(Typeface.SERIF, Typeface.ITALIC));
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1);
paint.setColor(Color.WHITE);
paint.setTextAlign(Paint.Align.CENTER);
float canvasWidth = mCanvas.getWidth();
float textWidth = paint.measureText(text);
float startPositionX = (canvasWidth - textWidth / 2) / 2;
mCanvas.drawText(text, startPositionX, mTextureWidth-12, paint);
paint = null;
}
// Flip the texture vertically.
Matrix flip = new Matrix();
flip.postScale(1f, -1f);
Bitmap temp = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), flip, true);
// Copy bitmap pixels into buffer.
mPixelBuffer.rewind();
temp.copyPixelsToBuffer(mPixelBuffer);
// Cleanup
temp.recycle();
temp = null;
flip = null;
}
Upvotes: 1
Views: 152
Reputation: 18413
Im not sure what section you are referring to by 'this block' so i assume you mean the whole method. You are creating objects inside this method
here
Paint paint = new Paint();
and here
Bitmap temp = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), flip, true);
so if this method is being called often you will see these objects GC'd quite alot. You should use an approach that does not require you to create objects on every call. For example you could create the bitmap
the first time the method is run and then manipulate the canvas
of that bitmap to achieve your flip.
You can grab the canvas
in the constructor and manipulate at the start of each method call with rotate(). This way you can just create the Bitmap
and Canvas
on first method call.
Also, whenever you create a Bitmap this can allocate video memory to this also, so its even more inefficient that it looks!
Upvotes: 1