Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

[Bug] Uncaught Failed to allocate memory!

Discussion in 'Project Tiny' started by Zoelovezle, Feb 28, 2019.

  1. Zoelovezle

    Zoelovezle

    Joined:
    Aug 7, 2015
    Posts:
    54
    Setup :
    1) Created a entity group (testcaseGroup => EntityGroup) .
    2) Added a empty entity with a component (testcaseTag => Component) with a data value (for testing used int32) .
    3) Created a Component System script , were i grabbed the entities which have testcaseTag attached .

    When does it appears :

    Case 1 : Destroyed grabbed entities & instantiated testcaseGroup
    Code (CSharp):
    1. namespace game {
    2.     /** New System */
    3.     export class testcase extends ut.ComponentSystem {
    4.      
    5.         OnUpdate():void {
    6.             let entities : ut.Entity[] = []
    7.             this.world.forEach( [ut.Entity , game.testcaseTag] , (entity , tag)=>{
    8.                 entities.push(entity)
    9.             })
    10.             for(let i = 0 ; i < entities.length ; i++)
    11.                     this.world.destroyEntity(entities[i])
    12.  
    13.             ut.EntityGroup.instantiate(this.world , "game.testcaseGroup")
    14.         }
    15.     }
    Error : The entity trying to destroy doesn't exist


    Case 2 : Added a exists check statement and then tried destroying . As it loops , the memory starts to increase thus casing lag on browser .
    Code (CSharp):
    1. namespace game {
    2.     /** New System */
    3.     export class testcase extends ut.ComponentSystem {
    4.        
    5.         OnUpdate():void {
    6.             let entities : ut.Entity[] = []
    7.             this.world.forEach( [ut.Entity , game.testcaseTag] , (entity , tag)=>{
    8.                 entities.push(entity)
    9.             })
    10.             for(let i = 0 ; i < entities.length ; i++) {
    11.                 if(this.world.exists(entities[i]))
    12.                     this.world.destroyEntity(entities[i])
    13.             }
    14.  
    15.             ut.EntityGroup.instantiate(this.world , "game.testcaseGroup")
    16.         }
    17.     }
    18. }
    Error : After meanwhile when the memory exceeds a certain amount , it throws the error Uncaught Failed to allocate memory!
     
  2. raymondyunity

    raymondyunity

    Unity Technologies

    Joined:
    Apr 30, 2018
    Posts:
    122
    Hey @Zoelovezle,

    Your memory is leaking because the system is always running and the EntityGroup in your system is being instantiated all the time. This happens in both of your snippets. If you're not using that entities array then you can instantiate and destroy your tag component in the same "for each" loop.

    You don't have to find if this entity exists because your system query will pick up the entity whenever it exists and matches the query of components.

    Code (CSharp):
    1.  
    2. OnUpdate():void {
    3.             this.world.forEach( [ut.Entity , game.testcaseTag] , (entity , tag)=>{
    4.                 console.log("instantiate");
    5.                 this.world.destroyEntity(entity);
    6.             })  
    7.         }
    8.  
    FYI: You don't even need to attach a "data value"/field to this tag component. It will still get picked up.
     
  3. Zoelovezle

    Zoelovezle

    Joined:
    Aug 7, 2015
    Posts:
    54
    Here's the situation . I have few effect groups . Whenever player hit's enemy i instantiate a effect group which has entity with animation sequence . Is there any other way of instantiating entities ? The only way i think of right now is reusing the instantiated effects as required .
     
  4. raymondyunity

    raymondyunity

    Unity Technologies

    Joined:
    Apr 30, 2018
    Posts:
    122
    Nope, that's the way, even instantiating prefabs is the same way.

    What you seem to be describing with the reuse of these "effect groups" will probably need a system that cleans up and reassigns "effect groups" to enemies that need them. With a bunch of components like "CleanUpEffectGroup", "ApplyEffectGroup", "EffectGroupPool" (holds an array of instantiated EffectGroups to recycle) and an "EffectGroupSystem" you can create something you described.
     
    Zoelovezle likes this.
  5. Zoelovezle

    Zoelovezle

    Joined:
    Aug 7, 2015
    Posts:
    54
    Great ! :)