Java performance (1)-Java Programming Guidelines

Source: Internet
Author: User

Address: http://blog.csdn.net/jinhuiyu/archive/2008/12/10/3487584.aspx

This part includes Java coding and performance issues. This guidelines is not dedicated to application servers, but it is a common rule in many cases, for a complete analysis of Java coding best practices, see Java Blueprints.
Avoid Serialization and Deserialization)

Serialization and deserialization of an object are very CPU-consuming (CPU-intensive) processes, it is likely to slow down your system operation speed, use
The transient keyword is used to reduce data serialization. In addition, customize your readObject () and
In some cases, the writeOjbect () method also has good performance in multiple systems.

Use StringBuffer for string operations

This section is not translated.

Assign a Null value to a variable that is no longer used for a long time

Explicitly assigning Null values to unused objects for a long time will help Java's garbage collector identify that this part of memory can be safely recycled. Although Java supports memory management, however, this does not prevent memory leakage and excessive memory utilization.

If the application does not release the object reference, it may cause memory leakage, or it may prevent the Java garbage collector from recycling these objects, resulting in excessive memory usage. After an object is used up, a clear null value will make the Garbage Collector better recycle memory.

One way to detect memory leakage is to use Profiling Tools to take a memory snapshot (memory
Snapshots), an application without memory leakage will display a stable dynamic memory heap (Active heap memory) after garbage collection in a stable State ).

Define the method as Final as needed

The new dynamic optimization compiler can optimize internal or program interaction, even if the Java method is not defined as Final. The original intention of using the Final Keyword: For program architecture and maintainability.

Final can be used unless you are very sure that your method cannot be rewritten.

Define a constant as Static Final.

If you define constants as static final, the dynamic compiler can easily optimize some constant folding.

Avoid Finalizers

Adding the finally Method to the Code will make garbage collection more expensive and unpredictable. The Java virtual machine does not guarantee the running time of the finally method. The finally method does not necessarily guarantee that the method will be run before the program exits.

Define the method parameters as final

If the parameters of a method are not modified within the method, define them as final, generally, all variables are set to final if they are not modified or set in the method.

Synchronize again when necessary

Do not synchronize your code snippet or method unless this is required. To avoid testability bottlenecks, ensure that your synchronization code segment or method is as short as possible. If the data structure is not synchronous, use the Java collections framework instead of using more expensive options such as java. util. hashTable.

Use DataHandlers for SOAP Attachment

Using a javax. activation. DataHandler for a SOAP attachment improves the performance. This section has not been touched, skipped, and will be re-translated when used.

JAX-RPC specifies:
■ A mapping of certain MIME types to Java types.
■ Any MIME type is mappable to a javax. activation. DataHandler.
As a result, send an attachment (.gif or XML document) as a SOAP attachment to an RPC style
Web service by utilizing the Java type mappings. When passing in any of the mandated Java type
Mappings (appropriate for the attachment's MIME type) as an argument for the web service,
JAX-RPC runtime handles these as SOAP attachments.
For example, to send out an image/gif attachment, use java. awt. Image, or create
DataHandler wrapper over your image. The advantages of using the wrapper are:
■ Reduced coding: You can reuse generic attachment code to handle the attachments because
TheDataHandler determines the content type of the contained data automatically. This
Feature is especially useful when using a document style service. Since the content is known
At runtime, there is no need to make callto attachment. setContent (stringContent,
"Image/gif"), for example.
■ Improved Performance: Informal tests have shown that usingDataHandler wrappers
Doubles throughput for image/gif MIME types, and multiplies throughput
Approximately 1.5 for text/xml or java. awt. Image for image/* types.

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.