Java learning: Solves the memory leakage in j2s

Source: Internet
Author: User

I don't know if the theme is "Memory leakage in j2s' is appropriate", but if the Resource Creation and release problems are not well handled in j2s, memory leakage in C/C ++ may also occur.

The most commonly used resources in j2m's are images and sounds. To speed up the running of games, we usually declare them as global variables. Due to the memory of the mobile phone, we cannot load these resources into the memory at the same time, therefore, we will import resources (create objects) when using them, and assign null values to resources (images or sounds) that are not currently needed (of course, this is my practice, I don't know if it's the same as everyone else ). If the processing is poor during this period, some resource objects will not be cleared in the memory, which will cause memory leakage. As a result, the available memory will become smaller and smaller. The following is an example.

1. Image Resources

Key Point: declare an object variable for each image resource
Assume thatProgramTwo image objects are declared:
Image pic1, pic2;
If you want to use pic1 and pic2 at a certain time point:
If (pic1 = NULL) pic1 = image. createimage ("/1.png ");
If (pic2 = NULL) pic2 = image. createimage ("/2.png ");
When these images are used up, you should:
Pic1 = NULL;
Pic2 = NULL;
If the consumer uses two additional images 3.png and 4.png, it is better to declare two variable objects pic3 and pic4 to import them. Do not use the pic1 and pic2 variables to import these two images, for example:
If (pic1 = NULL) pic1 = image. createimage ("/3.png"); // do not do this
If (pic2 = NULL) pic2 = image. createimage ("/4.png"); // do not do this
This will affect the collection of pic1 and pic2 objects by the garbage collector, resulting in memory collection being unclean.
2. Sound resources (different platforms have different processing methods for sound)

Key points: Like image resources, in addition, sound playback must be stopped when sound resources are recycled; in addition, you must first judge the sound status before each start to play the sound (simple, directly call stop to stop the sound and then play it again. If you do not stop playing the sound first and then call it to run the playback, an object that cannot be recycled may be generated (this is my guess) memory leakage.

For example:
Assume that sound is the sound object.
Release sound resources:
If (sound! = NULL)
{
Sound. Stop ();
Sound = NULL;
}
In this way, the sound resources can be safely reclaimed by the garbage collector.
Play Sound resources:
If (sound! = NULL)
{
Sound. Stop ();
Sound. Start ();
}
The above is my experience. If something is wrong, please forgive me.
 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.