Java Memory Management

Source: Internet
Author: User

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 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:
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, to allocate a cache container of a certain size to the cached container, according to a certain Algorithm Eliminate objects that do not need to be cached. On the one hand, this will improve the system's operation efficiency because of the object cache. At the same time, 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 the 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.

 

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.