9 Tips for Java memory management

Source: Internet
Author: User

9 Tips for Java memory management
A lot of people say, "Java is finished, just wait for the death!" "And why?" The simplest example is that the Java system is very memory-friendly! When you hear that, there will be a lot of people coming out to defend Java, and a bunch of performance test reports to prove it. In fact, in theory, the Java system is not more memory than the system developed in other languages, why there are so many reasons to prove that it does occupy memory? Two words, bad habits.

1. Do not use the new Boolean ().

In many scenarios, a Boolean type is required, such as the Boolean type set and get in JDBC are passed through a Boolean wrapper, and most ORM uses a Boolean to encapsulate the Boolean type, for example:
The following is a reference fragment:
Ps.setboolean ("isClosed", New Boolean (true));
Ps.setboolean ("isClosed", New Boolean (isClosed));
Ps.setboolean ("isClosed", New Boolean (i==3));
Typically, the number of Boolean instances constructed in these systems is quite large, so the system is filled with a lot of Boolean instance small objects, which are quite memory-intensive. The Boolean class is actually just two instances long enough, an instance of true, an instance of false.
The Boolean class provides two static variables:
The following is a reference fragment:
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);
So you can save a lot of memory. It is believed that if the Java specification directly prescribes a Boolean constructor as private, this will never happen again.

2, do not use the new Integer.

Similar to Boolean, there are many occasions when an integer is used to encapsulate int in Java development, and usually the values represented by int are usually very small. The instantiation of the integer is optimized in the SUN SDK, and the integer class caches 128 to 127 of the 256 state integers, and if you use integer.valueof (int i), the incoming int range is exactly within this, returning a static instance. This will also greatly reduce the memory footprint if we use integer.valueof instead of the new integer. If your system is to be used in a different SDK (such as the IBM SDK), then you can do it yourself, such as integerutils.valueof (), so you can use this feature in any SDK.

3. Use StringBuffer instead of string to add.

I'm not going to talk about this because I've been told N times. I just want to put a joke is not a joke, I am looking at a domestic "famous" Java development of the web system source code, unexpectedly found that a large number of the use of string addition, a method of assembling SQL statements to construct the most nearly 100 string instances. No words in it!

4. Excessive use of hash tables

Developers with a certain development experience often use hash tables (a hash table is implemented in the JDK as HashMap) to cache some data, thus increasing the speed of the system. For example, the use of HashMap cache some material information, personnel information and other basic information, which improves the system speed, but also increased the system's memory, especially when the cache of more data. In fact, we can use the concept of caching in the operating system to solve this problem, that is, to allocate a cache of a certain size cache container, according to a certain algorithm to eliminate the need to continue to cache the object, on the one hand, because of the object cache and improve the system's operational efficiency, It also reduces the memory footprint of the system because the cache container is not infinitely expanded. Now there are many open source cache implementation projects, such as Ehcache, Oscache, etc., these projects have implemented FIFO, MRU and other common caching algorithms.

5, avoid too deep class hierarchy and too deep method call.
Because both are very memory-intensive (especially the method call is a big consumer of stack space).
6. Variables are defined and instantiated only when they are used.
7, try to avoid the use of static variables, in-class private constants can be replaced with final.
8. Using object pooling technology for frequently used objects
9, to ensure that each IO operation, connection timely shutdown

9 Tips for Java memory management

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.