Java Performance Optimization Tips Highlights (3)

xiaoxiao2021-03-06  40

7. Generate the primary key

There are many ways to generate primary keys within EJBs, which analyzes several common methods and their characteristics. Use the identification mechanism built in the database (SQL Server's Identity or Oracle's Sequence). The disadvantage of this method is that EJB portability is poor. The primary key value (such as incremental operation) is calculated from the entity bean. Its disadvantage is that the transaction can be serialized and the speed is slow.

Use clock services such as NTP. This requires a local code for a particular platform to secure the bean to a particular OS. In addition, it also leads to such a possibility that two primary keys are generated within the same millisecond on the multi-CPU server. Borrow for Microsoft's ideas, create a GUID in the bean. However, if you do not help JNI, Java cannot determine the MAC address of the NIC; if you use JNI, the program is to depend on a specific OS.

There are several other ways, but these methods are the same limitations. It seems that only one answer is ideal: combined with RMI and JNDI. First bind the RMI remote object to the JNDI tree through the RMI registration. The client is looking for through JNDI. Below is an example:

Public Class KeyGenerator

Extends UnicastRemoteObject IMplements

Remote {private static long keyvalue = system.currenttimemillis ();

Public Static Synchronized long getKey ()

THROWS RemoteException {Return KeyValue ;}

8. Clear a session that no longer needs in time

In order to clear the no longer active session, many application servers have default session timeout, generally 30 minutes. When the application server needs to save more sessions, if the memory capacity is insufficient, the operating system shifts some memory data to disk, and the application server may also turn some unusual session according to the "most recent use" algorithm. Store to disk, may even throw "insufficient memory" exception. In a large-scale system, the cost of serialized sessions is very expensive. When the session is no longer needed, the httpsession.invalidate () method should be invoked to clear the session. HttpSession.INValidate () method can usually be called at the application's exit page.

9. Close the useless session in the JSP page

For those pages that do not have to track session status, shut down automatically created sessions can save some resources. Use the following Page instructions:

<% @ Page session = "false"%>

10. Servlet uses memory

Many developers are free to save a lot of information into the user session. Time, the object stored in the session does not have a timely manner to be recycled by the garbage collection mechanism. From the performance, the typical symptom is that the user feels that the system is periodically slow, but it is not possible to attribute the reason to any particular component. If you monitor the heap space of the JVM, its performance is that the memory is not properly larger. There are two main ways to solve such memory problems. The first method is to implement the HTTPSessionBindingListener interface in the BEAN of the session. Thus, as long as the valueUnbound () method is implemented, the resources used by the bean can be explicitly released.

Another way is to invalid the session as soon as possible. Most application servers have options for setting sessions as waste intervals. In addition, it is also possible to call the session's setMaxInactiveInterVal () method for programming, which is used to set the maximum interval of the client request allowed by the servlet container before invalidating sessions.

11. The HTTP Keep-Alive Keep-Alive function allows the client to the server-side connection to continuously valid. When there is a subsequent request to the server, the Keep-Alive function avoids the establishment or re-establishment. Most web servers on the market, including IPlanet, IIS, and Apache, HTTP Keep-Alive supports. This feature is usually useful for sites that provide static content. However, there is another problem with the burden heavier website: although there is a certain advantage of reserved the open connection, it also affects performance, because during the handling of the suspension, the resources that can be released are still occupied. . When the web server and the application server run on the same machine, the KEEP-Alive function has a high impact of resource utilization.

12. JDBC and Unicode

Think of you have learned some measures to improve performance when using JDBC, such as using the connection pool, correctly select the stored procedure and direct SQL, from the result set delete the extra column, pre-compiling the SQL statement, and so on. In addition to these obvious options, another good choice for improvement may be saved as Unicode (code page 13488). Java processes all data in Unicode, so the database driver does not have to perform a conversion process. But should be remembering: If this is used, the database will become larger because each Unicode character requires 2 byte storage space. In addition, if there are other non-Unicode program access to the database, performance issues still appear because the database driver still must perform a conversion process.

转载请注明原文地址:https://www.9cbs.com/read-61392.html

New Post(0)