Recommendations for Java in-memory optimization

Source: Internet
Author: User

1. Use final
The string type in the JDK is final, so the method inside the string is final, and when the method is declared final, the Java compiler looks for the opportunity to inline all the final methods (as to how inline is determined by the compiler), which can greatly improve the performance of the program.
Add: The hotspot virtual machine uses method inline to optimize the operation efficiency of the virtual machine, and another strategy is hotspot optimization.
2. Set the expiration reference to NULL
When you determine that an object is not being used, you should set it to null, which can speed up the collection of objects. For example, after the stack in the JDK is out of the stack, the objects in the stack become outdated references, and if these objects are not recycled, the
Stack and out-of-stack operations, and there is a memory leak problem.
3, in time to close the finaly inside the resources
(1) IO streams not only allocate resources in memory in the JVM, but also allocate resources on the operating system, and the JVM can reclaim resources allocated in memory, but the resources allocated on the operating system cannot be reclaimed. Therefore, the close () method needs to be called to reclaim the operating system's resources. Similar to the thread and bitmap (objects encapsulated in Android).
(2) When bitmap operation, the Recycle () method should be called in time to recover resources and avoid memory overflow. From Bitmapfactory (object in Android) source code can know that bitmap object generation is through JNI technology,
The lower level calls the C language. Therefore, in order to reclaim the bitmap object, the Recycle method is needed to free the memory of the part of the C language code, and the garbage collector cannot reclaim that part of the memory.
(3) Recovery in Finally, can ensure the correct closing of resources. Otherwise, there may be a situation where the resource is not properly shut down, such as an exception, which could lead to a resource leak.
4. Try to cache frequently used objects
Use frequently used objects as much as possible for caching, typically using arrays or hashmap these containers. Caching in this way can result in excessive cache usage in the system, which degrades the performance of the system. Therefore, you can use some third-party cache items, such as Ehcache and Oscache, which use some caching algorithms such as FIFO (first-in-a-go algorithm), LRU (Least recenly used least recently used algorithm), and so on.
There may be a problem after you implement the cache object, such as using the HashMap cache object, as the cached objects increase, there may be a memory overflow problem, this time can use soft reference to avoid the problem of oom. The caching algorithm can be used to solve the performance problem. In the Imageloader framework, both soft-referencing techniques and related cache algorithms are used.

Recommendations for Java in-memory optimization

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.