How to save memory and improve efficiency during Java programming

Source: Internet
Author: User

Many people say, "Java is over, just wait for the decline !", Why? The simplest example is that the java system occupies very memory! When I hear this, many people will come up to defend Java and give a bunch of Performance Test reports to prove this. In theory, the Java System does not occupy more memory than the system developed in other languages. Why is there so many reasons to prove that it actually occupies the memory? Bad habits.

1. Do not use new Boolean ().

In many scenarios, the boolean type is required. For example, set and get of the Boolean Type in JDBC are passed through Boolean encapsulation. Most ORM also uses Boolean to encapsulate the boolean type, for example:

The following is a reference clip:

PS. setboolean ("isclosed", new Boolean (true ));

PS. setboolean ("isclosed", new Boolean (isclosed ));

PS. setboolean ("isclosed", new Boolean (I = 3 ));

Generally, the number of Boolean instances constructed in these systems is quite large, so the system is full of a large number of Boolean instance small objects, which consumes a lot of memory. The Boolean class actually only needs two instances, one being true and the other being false.

The Boolean class provides two static variables:

The following is a reference clip:

Public static final Boolean true = new Boolean (true );

Public static final Boolean false = new Boolean (false );

Because the internal implementation of valueof is: Return (B? True: false );

Therefore, it can save a lot of memory. I believe that this will never happen again if the Java specification directly sets the Boolean constructor to private.

2. Do not use new integer.

Similar to boolean, there are many cases where integer is used to encapsulate int in Java Development, and the values represented by INT are usually very small. The instantiation of integer is optimized in Sun SDK. The integer class caches integers in the 128 to 127 States. If integer is used. valueof (int I), the input int range is exactly within this, and the static instance is returned. In this way, if we use integer. valueof instead of new integer, the memory usage will be greatly reduced. If your system needs to be used in different sdks (such as the ibm sdk), you can encapsulate the tool class by yourself, such as integerutils. valueof (), so that this feature can be used in any SDK.

3. Use stringbuffer to replace string addition.

I will not talk about this because I have been told n times. I just want to add a joke that is not a joke. I saw the source code of a Chinese "Famous" Java-developed web system and found that a large number of strings were used to add, in a method for assembling SQL statements, a maximum of 100 string instances can be constructed. Speechless!

4. Misuse of hash tables

Developers with some development experience often use Hash Tables (hashmap is an implementation of hash tables in JDK) to cache some data, thus improving the system running speed. For example, hashmap is used to cache some basic materials such as material information and personnel information, which increases the system speed and memory usage, especially when there are many cached materials. In fact, we can use the cache concept in the operating system to solve this problem, that is, allocate a certain size of cache container to the cached container, objects that do not need to be cached are eliminated according to certain algorithms. On the one hand, the system's operation efficiency will be improved because the cache container is not expanded without limit, this reduces the system memory usage. There are many open-source cache implementation projects, such as ehcache and Oscache. These projects all implement common cache algorithms such as FIFO and MRU.

5. Avoid too deep class hierarchies and too deep method calls.

Because both of them occupy a lot of memory (especially method calls are the largest consumer of stack space ).

6. variables are defined and instantiated only when they are used.

7. Avoid using static variables whenever possible. Private constants in the class can be replaced by final.

8. Use the object pool technology for frequently used objects

9. Ensure that the connection is closed in time for each Io operation

Note: QQ technology exchange group: add one if you are interested in 108614806.

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.