FWGS / xash3d-fwgs

Xash3D FWGS engine.
1.47k stars 224 forks source link

Use mmap to reduce memory usage #1030

Open MoeMod opened 1 year ago

MoeMod commented 1 year ago

https://github.com/FWGS/xash3d-fwgs/blob/ddc4d7666874ce050d33ba6cc5781f08180776c5/engine/common/mod_studio.c#L1093-L1096

Every model file loaded into memory with read+malloc+memcpy can be replaced with mmap, which helps to reduce physical memory usage for huge mods.

a1batross commented 1 year ago

In theory, we could use MAP_PRIVATE here, but I'm unsure about compatibility issues it can bring.

We could make testing branch with such changes, but then why you even need to make models this big?

nekonomicon commented 1 year ago

It can be useful in multiplayer mods with many weapon models like Counter Strike.

MoeMod commented 1 year ago

Our 1.6 GB apk has tons of models and sprites to load. Peak memory usage is 6GB (PSS) on Android and 2.6GB (Xcode) on iOS. Newest phone including even chinese phones with 8GB+ RAM can crash by OOM. We had tried these ways to reduce memory usage:

  1. mmap as titled to reduce physical RAM
  2. load textures only when bound to opengl to reduce VRAM and swap them out when unused
  3. calloc instead of malloc+memset to avoid all-zero dirty pages
  4. vm_copy instead of memcpy to kill redundant pages (darwin only)
  5. convert RGBA textures to ASTC format Maybe the most useful way is mmap here.