1 11 package org.eclipse.ui.internal.ide.application; 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 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 26 import org.eclipse.ui.internal.ide.Policy; 27 28 42 class IDEIdleHelper { 43 44 47 private static final int DEFAULT_GC_INTERVAL = 60000; 48 49 53 private static final int DEFAULT_GC_MAX = 8000; 54 55 59 private static final int GC_DELAY_MULTIPLIER = 60; 60 61 65 private static final int IDLE_INTERVAL = 5000; 66 67 71 private static final String PROP_GC = "ide.gc"; 73 77 private static final String PROP_GC_INTERVAL = "ide.gc.interval"; 79 84 private static final String PROP_GC_MAX = "ide.gc.max"; 86 protected IWorkbenchConfigurer configurer; 87 88 private Listener idleListener; 89 90 93 private long lastGC = System.currentTimeMillis(); 94 95 99 private int maxGC = DEFAULT_GC_MAX; 100 103 private int minGCInterval = DEFAULT_GC_INTERVAL; 104 105 108 private int nextGCInterval = DEFAULT_GC_INTERVAL; 109 110 private Job gcJob; 111 112 private Runnable handler; 113 114 118 IDEIdleHelper(IWorkbenchConfigurer aConfigurer) { 119 this.configurer = aConfigurer; 120 if (PlatformUI.getTestableObject().getTestHarness() != null) { 122 return; 123 } 124 String enabled = System.getProperty(PROP_GC); 125 if (enabled != null && enabled.equalsIgnoreCase(Boolean.FALSE.toString())) { 127 return; 128 } 129 Integer prop = Integer.getInteger(PROP_GC_INTERVAL); 131 if (prop != null && prop.intValue() >= 0) { 132 minGCInterval = nextGCInterval = prop.intValue(); 133 } 134 135 prop = Integer.getInteger(PROP_GC_MAX); 137 if (prop != null) { 138 maxGC = prop.intValue(); 139 } 140 141 createGarbageCollectionJob(); 142 143 final Display display = configurer.getWorkbench().getDisplay(); 145 handler = new Runnable () { 146 public void run() { 147 if (!display.isDisposed() && !configurer.getWorkbench().isClosing()) { 148 int nextInterval; 149 final long start = System.currentTimeMillis(); 150 if (!Platform.getJobManager().isIdle()) { 152 nextInterval = IDLE_INTERVAL; 153 } else if ((start - lastGC) < nextGCInterval) { 154 nextInterval = nextGCInterval - (int) (start - lastGC); 156 } else { 157 gcJob.schedule(); 158 nextInterval = minGCInterval; 159 } 160 display.timerExec(nextInterval, this); 161 } 162 } 163 }; 164 idleListener = new Listener() { 165 public void handleEvent(Event event) { 166 display.timerExec(IDLE_INTERVAL, handler); 167 } 168 }; 169 display.addFilter(SWT.KeyUp, idleListener); 170 display.addFilter(SWT.MouseUp, idleListener); 171 } 172 173 176 private void createGarbageCollectionJob() { 177 gcJob = new Job(IDEWorkbenchMessages.IDEIdleHelper_backgroundGC) { 178 protected IStatus run(IProgressMonitor monitor) { 179 final Display display = configurer.getWorkbench().getDisplay(); 180 if (display != null && !display.isDisposed()) { 181 final long start = System.currentTimeMillis(); 182 System.gc(); 183 System.runFinalization(); 184 lastGC = start; 185 final int duration = (int) (System.currentTimeMillis() - start); 186 if (Policy.DEBUG_GC) { 187 System.out.println("Explicit GC took: " + duration); } 189 if (duration > maxGC) { 190 if (Policy.DEBUG_GC) { 191 System.out.println("Further explicit GCs disabled due to long GC"); } 193 shutdown(); 194 } else { 195 nextGCInterval = Math.max(minGCInterval, GC_DELAY_MULTIPLIER * duration); 197 if (Policy.DEBUG_GC) { 198 System.out.println("Next GC to run in: " + nextGCInterval); } 200 } 201 } 202 return Status.OK_STATUS; 203 } 204 }; 205 gcJob.setSystem(true); 206 } 207 208 211 void shutdown() { 212 if (idleListener == null) { 213 return; 214 } 215 final Display display = configurer.getWorkbench().getDisplay(); 216 if (display != null && !display.isDisposed()) { 217 try { 218 display.asyncExec(new Runnable () { 219 public void run() { 220 display.timerExec(-1, handler); 221 display.removeFilter(SWT.KeyUp, idleListener); 222 display.removeFilter(SWT.MouseUp, idleListener); 223 } 224 }); 225 } catch (SWTException ex) { 226 } 228 } 229 } 230 } 231 | Popular Tags |