summaryrefslogtreecommitdiffstats
path: root/logo/src/xlogo/MemoryChecker.java
blob: d9e1d0ee8b820158ae3a403512247490960d5653 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* XLogo4Schools - A Logo Interpreter specialized for use in schools, based on XLogo by Loic Le Coq
 * Copyright (C) 2013 Marko Zivkovic
 * 
 * Contact Information: marko88zivkovic at gmail dot com
 * 
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free 
 * Software Foundation; either version 2 of the License, or (at your option) 
 * any later version.  This program is distributed in the hope that it will be 
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
 * Public License for more details.  You should have received a copy of the 
 * GNU General Public License along with this program; if not, write to the Free 
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA 02110-1301, USA.
 * 
 * 
 * This Java source code belongs to XLogo4Schools, written by Marko Zivkovic
 * during his Bachelor thesis at the computer science department of ETH Zurich,
 * in the year 2013 and/or during future work.
 * 
 * It is a reengineered version of XLogo written by Loic Le Coq, published
 * under the GPL License at http://xlogo.tuxfamily.org/
 * 
 * Contents of this file were initially written by Loic Le Coq,
 * a lot of modifications, extensions, refactorings have been applied by Marko Zivkovic 
 */

/**
 * Title : XLogo
 * Description : XLogo is an interpreter for the Logo
 * programming language
 * 
 * @author Loïc Le Coq
 **/
package xlogo;

import xlogo.messages.async.dialog.DialogMessenger;
import xlogo.storage.global.GlobalConfig;

/**
 * This class is a thread that prevents from memory Overflow <br>
 * Those problems could happen when a program loops indefinitely<br>
 * Eg:<br>
 * <br>
 * <tt>
 * To bad<br>
 * fd 1 rt 1 <br>
 * bad<br>
 * this lines will explode memory<br>
 * end <br>
 * </tt>
 * 
 * @author loic
 * 
 * @author Marko Zivkovic - Extensive tests and provocation of the memory usage error message have shown
 * that in XLogo4Schools, using a maximum 128MB heap, the memory threshold is never reached
 * (Or very, very late in a gigantic recursion).
 * 
 */
public class MemoryChecker extends Thread
{
	/**
	 * The main frame
	 */
	private Application	cadre;
	/**
	 * This boolean indicates if the thread has to continue.<br>
	 * If false, the thread will stop.
	 */
	private boolean		alive;
	
	/**
	 * Constructs the Memory Checker for the main Frame
	 * 
	 * @param cadre
	 *            the main Frame
	 */
	public MemoryChecker(Application cadre)
	{
		this.cadre = cadre;
	}
	
	private static int maxSleepTime = 10000;
	private static int minSleepTime = 500;
	private static int sleepRange = maxSleepTime - minSleepTime;
	
	/**
	 * The Run Method for the Thread
	 */
	public void run()
	{		
		/*
		 * Marko : I reduced the amount of calculations done in every iteration.
		 * Before it included 1 subtraction and 2 divisions and a fetch from GlobalConfig,
		 * now only 1 subtraction is used to determine consumed > consumptionThreshold
		 * and another long comparison was added to regulate frequency of memory checks.
		 */
		long consumptionThreshold =  GlobalConfig.getMemoryThreshold();
		long listenThreshold = (long) (0.8 * consumptionThreshold);

		
		/*
		 * I increased default sleeping time to about 10 seconds, because a check every 1 second is too much overhead for doing nothing useful.
		 * However, as soon as the listenThreshold is reached, the sleepTime will start to decrease.
		 * Only once the listenThreshold is reached,
		 * the MemoryChecker becomes important to prevent an OutOfMemoryError and start suggesting the garbage collector to work.)
		 */
		int sleepTime = maxSleepTime;
		
		alive = true;
		
		while (alive)
		{
			try
			{
				Thread.sleep(sleepTime);
			}
			catch (InterruptedException ignore)
			{}
			long free = Runtime.getRuntime().freeMemory();
			long total = Runtime.getRuntime().totalMemory();
			long consumed = (total - free);
			
			if (consumed >= listenThreshold)
			{
				sleepTime = (int) (maxSleepTime - consumed / total * sleepRange);
				
				/*
				 * Marko : 
				 * In XLogo, when the error dialog was displayed, it usually kept displaying several times, disrupting any further use of the interpreter.
				 * The only out was to restart the application. This indicates that, although heavy memory usage was detected by this thread,
				 * the garbage collector did not yet start to free memory from dead objects.
				 * (Discussions about GC in the Internet indicate that the Java GC starts working as soon as a heap generation gets full.
				 * It is possible that the young generation is not full enough when 0.9*MaxMemory is reached, thus the message keeps appearing all the time,
				 * because memory is not sufficiently cleaned up.)
				 * Therefore, at least suggest the GC that it would be a good moment for garbage collection, and help recover. That's the best we can do.
				 */
				System.gc();
			}
			else
				sleepTime = maxSleepTime; 
			
			if (consumed > consumptionThreshold)
			{
				cadre.error = true;
				alive = false;
				DialogMessenger.getInstance().dispatchError(Logo.messages.getString("erreur"),
						Logo.messages.getString("depassement_memoire"));
			}
		}
	}
	
	/**
	 * Causes the memory checker loop to break.
	 * @author Marko
	 */
	public void kill()
	{
		alive = false;
	}
	
}