[Pogamut-list] Full memory

Daniel pogamut-forum at artemis.ms.mff.cuni.cz
Thu Apr 23 12:18:53 CEST 2009


Re: Re: Re: Full memory

Author: Daniel

Hi Niels!

First at all, Jakub, don´t think I forgot to answer you post about paste my code, but I think I have fix everthing, but before share it I would like to try it a bit more, so please don´t get angry cause Im answering Niels first ;-P

Ok, Niels, Im not one of the Pogamut Lads, but I will try to help anyway, as we are working in the same stuff.

About Anonymous classes, we defenly have to get rid of them, I going to copy paste code which should work for you...at least is working for me...so far! (cross my fingers :) )

//// Wrong code, thats how I worked before: ////

(...)

protected void postPrepareAgent() throws PogamutException { 
        body.addTypedRcvMsgListener(misilComming, MessageType.INCOMMING_PROJECTILE);
}//end postPrepareAgent

(...)

    RcvMsgListener misilComming = new RcvMsgListener() {
        IncommingProjectile prj;
        @Override
        public void receiveMessage(RcvMsgEvent e) {
            log.log(Level.SEVERE," misil " +e.getMessage());
            prj = (IncommingProjectile) e.getMessage();            
        }
      
    }; //end of listener

}//end of the Agent class

//// And this is how Im working now, and as I said, is working so far.

(...)

 protected void postPrepareAgent() throws PogamutException { 
        MisilComming mc = new MisilComming();
        body.addTypedRcvMsgListener(mc, MessageType.INCOMMING_PROJECTILE);
}//end postPrepareAgent

(...)

    public class MisilComming implements RcvMsgListener{

        public MisilComming(){
        }

        IncommingProjectile prj;
        @Override
        public void receiveMessage(RcvMsgEvent e) {
            log.log(Level.SEVERE," misil " +e.getMessage());
            prj = (IncommingProjectile) e.getMessage();
            esquivaMisil(prj);
        }
}

}//end of the Agent class

///// End of codes ////

About garbage collection...well, any website would explain it much better than me, google it. But shortly: the whole story is about deleting objects, and when to do it. If Java didn´t use the garbage collection the memory would get full as you don´t remove objects manually in code. What is needed to remove and object? it has to be no referenced from anyone, that means not being used by any other object. However sometimes might happen that an object is still referenced by an old object, so not used anymore but is not removed, so here we have a memory leech...not sure I explained good enough, but the name of this is Memory Leak, if you want to google it :)

Some times, if the "forgotten" object is small, and is a normal aplication, nothing happens, but keep in mind that an Agent here is iterated like 4 times per second, so the small thing becomes a big deal after a while.

Hope I helped...if not, let me know and I will do my best to explain myself better.

Good luck Neils!



> Can you explain this problem more thouroughly? I think I have this problem, but I don't know for sure and I don't know how to fix it.
> I create anonymous Listeners to my agents in an Experiment class, and from the listener I reference the ExperimentDescriptor. I also created a reference to the Agent class from the AgentBody, can that be a problem?
> I don't really understand a lot about these references and the garbage collection, so I would be very glad if you could help me out here.
> 
> Thanks!
> 
> Niels





More information about the Pogamut-list mailing list