【发布时间】:2011-10-02 06:35:14
【问题描述】:
为什么我的纹理看起来占用了这么多空间?
我的应用大量使用 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。堆大小使用和内存使用之间有什么关系吗?我认为这 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);
}
*hprof文件使用mat分析,eclipse ddms生成相同数据
【问题讨论】: