1 11 package org.eclipse.ui.internal.texteditor.quickdiff.compare.rangedifferencer; 12 13 18 public class LinkedRangeFactory { 19 20 27 public static class LowMemoryException extends Exception { 28 29 36 private static final long serialVersionUID= 3977582493823939894L; 37 38 41 public LowMemoryException() { 42 super(); 43 } 44 45 50 public LowMemoryException(String message) { 51 super(message); 52 } 53 } 54 55 58 private static final double THRESHOLD= 0.1; 59 62 private static final long CHECK_INTERVAL= 5000; 63 66 private static final long OBJECT_SIZE= 100; 67 70 private static final long MAXIMAL_INTERVAL_REQUIREMENT= CHECK_INTERVAL * OBJECT_SIZE; 71 74 private static final long MAX_MEMORY_CONSUMPTION= 10 * 1024 * 1024; 75 78 private static final long MAX_INSTANCES= MAX_MEMORY_CONSUMPTION / OBJECT_SIZE; 79 80 81 84 private LowMemoryException fLowMemoryException= new LowMemoryException(); 85 86 89 private long fCount= 0; 90 91 99 public LinkedRangeDifference newRange(LinkedRangeDifference next, int operation) throws LowMemoryException { 100 check(); 101 return new LinkedRangeDifference(next, operation); 102 } 103 104 110 private void check() throws LowMemoryException { 111 if (fCount % CHECK_INTERVAL == 0) { 112 113 Runtime runtime= Runtime.getRuntime(); 114 long maxMemory= runtime.maxMemory(); 115 long maxFreeMemory= maxMemory - (runtime.totalMemory() - runtime.freeMemory()); 116 117 if (((float) (maxFreeMemory - MAXIMAL_INTERVAL_REQUIREMENT)) / maxMemory < THRESHOLD) 118 throw fLowMemoryException; 119 } 120 if (++fCount >= MAX_INSTANCES) 121 throw fLowMemoryException; 122 } 123 } 124 | Popular Tags |