KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > texteditor > quickdiff > compare > rangedifferencer > LinkedRangeFactory


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.texteditor.quickdiff.compare.rangedifferencer;
12
13 /**
14  * Memory-monitoring factory for <code>LinkedRangeDifference</code>.
15  *
16  * @since 3.0
17  */

18 public class LinkedRangeFactory {
19
20     /**
21      * Exception that is thrown after the minimal allowed free memory is reached.
22      * <p>
23      * This class is not intended to be serialized.
24      * </p>
25      *
26      */

27     public static class LowMemoryException extends Exception JavaDoc {
28
29         /**
30          * Serial version UID for this class.
31          * <p>
32          * Note: This class is not intended to be serialized.
33          * </p>
34          * @since 3.1
35          */

36         private static final long serialVersionUID= 3977582493823939894L;
37
38         /**
39          * Initialize without detail message.
40          */

41         public LowMemoryException() {
42             super();
43         }
44
45         /**
46          * Initialize with the given detail message.
47          *
48          * @param message the detail message
49          */

50         public LowMemoryException(String JavaDoc message) {
51             super(message);
52         }
53     }
54
55     /**
56      * Relative amount of memory that must be free in order to allow the creation of additional instances
57      */

58     private static final double THRESHOLD= 0.1;
59     /**
60      * Number of instantiations after which the amount of free memory is checked
61      */

62     private static final long CHECK_INTERVAL= 5000;
63     /**
64      * Considered maximal size of a difference object in bytes.
65      */

66     private static final long OBJECT_SIZE= 100;
67     /**
68      * The maximal memory requirement for the next round in bytes.
69      */

70     private static final long MAXIMAL_INTERVAL_REQUIREMENT= CHECK_INTERVAL * OBJECT_SIZE;
71     /**
72      * Allowed memory consumption in bytes.
73      */

74     private static final long MAX_MEMORY_CONSUMPTION= 10 * 1024 * 1024;
75     /**
76      * The maximal number of instances.
77      */

78     private static final long MAX_INSTANCES= MAX_MEMORY_CONSUMPTION / OBJECT_SIZE;
79
80
81     /**
82      * Preallocated low memory exception
83      */

84     private LowMemoryException fLowMemoryException= new LowMemoryException();
85
86     /**
87      * Number of instantiations
88      */

89     private long fCount= 0;
90
91     /**
92      * Create a new linked range difference with the given next range and operation.
93      *
94      * @param next the next linked range difference
95      * @param operation the operation
96      * @return the new linked range difference
97      * @throws LowMemoryException
98      */

99     public LinkedRangeDifference newRange(LinkedRangeDifference next, int operation) throws LowMemoryException {
100         check();
101         return new LinkedRangeDifference(next, operation);
102     }
103
104     /**
105      * After <code>CHECK_INTERVAL</code> calls check whether at least a fraction of <code>THRESHOLD</code>
106      * of the maximal available memory is free, otherwise throw an {@link LowMemoryException}.
107      *
108      * @throws LowMemoryException
109      */

110     private void check() throws LowMemoryException {
111         if (fCount % CHECK_INTERVAL == 0) {
112
113             Runtime JavaDoc 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