Memory Management requires careful calculation-the computer newspaper has been released

Source: Internet
Author: User

Not afraid of exams

Pcw-chendx@vip.sina.com

Many questions will be prepared for recruitment related to programming. If you are not careful about the questions, the candidates will follow suit. Especially for recent graduates who do not have much work experience, the answers are often too theoretical, it is difficult to satisfy the Examiner. To this end, we have specifically launched this series. By analyzing the real questions, we can make everyone more practical in answering questions, so that the examiner can be satisfied and find a job smoothly.

 

Memory Management Careful calculation

Javami Studio Chen yuefeng

Memory Management is the focus of software development. Without changing functions, how to use memory more effectively and reduce resource usage is one of the important indicators to measure software performance. Therefore, many companies focus on memory management during recruitment.

 

Recruitment question: the following code (Java) Is the memory space correctly released? Why?

Image
IMG [] = new image [10];

For (int
I = 0; I <10; I ++ ){

IMG [I]
= Image. createimage ("/img/" + I + Images .png");

}

IMG
= NULL; // release the memory

 

Answer:This Code does not release all memory. The correct code for releasing all memory should be:

Image IMG [] =
New image [10];

For (int
I = 0; I <10; I ++ ){

IMG [I]
= Image. createimage ("/img/" + I + Images .png");

}

// Release the memory occupied by the image in sequence

For (int
I = 0; I <10; I ++ ){

IMG [I]
= NULL;

}

IMG
= NULL; // release the memory of the Image array

In the code, IMG = NULL only releases the memory occupied by the Image array object, and the memory occupied by each image object is not released. Therefore, if you need to release all the memory, the memory occupied by each image object must be released first, and then the memory occupied by the Image array can be correctly released.

 

Memory usage cannot be ignored

The examiner asked this question to test whether the programmer has experience and sufficient knowledge of memory management. Improper Memory Management During Software DevelopmentYesIf it is light, it will lead to low efficiency. If it is heavy, it may lead to system crash. Therefore, the design of memory management cannot be neglected.

In the development of mobile phone software, because mobile phone memory is not large, the requirements for memory management are more stringent. If you are interested in mobile phone software development, you must pay more attention and practice, increase the execution efficiency of the program so that you can stand out in the recruitment process.

 

Practical Application

In the actual development process, how is the memory managed? How can we improve the execution speed and efficiency? The following two sets of code are described. The function of the code is to save memory, and the effect of the first group of code is not good.

Code 1:

Public void paint (Graphics g ){

......

G. drawstring ("loading ...", 100,100, graphics. Top
| Graphics. Left );

......

}

Comments: The function of this Code is to draw the string "loading ...", Function can be implemented, but the system needs to generate a String object every time it is drawn, which is a waste of memory and the anchor value needs to be calculated every time, which will reduce the execution efficiency of the program.

 

Code 2:

Public void run (){

......

While (true ){

Int n = 10;

......

System. GC ();

}

}

Comment 2: In this code, an int Variable N is generated every time within the loop of the thread run. In this way, memory needs to be allocated for N each time, and the GC method is called every time at the end of the loop, low efficiency.

 

 

The second code can save memory.

Code 1:

Public static final int topleft =
Graphics. Top | graphics. Left; // avoid Repeated Computation

Private string loadstr = "loading ..."; // Generate a String object only once

Public void paint (Graphics g ){

......

G. drawstring (loadstr, 100,100, topleft );

......

}

Comments: The topleft constant is declared to avoid repeated computation, saving memory and improving program execution speed. By declaring the loadstr object, the string object is generated only once in the memory, saving memory usage.

 

Code 2:

Public void run (){

......

Int n = 10;

Runtime RT = runtime. getruntime ();

While (true ){

......

If (RT. freememory () <
102400) {// The available memory is less than 100 KB

System. GC ();

}

}

}

Comments: Declare the Variable N in the loop to save memory usage. In actual development, many people think that memory management is to use system. GC for memory cleaning, in fact, good memory usage habits are the most important, frequent calls to system. GC () can even significantly reduce the execution efficiency of the program. Therefore, the correct method is to call the GC method to clean up the memory when the available memory is smaller than a certain value.

 

Related Article

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.