Cownew open-source-saving java program memory-general Linux technology-Linux programming and kernel information. The following is a detailed description. What is the impression of a java System? Memory usage! In this statement, more than N people stand 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 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:
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:
Public static final Boolean TRUE = new Boolean (true );
Public static final Boolean FALSE = new Boolean (false );
You only need to take the two variables as needed,
For example:
Ps. setBoolean ("isClosed", Boolean. TRUE );
So what should I do to create a boolean based on a Boolean variable like the two or three statements? You can use the static method provided by Boolean: Boolean. valueOf ()
For example:
Ps. setBoolean ("isClosed", Boolean. valueOf (isClosed ));
Ps. setBoolean ("isClosed", Boolean. valueOf (I = 3 ));
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) Replace string addition with StringBuffer. 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 are used to add, in a method for assembling SQL statements, a maximum of 100 string instances can be constructed. Speechless!
(4) Over-utilized hash tables, developers with certain development experience often use hash tables (one implementation of hash tables in JDK is HashMap) to cache some data, this improves 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.
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.