web编程 端口分配_以编程方式衡量分配
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.html
web編程 端口分配
總結
以上是生活随笔為你收集整理的web编程 端口分配_以编程方式衡量分配的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 太原是几线城市 太原属于几线的城市
- 下一篇: 数据结构压缩_将数据压缩到数据结构中