# E/libEGL：在没有当前上下文的情况下调用 OpenGL ES API（每个线程仅记录一次）。 E/libGLESv2：GL_OUT_OF_MEMORY

- **ID:** `android/gl-error-out-of-memory`
- **领域:** android
- **类别:** runtime_error
- **错误码:** `GL_OUT_OF_MEMORY (0x0505)`
- **验证级别:** ai_generated
- **修复率:** 75%

## 根因

在调用 API 之前 OpenGL ES 上下文丢失或未正确初始化，通常是由于纹理内存耗尽或线程错误。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Android 8.0 | active | — | — |
| Android 9 | active | — | — |
| Android 10 | active | — | — |
| Android 11 | active | — | — |
| Android 12 | active | — | — |
| Android 13 | active | — | — |
| Android 14 | active | — | — |
| Android 15 | active | — | — |

## 解决方案

1. ```
   Ensure the GL context is current before any GL call: run all GL operations on the same thread where the GLSurfaceView.Renderer is active. Use GLSurfaceView.queueEvent() to post GL commands on the renderer thread. Example: glSurfaceView.queueEvent(() -> { GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); });
   ```
2. ```
   Reduce texture memory usage by compressing textures (e.g., use ETC2 or ASTC format) and calling GLES20.glDeleteTextures() for unused textures. Monitor with android.util.Log and check GLES20.glGetError() after each call.
   ```
3. ```
   Recreate the GL context when it's lost by overriding onSurfaceCreated() in the Renderer and reinitializing all textures and shaders there.
   ```

## 无效尝试

- **Increase heap size in the manifest with android:largeHeap="true"** — GL_OUT_OF_MEMORY is GPU memory related, not Java heap; largeHeap only affects Dalvik/ART heap, not GPU memory. (90% 失败率)
- **Reduce texture dimensions to 0x0 to free memory** — Zero-sized textures are invalid and cause GL errors; textures must have positive dimensions. (95% 失败率)
- **Call eglMakeCurrent in a background thread without synchronization** — OpenGL contexts are not thread-safe; making current from a background thread while the UI thread uses the same context causes crashes. (85% 失败率)
