Understanding Asset Management in AppGameKit
File and Memory Handling
AppGameKit loads assets (images, sounds, models) from disk into memory using explicit load commands (e.g., LoadImage()
, LoadSound()
). Once loaded, assets persist until manually deleted or the app exits. Unlike engines with built-in garbage collection or asset reference tracking, AGK requires careful resource lifecycle management to avoid memory bloat.
Scene Management
AGK does not natively support scene graphs or lifecycle hooks. Scene transitions are implemented via custom logic, often using arrays or state machines. Poor handling of transitions (e.g., failing to clear sprites or resources) leads to increased CPU/GPU load and memory fragmentation over time.
Symptoms of Performance Issues
- FPS drops during or after scene transitions
- Increased load times when revisiting previous scenes
- Crashes on low-memory devices (especially mobile)
- Visual artifacts or duplicated sprites remaining after transitions
- Profilers show increased memory usage over time
Root Causes
1. Orphaned Assets After Scene Changes
Assets loaded but not deleted (e.g., images, sounds) stay in memory even if no longer used. If scenes are reloaded frequently, this leads to accumulation.
2. Sprite Leaks from Unmanaged Arrays
Sprites created dynamically and not deleted before reallocation or scene exit remain active, consuming GPU and CPU resources.
3. Blocking Loads on Main Thread
All asset loads in AGK are synchronous. Large images or audio files cause hitches and stutter when loaded mid-game without a loading screen or thread break.
4. Texture Memory Fragmentation
Frequent creation and deletion of differently sized images or text renderings can fragment texture memory, especially on devices with strict memory layouts.
5. Inefficient Use of Draw Calls
Using multiple full-screen images, unbatched sprites, or layered effects without grouping increases render time significantly.
Diagnostics and Debugging
1. Monitor Memory Usage
Print(ScreenFPS()) Print(GetMemoryUsage())
Use GetMemoryUsage()
periodically to track memory growth over scenes.
2. Validate Loaded Assets
Implement an asset manager to track loaded resources and verify DeleteImage()
, DeleteSprite()
, etc., are called correctly during scene transitions.
3. Use Profiling Builds
AGK Tier 2 (C++) allows integration with native profilers (e.g., Visual Studio Profiler, Instruments on macOS) to inspect memory allocation and CPU hotspots.
4. Visual Debugging of Sprite Count
Print(GetSpriteCount())
Check for unexpectedly high sprite counts after scene loads, indicating leaks or accumulation.
5. Track Scene State Changes
Log or output state machine transitions and time taken to complete asset load and cleanup.
Step-by-Step Fix Strategy
1. Create a Scene Cleanup Routine
for i = 1 to spriteCount DeleteSprite(spriteArray[i]) next for i = 1 to imageCount DeleteImage(imageArray[i]) next
Always delete resources and reset scene-specific data before entering a new scene.
2. Implement Lazy Loading or Preloading
Load assets during a splash screen or loading phase. Do not load large files during active gameplay.
3. Use Texture Atlases
Combine UI or animation sprites into atlases to reduce draw calls and avoid fragmenting texture memory.
4. Batch Similar Renderables
Group sprites with shared properties (e.g., same image) to minimize render state changes.
5. Isolate and Reuse Core Assets
Avoid reloading shared assets between scenes. Keep them in a persistent manager and only reload dynamic or scene-specific items.
Best Practices
- Track and log every
Load*
andDelete*
call to ensure balance - Use a central scene/state manager for transitions
- Use loading screens to prepare heavy assets off-frame
- Limit per-frame dynamic sprite creation
- Optimize all images for power-of-two dimensions when possible
Conclusion
AppGameKit’s simplicity is its strength, but it puts the onus on developers to manage memory, resources, and lifecycle. Without careful cleanup and optimization, performance degradation becomes inevitable in content-rich projects. By introducing systematic asset management, preloading strategies, and profiling discipline, developers can build smooth, scalable games that maintain performance across scene transitions and platform targets.
FAQs
1. Why does memory usage increase after each scene?
Because assets or sprites from previous scenes are not deleted. Use DeleteImage()
, DeleteSprite()
, and clear arrays explicitly.
2. Can AppGameKit load assets in the background?
No, asset loading is synchronous. Use a loading screen or staged frame loops to break up the work visually.
3. How do I reuse assets without reloading them?
Keep shared assets in a global asset manager and reference their IDs instead of reloading per scene.
4. Is Tier 2 better for performance-critical games?
Yes. Tier 2 (C++) allows for deeper system access, native debugging, and advanced memory profiling.
5. What causes stuttering during scene switches?
Usually large asset loads, unbatched sprite cleanup, or lack of proper scene reset logic. Preloading and systematic deletion help.