This site requires JavaScript, please enable it in your browser!
Greenfoot back
welleid
welleid wrote ...

2013/4/14

Releasing memory

welleid welleid

2013/4/14

#
Hi, I am currently dealing with memory issues. I often get Java error messages. I want to know, first, is there a way releasing memory used by an actor once with used the removeObject() method or is it automatically done, And secondly is there a way we can release the memory used by a world. I have three game levels, and when you finish one, you go to the next one clicking on a button using setWorld( new ...World() ); (not sure about the structure, but that's the idea^^) So I think the old one is still there, using memory... Thanks a lot !
Gevater_Tod4711 Gevater_Tod4711

2013/4/14

#
In java there is no cleaning up of memory like in C++ or other languages because a manual relese of memory sometimes works better but most times is a high risk because you can be shure that you forget some parts of the code if you code gets more and more complex. So in java there is the GarbageCollector that collects all object that aren't referenced. All these objects can never be reached and so they aren't needed anymore and they get deleted by the GC. If the GC delets an object the memory of this object again is free for new objects. This way it's much easyer for the programmer because you don't have to care about all the data waste that isn't needed anymore. If you delete an object in Greenfoot using removeObject the references in the world are deleted and the GC will collect them so their memory is free again. The same applies to the setWorld method. If the world isn't needed anymore it gets deleted.
welleid welleid

2013/4/15

#
Ok so how could I get rid off those memory problems ? Cause I know I use a lot of different actors in my game, but whenever they're no use anymore, I use the removeObject() method to get rid of it. And is it only when I play it on Greenfoot ? or will I still have problems when someone plays it once I exported it, and put it on green foot website for example ? Thanks a lot !
Gevater_Tod4711 Gevater_Tod4711

2013/4/15

#
You can't realy get rid of this problems. If you delete an object using the removeObject() method the object is deleted and the memory again is free to get used by other objects. Probably you often get this error: OutOfMemoryError: Java heap space. But this is just because greenfoot takes not very much space of the RAM. It could get more but is doesn't take it because in normal cases greenfoot games are very small and they don't need much RAM. So this problem will always be there. If you export your programm into a jar file or in the webside it's still a java application and so the GC deletes the objects that aren't needed anymore. So if you use the removeObject method you aren't doing anything wrong. The GC will delete all the objects that aren't needed for you. If you get this errors (OutOfMemory) you will have to restart greenfoot. The only thing you can do against it is not to initialise all the objects you need in one act cycle. If you slow it down the programm has more time for it and you will not get these errors that often.
danpost danpost

2013/4/15

#
It is possible that you have something misplaced in your code that is causing memory to be used up quickly. Some things you might try to ease the situation are: (1) cut down on amount or size of sound and image files (2) ensure that the creation of objects are regulated properly (3) reset any un-used object fields to 'null' when done with them (4) avoid multiple classes for objects that have minor differences. For example, if you have 4 buttons ('Start', 'About', 'Info', and 'Quit'), do not create a seperate class for each one. Use the same class with different values within their String 'caption' fields. Same thing if the objects are just of different colors, or even images. If they have basically the same functionality, then use the same class.
welleid welleid

2013/4/15

#
Thanks for your quick answers ! Basically, my game is a Breakout one that I have to do for school project. I made the graphics myself so perhaps should I down a bit the quality, even if it is already poor for me (under 300Ko for bg). In fact, I set three levels, bonus, different balls... So it does a consequent amount of classes, even if I use as Danpost says, one class for all the buttons and whenever I can. But I dont understand what you mean by point 3, danpost. Thanks a lot ! EDIT : oh yeah and I often have another problem linked to that memory bug, when I open my project, it says that it was coded with an old version, before Greenfoot 0.5. I began the project less than one month ago^^' And by the way, I have another question. I think it's simple but I dont know how to do it. I'm doing a counter inspired from the tutorial. But I have a function to do the score, and i need to know in which world i'm in, from an actor class. My function is like that :
Espace EspaceWorld = (Espace) getWorld();
        Score score = EspaceWorld.getScore();
            
        score.getTheScore(amountOfScore);
where Espace is my world. But when I go to second level, this function crashes. Thanks a lot !
danpost danpost

2013/4/15

#
It might be easier to sub-class your worlds with a super-class containing the method 'getScore' and any other methods that all your worlds may use. Then you can use:
SuperClass superClass = (SuperClass) getWorld();
Score score = superClass.getScore();
// etc.
and it would not matter what sub-world you were in.
danpost danpost

2013/4/15

#
@welleid, I left a message for you on my Private Messaging scenario.
You need to login to post a reply.