Out momory a burst of dizzy memory and overflow. The pain is often on the phone, and the saying goes that the phone has to tighten its belt in memory.
Although now PC memory G is not strange, but on the phone can only be remembered by K, perhaps a comrade will immediately jump out of the M, remember that China is not rich most mobile phones are low-end. The mobile phone program makes me seem to be back to the DOS era (self-comfort that era has also been a lot of good master maybe I am next). It comes down to some of the following ways to do memory optimization. code optimization, picture optimization, third-party tool optimization, etc...
A Code optimization
Memory overflow certainly and the code can not escape the relationship, 99.99% learning Java people know that the garbage collector is a great advantage of Java and accordingly to ridicule C + +. Obviously this feature for code writers save a lot of things, but this feature has brought a lot of hidden dangers. For example, in the game often have different scenes of the switch, such as from the game logic back to the main menu logic, the game logic object attitude many people will choose to forget waiting for the garbage collector to collect the corpse ^_^. At first glance, there seems to be nothing wrong with the garbage collector. In fact, the garbage collector is not real time, it is not like the C + + DELETE statement immediately released unused memory. When you switch from game logic to main menu logic, two objects are present at the same time it is likely that memory will not be enough. Read here Many people will find that in fact the garbage collector in the J2ME is not how to use, from a point of view in the J2ME all garbage must be manually released, except simple type all objects must be explicitly empty such as imgs=null; In fact, Java provides a good tool for finding memory overflows, java.lang.Runtime.freeMemory (). It can return the current number of remaining memory, the appropriate placement in the code can be effective monitoring of memory usage.
A large part of the J2ME programmers were previously engaged in PC software development work, and ample memory masks many bad habits of writing code. As shown below:
A is not empty
A=new Logic ();
Many people may disagree with this, and they will think that new objects will flush out the old objects and free up memory. Here the bread contains two questions: 1. This code is the first to create the object and then the assignment operation, that is, there are two objects in the meantime, this is likely to produce overflow. 2. This will also hinder the work of the garbage collector.
A better formulation is as follows:
A=null;
A=new Logic ();
It's a bit of a hassle, but it's still necessary in J2ME. Then look at the following example.
DrawString ("Playtime:" + Time, 50,50,graphics.left| Graphics.top);
"Playtime:" + time is perfect in the paint () method each time is brushed to display on the screen. Crises tend to be hidden in beautiful looks, and this statement causes new memory reallocation to store "game time:" + time and then it must be released by the garbage collector, doubled, and prone to memory overflow. And so on, you should try to avoid repeating the definition of objects in a repeating method. Similar to the paint () method exists in the loop.
It would be more legitimate to put all the initialization of an object in a constructor, and most people usually do it by initializing the resources to be used by the current logic.
A large part of the memory overflow occurs in the constructor. The peak of memory usage is in the constructor, so avoiding this peak can effectively prevent overflow. The best way to recommend this is to initialize it the first time you use it. As shown below:
if (img==null) {
Class
}
Now do the game a lot of times need to map array, sound array, and some other resources many of these resources can be placed in the code can also be placed in the file.
It is highly recommended that these resources be placed in the file when required to enter in the load. These resource files, if placed in code, will take up a small amount of code space, and the code is usually loaded into memory as soon as the program runs.
In addition to the methods listed above, there are some familiar examples, such as closing unused RMS, shutting down unused network connections, and shutting down unused streams. Stop the thread correctly. Good program architecture to reduce code coupling is also a good way, no matter in the code mode, memory release can be very clear analysis.