Joke Collection Website - Cold jokes - Several skills of memory management in Java language.
Several skills of memory management in Java language.
(1) Don't use newBoolean ().
In many cases, Boolean types are necessary. For example, set and get of boolean type of JDBC are all passed through Boolean encapsulation, and most ORM also use Boolean to encapsulate Boolean types, such as:
ps.setBoolean("isClosed ",new boolean(true));
ps.setBoolean("isClosed ",new boolean(is closed));
ps.setBoolean("isClosed ",new boolean(I = = 3));
Usually, 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. In fact, Boolean classes only need two instances, one is true and the other is false.
Boolean classes provide two static variables:
publistaticfinalbooleantrule = new boolean(true);
publistaticfinalbooleanflast = new boolean(false);
Only take these two variables when necessary,
For example:
Ps.setBoolean("isClosed ",Boolean type. True);
How about creating a Boolean value from a Boolean variable, like two or three sentences? You can use the static method provided by Boolean: Boolean.valueOf ().
For example:
ps.setBoolean("isClosed ",boolean . value of(is closed));
ps.setBoolean("isClosed ",boolean . value of(I = = 3));
Because the internal implementation of valueOf is: return(b? True: false);
This can save a lot of memory. I believe that if the Java specification directly designates the constructor of Boolean as private, this situation will never happen again.
(2) Don't use newInteger.
Similar to Boolean, there are many occasions when Integer is used to encapsulate int in java development, and the value represented by int is usually very small. The instantiation of Integer is optimized in SUNSDK, and the Integer class caches 256 integers from-128 to 127. If Integer.valueOf(inti) is used, and the range of the int passed in is within this range, a static instance is returned. In this way, if we use Integer.valueOf instead of newInteger, the memory occupation will also be greatly reduced. If your system is to be used in different SDK (such as IBMSDK), you can make your own tool package, such as IntegerUtils.valueOf (), so that you can use this feature in any SDK.
(3) Use StringBuffer instead of adding strings.
I won't say much about this, because I've said it n times. I just want to tell a joke that is not a joke. When I read the source code of a "famous" java-developed WEB system in China, I found that a large number of strings were added together, and at most nearly 100 string instances were constructed by a method of assembling SQL statements. Silence!
(4) Excessive use of hash tables
Developers with certain development experience often use hash table (one implementation of hash table in JDK is HashMap) to cache some data, thus improving the running speed of the system. For example, in java course, it is considered that using HashMap to cache some basic materials such as material information and personnel information will increase the system speed and memory occupation, especially when there are many cached materials. In fact, we can use
- Previous article:dirty joke
- Next article:Funny pictures of fate, please give me a funny picture
- Related articles
- Why is the box office of Crosstalk Movie I Want Happiness so bleak?
- What do girls mean when they laugh at boys and boys say you are stupid?
- Ask for a few hilarious jokes, the more the better, not the yellow ones! !
- What does the account manager of Metro Supermarket mainly do? Is it easy to do? Is there any development prospect?
- National Eye Love Day Essay
- How to do the project budget?
- Marry a widow and see what Sun Tzu's art of war is.
- How to choose the best letter of license plate number?
- My experiences in Yangpingguan Town, Hanzhong, Shaanxi Province
- What gift should I bring when I go to the woman's house to meet my parents for the first time?