1 11 package org.eclipse.ui.internal.ide; 12 13 import org.eclipse.core.runtime.IProgressMonitor; 14 import org.eclipse.core.runtime.IStatus; 15 import org.eclipse.core.runtime.Platform; 16 import org.eclipse.core.runtime.Status; 17 import org.eclipse.core.runtime.jobs.Job; 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.SWTException; 20 import org.eclipse.swt.widgets.Display; 21 import org.eclipse.swt.widgets.Event; 22 import org.eclipse.swt.widgets.Listener; 23 import org.eclipse.ui.PlatformUI; 24 import org.eclipse.ui.application.IWorkbenchConfigurer; 25 26 40 class IDEIdleHelper { 41 42 45 private static final int DEFAULT_GC_INTERVAL = 60000; 46 47 51 private static final int DEFAULT_GC_MAX = 8000; 52 53 57 private static final int GC_DELAY_MULTIPLIER = 60; 58 59 63 private static final int IDLE_INTERVAL = 5000; 64 65 69 private static final String PROP_GC = "ide.gc"; 71 75 private static final String PROP_GC_INTERVAL = "ide.gc.interval"; 77 82 private static final String PROP_GC_MAX = "ide.gc.max"; 84 protected IWorkbenchConfigurer configurer; 85 86 private Listener idleListener; 87 88 91 private long lastGC = System.currentTimeMillis(); 92 93 97 private int maxGC = DEFAULT_GC_MAX; 98 101 private int minGCInterval = DEFAULT_GC_INTERVAL; 102 103 106 private int nextGCInterval = DEFAULT_GC_INTERVAL; 107 108 private Job gcJob; 109 110 private Runnable handler; 111 112 116 IDEIdleHelper(IWorkbenchConfigurer aConfigurer) { 117 this.configurer = aConfigurer; 118 if (PlatformUI.getTestableObject().getTestHarness() != null) { 120 return; 121 } 122 String enabled = System.getProperty(PROP_GC); 123 if (enabled != null && enabled.equalsIgnoreCase(Boolean.FALSE.toString())) { 125 return; 126 } 127 Integer prop = Integer.getInteger(PROP_GC_INTERVAL); 129 if (prop != null && prop.intValue() >= 0) { 130 minGCInterval = nextGCInterval = prop.intValue(); 131 } 132 133 prop = Integer.getInteger(PROP_GC_MAX); 135 if (prop != null) { 136 maxGC = prop.intValue(); 137 } 138 139 createGarbageCollectionJob(); 140 141 final Display display = configurer.getWorkbench().getDisplay(); 143 handler = new Runnable () { 144 public void run() { 145 if (!display.isDisposed() && !configurer.getWorkbench().isClosing()) { 146 int nextInterval; 147 final long start = System.currentTimeMillis(); 148 if (!Platform.getJobManager().isIdle()) { 150 nextInterval = IDLE_INTERVAL; 151 } else if ((start - lastGC) < nextGCInterval) { 152 nextInterval = nextGCInterval - (int) (start - lastGC); 154 } else { 155 gcJob.schedule(); 156 nextInterval = minGCInterval; 157 } 158 display.timerExec(nextInterval, this); 159 } 160 } 161 }; 162 idleListener = new Listener() { 163 public void handleEvent(Event event) { 164 display.timerExec(IDLE_INTERVAL, handler); 165 } 166 }; 167 display.addFilter(SWT.KeyUp, idleListener); 168 display.addFilter(SWT.MouseUp, idleListener); 169 } 170 171 174 private void createGarbageCollectionJob() { 175 gcJob = new Job(IDEWorkbenchMessages.IDEIdleHelper_backgroundGC) { 176 protected IStatus run(IProgressMonitor monitor) { 177 final Display display = configurer.getWorkbench().getDisplay(); 178 if (display != null && !display.isDisposed()) { 179 final long start = System.currentTimeMillis(); 180 System.gc(); 181 System.runFinalization(); 182 lastGC = start; 183 final int duration = (int) (System.currentTimeMillis() - start); 184 if (Policy.DEBUG_GC) { 185 System.out.println("Explicit GC took: " + duration); } 187 if (duration > maxGC) { 188 if (Policy.DEBUG_GC) { 189 System.out.println("Further explicit GCs disabled due to long GC"); } 191 shutdown(); 192 } else { 193 nextGCInterval = Math.max(minGCInterval, GC_DELAY_MULTIPLIER * duration); 195 if (Policy.DEBUG_GC) { 196 System.out.println("Next GC to run in: " + nextGCInterval); } 198 } 199 } 200 return Status.OK_STATUS; 201 } 202 }; 203 gcJob.setSystem(true); 204 } 205 206 209 void shutdown() { 210 if (idleListener == null) { 211 return; 212 } 213 final Display display = configurer.getWorkbench().getDisplay(); 214 if (display != null && !display.isDisposed()) { 215 try { 216 display.asyncExec(new Runnable () { 217 public void run() { 218 display.timerExec(-1, handler); 219 display.removeFilter(SWT.KeyUp, idleListener); 220 display.removeFilter(SWT.MouseUp, idleListener); 221 } 222 }); 223 } catch (SWTException ex) { 224 } 226 } 227 } 228 } | Popular Tags |