为什么我的纹理看起来占用了这么多空间?
我的应用程序大量使用opengl,在操作期间产生以下堆状态*:
Used heap dump 1.8 MB
Number of objects 49,447
Number of classes 2,257
Number of class loaders 4
Number of GC roots 8,551
Format hprof
JVM version
Time 1:08:15 AM GMT+02:00
Date Oct 2, 2011
Identifier size 32-bit
但是,当我在手机上使用任务管理器查看我的应用程序的ram使用时,它说我的应用程序使用了44.42MB。堆大小使用和ram使用之间是否有任何关系?我认为42MB必须是我的开放式GL纹理,但我无法弄清楚为什么它们占用了这么多空间,因为在磁盘上所有文件一起只占用24MB(并且它们并非全部加载在同一个时间)。而且我甚至通过在纹理加载之前调整位图大小来使其中许多变小。我还动态创建一些纹理,但在使用后也会破坏这些纹理。
我正在使用OpenGL 1.0和Android 2.2,我用来加载纹理的典型代码如下所示:
static int set_gl_texture(Bitmap bitmap){
bitmap = Bitmap.createScaledBitmap(bitmap, 256, 256, true);
// generate one texture pointer
mGL.glGenTextures(1, mTextures, 0);
mGL.glBindTexture(GL10.GL_TEXTURE_2D, mTextures[0]); // A bound texture is
// an active texture
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0,GL10.GL_RGBA, bitmap, 0);
// create nearest filtered texture
mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_LINEAR); // This is where the scaling algorithms are
mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_LINEAR); // This is where the scaling algorithms are
mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
GL10.GL_CLAMP_TO_EDGE);
mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
GL10.GL_CLAMP_TO_EDGE);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
Log.v("GLSurfaceView", "Loading Texture Finished, Error Codes:"+mGL.glGetError());
return mTextures[0];
}
我用来加载位图的代码如下所示:
public int load_texture(int res_id){
if(mBitmapOpts==null){
mBitmapOpts = new BitmapFactory.Options();
mBitmapOpts.inScaled = false;
}
mBtoLoad = BitmapFactory.decodeResource(MyApplicationObject.getContext().getResources(),res_id, mBitmapOpts);
assert mBtoLoad != null;
return GraphicsOperations.set_gl_texture(mBtoLoad);
}
*使用mat分析hprof文件,eclipse ddms生成相同的数据
答案 0 :(得分:-1)
PNG是压缩图像。为了让OpenGL使用它们,必须对png进行解压缩。这种解压缩会增加内存大小。
您可能希望以某种方式减小某些纹理的大小。也许不使用512x512图像,而是使用256x256或128x128。您使用的某些纹理可能不需要太大,因为它们会进入屏幕尺寸有限的移动设备。