Memory Management in Java

Source: Internet
Author: User

What is the impression that the Java system gives people? Account for Memory! Say it, there will be more than n people stand up to defend Java, and a bunch of performance test reports to prove this.

In fact, in theory, the Java system is not more memory than the system developed in other languages, then why there are so many n 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:

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:

public static Final Boolean true = new Boolean (true);

public static Final Boolean false = new Boolean (false);

Just take these two variables when you need them,

Like what:

Ps.setboolean ("isClosed", boolean.true);

So what if you want to create a Boolean based on a Boolean variable, like 2 or 3? You can use the static method provided by Boolean: Boolean.valueof ()

Like what:

Ps.setboolean ("isClosed", Boolean.valueof (isClosed));

Ps.setboolean ("isClosed", Boolean.valueof (i==3));

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 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. Job coordinates www.zhizuobiao.com

(3) Add stringbuffer instead of strings.

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 look at a domestic "famous" Java development of the source of the web system, unexpectedly found that a large number of the use of string addition, a method of assembling SQL statements to construct up to 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 hierarchies and too deep method calls.

Because both are very memory-intensive (especially the method call is a big consumer of stack space).

(6) A variable is defined and instantiated only when it is used.

(7) Try to avoid using static variables, the private constants within the class can be replaced by final.

Memory Management in Java

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.