网站开发的项目,网站开发文档总结,怎么进行网站开发,百度收录有什么好处web编程 端口分配我从Heinz Kabutz撰写的Java专家通讯中获得了这个技巧。 #xff08;对于所有想要了解JDK内容的Java开发人员来说#xff0c;这是绝对必要的#xff01;#xff09; 特别是对于编写低延迟代码的开发人员#xff0c;即使对于普通的Java代码#xff0c;分… web编程 端口分配 我从Heinz Kabutz撰写的Java专家通讯中获得了这个技巧。 对于所有想要了解JDK内容的Java开发人员来说这是绝对必要的 特别是对于编写低延迟代码的开发人员即使对于普通的Java代码分配也是您真正要避免的事情。 有关更多详细信息请参阅我以前的文章“ 优化的第一条规则 ”和“ 重新审视性能优化的第一条规则逃逸分析的效果 ”。 在本技巧之前我一直使用分析器来计算分配或者我想您可以使用对Runtime的调用来查看JVM已分配了多少堆内存。 使用MBean我们能够查询单个线程的分配情况。 这为我们提供了一种非常精确的方法来测量特定线程是否已分配以及是否分配了多少线程。 在为零分配进行编码的情况下可以在测试中包含对该代码的调用断言没有分配。 下面是一个简单的类您可以根据时事通讯的提示使用它。 您会注意到构造函数进行了校准以调整由bean本身创建的分配量。 还有一些防御性代码可确保仅从单个线程调用该类。 您可以调用方法markAllocations来查找自上一个标记以来已分配的字节数。 printAllocations是一种方便的方法用于打印从最后一个标记到标准输出的分配。 构造类之后将reset分配分配调用reset或调用markAllocations或printAllocations 。 在测试中您可能具有以下代码 Allocations measure new AllocationsMeasure();
...
//critical code
...
assertEquals(0, measure.markAllocations()); 以下是AllocationsMeasure完整代码 package util;import javax.management.*;
import java.lang.management.ManagementFactory;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;/*** Created by daniel on 06/07/2015.*/
public class AllocationMeasure {private final String GET_THREAD_ALLOCATED_BYTES getThreadAllocatedBytes;private final String[] SIGNATURE new String[]{long.class.getName()};private final String threadName Thread.currentThread().getName();private final Object[] PARAMS new Object[]{Thread.currentThread().getId()};private MBeanServer mBeanServer;private ObjectName name null;private AtomicLong allocated new AtomicLong();private long BYTES_USED_TO_MEASURE 336;private long tid;public AllocationMeasure(){tid Thread.currentThread().getId();try {name new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME);mBeanServer ManagementFactory.getPlatformMBeanServer();} catch (MalformedObjectNameException e) {e.printStackTrace();}//calibratefor (int i 0; i 100; i) {//run a few loops to allow for startup anomaliesmarkAllocations();}long callibrate threadAllocatedBytes();BYTES_USED_TO_MEASURE threadAllocatedBytes()-callibrate;reset();}public void reset(){if(tid ! Thread.currentThread().getId())throw new AssertionError(AllocationMeasure must not be used over more than 1 thread.);allocated.set(threadAllocatedBytes());}private long threadAllocatedBytes() {try {return (long)mBeanServer.invoke(name,GET_THREAD_ALLOCATED_BYTES,PARAMS,SIGNATURE);} catch (Exception e) {throw new IllegalArgumentException(e);}}public long markAllocations() {if(tid ! Thread.currentThread().getId())throw new AssertionError(AllocationMeasure must not be used over more than 1 thread.);long mark1 ((threadAllocatedBytes()-BYTES_USED_TO_MEASURE) - allocated.get());allocated.set(threadAllocatedBytes());return mark1;}public void printAllocations(CharSequence marker) {if(tid ! Thread.currentThread().getId())throw new AssertionError(AllocationMeasure must not be used over more than 1 thread.);long mark1 ((threadAllocatedBytes()-BYTES_USED_TO_MEASURE) - allocated.get());System.out.println(threadName allocated marker : mark1);allocated.set(threadAllocatedBytes());}public static void main(String[] args) {String TEST Test;AllocationMeasure allocationMeasure new AllocationMeasure();for (int i 0; i 1000; i) {allocationMeasure.reset();//allocationMeasure new AllocationMeasure();long mark1 allocationMeasure.markAllocations();if(mark1 0 )System.out.println(m1: mark1);}allocationMeasure.printAllocations(TEST);}
}翻译自: https://www.javacodegeeks.com/2015/07/measuring-allocations-programmatically.htmlweb编程 端口分配