Particle systems and memory leaks

Discussion in 'Programming' started by ZombieSquirrel, Oct 12, 2019 at 6:04 PM.

  1. ZombieSquirrel

    ZombieSquirrel Member

    Joined:
    Oct 18, 2018
    Posts:
    98
    Hi everyone.

    I'm currently making a weather system, with the current states:
    "sunny"
    "cloudy"
    "rain"
    "storm"
    "snow"
    "snowstorm"

    I can get it to work and it looks beautifull, but whenever I leave the game on too long, or stay in a room for too long, the game experiences more and more lag to the point of it becoming unplayable.
    My rooms are persistent.

    Basically, I have an emitter for each of the weather states.
    These are created in the ROOM START event. Room start and Room end should be fine right?
    It's a lot of code, and I won't post it here. Basically in the ROOM START event, I create the particle system, create the particles, define the parameters of the particles, define the regions, and create the emitters.

    At room start, the system, the particles,... are defined. Then in if statements, the emitters are created:

    If it's sunny, no emitter is created.
    If it's cloudy, only the cloud emitter.
    If it's rain or storm, clouds and rain are generated at different intensities.
    Same for snow, and snowstorm.

    What confuses me is this: in the ROOM END and CLEANUP events, I put this code:

    part_type_destroy(particleType_Rain);
    part_type_destroy(particleType_Snow);
    part_type_destroy(particleType_Splash);
    part_type_destroy(particleType_Cloud);

    part_emitter_destroy(particle_system, emitterRain);
    part_emitter_destroy(particle_system, emitterStorm);
    part_emitter_destroy(particle_system, emitterSnow);
    part_emitter_destroy(particle_system, emitterSnowstorm);
    part_emitter_destroy(particle_system, emitterCloud);

    part_system_destroy(particle_system);

    This should destroy everything I created in the room start event right? All the particles, all the emitters and the particle system are covered here.

    I did this to prevent the memory leaks and clean up memory, so I won't have lag.

    Yet somehow, it happens. After a long enough time, lag builds up and builds up,... Which leaves me to believe I didn't clean up the memory.

    Any ideas on how to properly clean up to prevent lag?
     
  2. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,703
    First of all, you might wanna check the game using the debug mode / profiler, it might help checking whether it's actually the particle system creating lag or not.

    Also, do you recreate and destroy the particle types all the time? Shouldn't you define those once at game-start and then never touch them again? I'm thinking that might be a potential cause (some resource leak bug for particle types because they're not expected to be destroyed and recreated a lot). Instead of destroying particle types, you should clear the particle systems instead (which removes all particles that currently exist in them). In my games, I generally only make one or two global particle systems (foreground and background, or just foreground if I don't have background particles) and then spawn every particle in either of those, because having to manage several independent systems and dynamically destroying and creating stuff felt like too much work.
     
    ParodyKnaveBob likes this.
  3. ZombieSquirrel

    ZombieSquirrel Member

    Joined:
    Oct 18, 2018
    Posts:
    98
    So destroying the particle types isn't necessary? Just the particle emitters?

    Also, they are only created at room start, and are only deleted at room end.
     
  4. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,703
    Destroying the particle type will remove the definition of e.g. the cloud particle from the game entirely, you probably want to destroy the individual particles instead (by clearing the particle systems). I'm thinking the leak is caused by either of these two things:
    • Definitions that are deleted aren't properly garbage-collected
    • Destroying a particle definition when particles of that type still are in use causes them to get "orphaned" and never properly removed when their lifetime ends
     
  5. ZombieSquirrel

    ZombieSquirrel Member

    Joined:
    Oct 18, 2018
    Posts:
    98
    Oooh indeed I haven't cleared the particle systems.
    Does this means particles keep existing if you don't clear the emitters before destroying them?
    What about just staying in a room for long? Do particle emitters always need to be cleared in certain time intervals, just to be sure that the particle count doesn't build up and up and up?
     
  6. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,703
    Emitters spawn particles, based on a previously defined particle type. Particles exist in your particle system with behavior based off a particle type. It's usually particles that take up the most processing power, simply because you spawn a ton of particles at once. Once a particle is created, you can't tell if it was created by an emitter, another particle (death / step particle creation parameter), or manually created by code.

    Just for the record, I don't use particle emitters either, I create particles manually... again, less things to micromanage and more control.
     
  7. ZombieSquirrel

    ZombieSquirrel Member

    Joined:
    Oct 18, 2018
    Posts:
    98
    Hmm I might have to look into it, how to create particles manually. It sounds harder to understand than using emitters.
     
  8. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,703
    Not difficult at all:
    upload_2019-10-13_12-21-40.png
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice