1、Java 项目性能优化一良好编程习惯在 JAVA 程序中,性能问题的大部分原因并不在于 JAVA 语言,而是程序本身。养成良好的编码习惯非常重要,能够显著地提升程序性能。详细请参考:http:/ 批量处理其实从性能上考虑,它是很不可取的,浪费了很大的内存。从它的机制上讲,Hibernate 它是先把符合条件的数据查出来,放到内存当中,然后再进行操作。实际使用下来性能非常不理想。解决方案可分为以下四种:1:绕过 Hibernate API ,直接通过 JDBC API 来做,这个方法性能上是比较好的。也是最快的.2:运用存储过程。代价是移植性降低3:缓存。对于构建的业务系统,如果有些数据要经常要
2、从数据库中读取,同时,这些数据又不经常变化,这些数据就可以在系统中缓存起来,使用时直接读取缓存,而不用频繁的访问数据库读取数据。缓存工作可以在系统初始化时一次性读取数据,特别是一些只读的数据,当数据更新时更新数据库内容,同时更新缓存的数据值。 4:增加数据库连接池的大小。三Java 虚拟机堆和垃圾回收设置任何 Java 应用的性能调整基础都涉及到堆的大小和垃圾回收设置。(这里主要讨论 Sun HotSpor JVM).堆可分为三代,年轻的(新的),年老的和持久的。Hotspot JVM的内存基本配置包括最大堆大小,初始堆大小和年轻一代堆的大小。当配置最大堆大小时可参考下面一些指导:最大大小应小
3、于物理内存,避免虚存的页面调度。需要减去其他进程使用的内存 在负载测试时进行优化注重不要将最大堆大小设置得过大。堆越大,内存中保存的对象越多。内存中对象越多,回收过程时间越长。配置初试堆大小的一般性策略包括:将初始大小设置为最大堆大小将初始大小设置为最大堆大小的 1/4 到 1/2对于年轻一代堆大小,Sun 推荐是设置为最大堆大小的 1/3。也可以选择不同的垃圾回收算法。首先是增量垃圾回收。该算法的意思是减少单个对象回收停顿时间,这样的结果是整体回收性能的下降。该算法将相互引用的对象分组,然后尝试按组回收。尝试回收的部分越小,回收处理的时间往往会越少。四、Linux 内核的网络参数调整 在 l
4、inux 中,我们可以根据不同的需求来调整/proc/sys/net/ipv4/目录下的内核网络参数,通过合理的配置这里内核网络参数,从而达到提高网络的安全性和系统的稳定性的目的。对于流量很大的网站来说,默认的 Linux 内核网络参数偏小,因此如果你的网站流量非常大,或者上传下载大文件比较多,可以针对性的调整内核网络参数,扩大内核的 TCP 接收数据和发送数据的 Buffer 缓冲区大小。详细内核参数调优请参考:http:/ private ByteBuffer readBuffer = ByteBuffer.allocate(1024);/调整缓存的大小可以看到打印输出的变化 privat
5、e Map clientMessage = new java.util.concurrent.ConcurrentHashMap();private int clientCount = 0;/private ExecutorService readPool = Executors.newFixedThreadPool(100);/private ExecutorService writePool = Executors.newFixedThreadPool(100);public void start() throws IOException ServerSocketChannel ssc =
6、 ServerSocketChannel.open(); ssc.configureBlocking(false); ssc.socket().bind(new InetSocketAddress(“192.168.1.102“, 25); selector = Selector.open(); ssc.register(selector, SelectionKey.OP_ACCEPT); while (!Thread.currentThread().isInterrupted() selector.select(); Set keys = selector.selectedKeys(); I
7、terator keyIterator = keys.iterator(); while (keyIterator.hasNext() SelectionKey key = keyIterator.next(); if (!key.isValid() continue; if (key.isAcceptable() accept(key); else if (key.isReadable() new ReadThread(selector,key,clientMessage).run();/readPool.execute(new ReadThread(selector,key,clientM
8、essage); else if(key.isWritable()new WriteThread(selector,key,clientMessage).run();/writePool.execute(new WriteThread(selector,key,clientMessage);keyIterator.remove(); private void accept(SelectionKey key) throws IOException clientCount+;ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
9、 SocketChannel clientChannel = ssc.accept(); clientChannel.configureBlocking(false); clientChannel.register(selector, SelectionKey.OP_WRITE);ByteBuffer writeBuffer = ByteBuffer.wrap(“220 Email Server Ready.rn“.getBytes(); clientChannel.write(writeBuffer);clientChannel.register(selector, SelectionKey
10、.OP_READ);System.out.println(“a new client connected,Client:“+clientCount); public static void main(String args) throws IOException System.out.println(“Email Server started.“); new server().start(); public class ReadThread implements Runnableprivate Selector selector; private ByteBuffer readBuffer =
11、 ByteBuffer.allocate(1024);/调整缓存的大小可以看到打印输出的变化 private SelectionKey key;private Map clientMessage;public ReadThread(Selector selector,SelectionKey key,Map clientMessage)this.selector = selector;this.key = key;this.clientMessage = clientMessage;public void run() / TODO Auto-generated method stubSocke
12、tChannel socketChannel = (SocketChannel) key.channel(); / Clear out our read buffer so its ready for new data this.readBuffer.clear(); / Attempt to read off the channel int numRead; try numRead = socketChannel.read(this.readBuffer); if(numRead 0) byte newBytes = new bytenumRead; System.arraycopy(rea
13、dBuffer.array(), 0, newBytes, 0, numRead); clientMessage.put(socketChannel, newBytes); System.out.println(new String(newBytes); try socketChannel.register(selector, SelectionKey.OP_WRITE); catch (ClosedChannelException e) / TODO Auto-generated catch blocke.printStackTrace(); public class WriteThread
14、 implements Runnable private Selector selector; private SelectionKey key;private Map clientMessage;public WriteThread(Selector selector,java.nio.channels.SelectionKey key,Map clientMessage) this.selector = selector;this.key = key;this.clientMessage = clientMessage;public void run() / TODO Auto-gener
15、ated method stubSocketChannel socketChannel = (SocketChannel) key.channel(); byte newBytes = clientMessage.get(socketChannel);if(newBytes != null String response = “;if(command.toUpperCase().startsWith(“EHLO“)response = “250-mail.extmail.orgrn“ +“250-PIPELININGrn“ +“250-SIZE 10485760rn“ +“250-VRFYrn
16、“ +“250-ETRNrn“ +“250-STARTTLSrn“ +“250-AUTH LOGIN PLAINrn“ +“250-AUTH=LOGIN PLAINrn“ +“250-ENHANCEDSTATUSCODESrn“ +“250-8BITMIMErn“ +“250 DSNrn“;else if(command.toUpperCase().startsWith(“AUTH“)response = “334 VXNlcm5hbWU6rn“;else if(“dGlnZXJAZXh0bWFpbC5vcmc=rn“.equals(command)response = “334 UGFzc3
17、dvcmQ6rn“;else if(“c2hlbmZlaW5mbw=rn“.equals(command)response = “235 2.7.0 Authentication successfulrn“;else if(command.toUpperCase().startsWith(“MAIL“)response = “250 2.1.0 Okrn“;else if(command.toUpperCase().startsWith(“RCPT“)response = “250 2.1.5 Okrn“;else if(command.toUpperCase().startsWith(“DA
18、TA“)response = “354 End data with .rn“;else if(command.lastIndexOf(“rn.rn“) 0)response = “250 2.0.0 Ok: queued as 3C6332F8068Arn“;else if(command.toUpperCase().startsWith(“QUIT“)response = “221 2.0.0 Byern“;elseresponse = “334 errorrn“;ByteBuffer writeBuffer = ByteBuffer.wrap(response.getBytes(); try socketChannel.write(writeBuffer);socketChannel.register(selector, SelectionKey.OP_READ); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();clientMessage.put(socketChannel, new byte0);