Software Engineering for Students A Programming Approach
Software Engineering for Students A Programming Approach Software Engineering for Students A Programming Approach
15.9 Garbage collection 215 dangerous because it can lead to major errors (or subtle but dangerous errors). The pointer is often mentioned in the same sentence as the infamous goto statement as a potential source for obtuse and error-prone code. A number of issues should be considered when evaluating a language’s implementation of pointers. Since the same data object may be referenced through more than one pointer variable, care must be taken not to create a “dangling” pointer. That is, a pointer which references a location that is no longer in use. Does the language provide any assistance in reducing the opportunities for such errors? The security of pointers is enhanced in such languages as Ada and Java, which require the programmer to bind a pointer variable to reference only objects of a particular type. Programs written in such languages as C and C++, which allow pointers to dynamically reference different types of object, are notoriously awkward to debug. What provisions (e.g. scoping mechanisms, explicit programmer action or garbage collection procedures) does the language provide for the reclamation of space which is no longer referenced by any pointer variable? This issue is discussed below. In Java, the program has no explicit access to memory addresses and it is therefore impossible for such a program to make the kind of mistake possible in C++. When a Java program needs memory, it creates a new object. For example, a program can instantiate an object of type Button by: Button aButton = new Button("Press here"); This creates a pointer to the new object aButton. In Java this pointer is termed a reference, but there is no way in which the Java program can misuse this pointer. For example, arithmetic is not permitted on a reference, nor can the pointer be used to refer to an object of another class. (Both these operations are allowed in a C++ program.) Thus the Java program is prevented from causing a whole class of subtle and dangerous errors. 15.9 ● Garbage collection A subtle source of errors can arise when memory is freed (or not) after being allocated to hold some dynamic data structure. In C++, the programmer explicitly issues a function call to free memory. The memory manager then adds the retrieved memory to its pool of available memory; this process is termed garbage collection. When used incorrectly, two types of errors can arise: 1. memory leaks – memory is no longer in use, but has not been reclaimed by the memory manager 2. memory corruption (dangling pointer) – memory has been returned from use, but is still in use. In a memory leak, a program acquires some memory, uses it, but then fails to return it for garbage collection. This memory is thereby rendered useless. In a program that only runs for a short time, the memory is reclaimed when the program
216 Chapter 15 ■ Object-oriented programming terminates, so that there is no great problem. However, if the program is a component in a real-time system, it may have an effectively infinite lifetime, in which case memory loss is serious. In memory corruption, a program acquires some memory, uses it, returns it for garbage collection, but then continues to use it. This is, of course, a programming error, but in large complex programs such a mistake is not unusual. The memory management system may now allocate this same memory area to some other program (or to the same program). The consequence is that two programs are now using the same area of memory unknown to each other. This tends to result either in a program crash – if we are lucky – but often the result is some subtle error, which manifests itself in some strange manner, some time after the crime has been committed. For example, some data has become mysteriously corrupted. In such a situation, debugging becomes a nightmare. In Java, the garbage collection system periodically and automatically checks for objects that are no longer in use. It then frees any available memory. Thus the programmer is freed from the task of keeping track of what memory is in use and many potential errors are therefore avoided. The disadvantage is that the programmer has limited control over when the garbage collector does its work. This might be done in a variety of ways, depending on the implementation: ■ at periodic time intervals ■ when available memory is exhausted ■ never (planning that demand will not exceed supply) ■ when a program explicitly requests it. The garbage collector needs a stable situation in order to analyze and collect unused memory and therefore an implementation will normally freeze all running programs when the garbage collector goes into action. This means that programs may be suspended at unpredictable times. For some applications this is probably acceptable. However, for real-time programs, sudden unpredictable stops are unacceptable and a special attention to scheduling the garbage collection is required. In summary, C++ supports explicit allocation and deallocation of memory, with explicit access to memory pointers. This is power with considerable responsibility. In Java, allocation and deallocation is implicit and automatic, with no access to memory pointers. This avoids a notorious class of programming bugs. SELF-TEST QUESTION 15.10 Draw up a table that compares the memory allocation scheme of C++ with that of Java according to the criteria software reliability, development effort and performance (run-time speed).
- Page 188 and 189: CHAPTER 13 Refactoring This chapter
- Page 190 and 191: 13.3 ● Move Method 13.6 Inline Cl
- Page 192 and 193: class Sprite Instance variables x y
- Page 194 and 195: Summary Summary 171 it is making po
- Page 196: PART C PROGRAMMING LANGUAGES
- Page 199 and 200: 176 Chapter 14 ■ The basics and a
- Page 201 and 202: 178 Chapter 14 ■ The basics > > >
- Page 203 and 204: 180 Chapter 14 ■ The basics > Ear
- Page 205 and 206: 182 Chapter 14 ■ The basics > Cas
- Page 207 and 208: 184 Chapter 14 ■ The basics > > >
- Page 209 and 210: 186 Chapter 14 ■ The basics > } }
- Page 211 and 212: 188 Chapter 14 ■ The basics Unfor
- Page 213 and 214: 190 Chapter 14 ■ The basics Ada d
- Page 215 and 216: 192 Chapter 14 ■ The basics The w
- Page 217 and 218: 194 Chapter 14 ■ The basics In a
- Page 219 and 220: 196 Chapter 14 ■ The basics > str
- Page 221 and 222: 198 Chapter 14 ■ The basics Answe
- Page 223 and 224: CHAPTER 15 Object-oriented programm
- Page 225 and 226: 202 Chapter 15 ■ Object-oriented
- Page 227 and 228: 204 Chapter 15 ■ Object-oriented
- Page 229 and 230: 206 Chapter 15 ■ Object-oriented
- Page 231 and 232: 208 Chapter 15 ■ Object-oriented
- Page 233 and 234: 210 Chapter 15 ■ Object-oriented
- Page 235 and 236: 212 Chapter 15 ■ Object-oriented
- Page 237: 214 Chapter 15 ■ Object-oriented
- Page 241 and 242: 218 Chapter 15 ■ Object-oriented
- Page 243 and 244: 220 Chapter 15 ■ Object-oriented
- Page 245 and 246: 222 Chapter 16 ■ Programming in t
- Page 247 and 248: 224 Chapter 16 ■ Programming in t
- Page 249 and 250: 226 Chapter 16 ■ Programming in t
- Page 251 and 252: 228 Chapter 16 ■ Programming in t
- Page 253 and 254: 230 Chapter 16 ■ Programming in t
- Page 255 and 256: 232 Chapter 16 ■ Programming in t
- Page 257 and 258: 234 Chapter 16 ■ Programming in t
- Page 259 and 260: 236 Chapter 16 ■ Programming in t
- Page 261 and 262: 238 Chapter 17 ■ Software robustn
- Page 263 and 264: 240 Chapter 17 ■ Software robustn
- Page 265 and 266: 242 Chapter 17 ■ Software robustn
- Page 267 and 268: 244 Chapter 17 ■ Software robustn
- Page 269 and 270: 246 Chapter 17 ■ Software robustn
- Page 271 and 272: 248 Chapter 17 ■ Software robustn
- Page 273 and 274: 250 Chapter 17 ■ Software robustn
- Page 275 and 276: 252 Chapter 17 ■ Software robustn
- Page 277 and 278: 254 Chapter 17 ■ Software robustn
- Page 279 and 280: 256 Chapter 17 ■ Software robustn
- Page 281 and 282: 258 Chapter 17 ■ Software robustn
- Page 283 and 284: 260 Chapter 18 ■ Scripting GNU/Li
- Page 285 and 286: 262 Chapter 18 ■ Scripting In sum
15.9 Garbage collection 215<br />
dangerous because it can lead to major errors (or subtle but dangerous errors). The<br />
pointer is often mentioned in the same sentence as the infamous goto statement as a<br />
potential source <strong>for</strong> obtuse and error-prone code. A number of issues should be considered<br />
when evaluating a language’s implementation of pointers.<br />
Since the same data object may be referenced through more than one pointer variable,<br />
care must be taken not to create a “dangling” pointer. That is, a pointer which<br />
references a location that is no longer in use. Does the language provide any assistance<br />
in reducing the opportunities <strong>for</strong> such errors?<br />
The security of pointers is enhanced in such languages as Ada and Java, which<br />
require the programmer to bind a pointer variable to reference only objects of a particular<br />
type. Programs written in such languages as C and C++, which allow pointers to<br />
dynamically reference different types of object, are notoriously awkward to debug.<br />
What provisions (e.g. scoping mechanisms, explicit programmer action or garbage<br />
collection procedures) does the language provide <strong>for</strong> the reclamation of space which is<br />
no longer referenced by any pointer variable? This issue is discussed below.<br />
In Java, the program has no explicit access to memory addresses and it is there<strong>for</strong>e<br />
impossible <strong>for</strong> such a program to make the kind of mistake possible in C++. When a<br />
Java program needs memory, it creates a new object. For example, a program can<br />
instantiate an object of type Button by:<br />
Button aButton = new Button("Press here");<br />
This creates a pointer to the new object aButton. In Java this pointer is termed a<br />
reference, but there is no way in which the Java program can misuse this pointer. For<br />
example, arithmetic is not permitted on a reference, nor can the pointer be used to refer<br />
to an object of another class. (Both these operations are allowed in a C++ program.)<br />
Thus the Java program is prevented from causing a whole class of subtle and dangerous<br />
errors.<br />
15.9 ● Garbage collection<br />
A subtle source of errors can arise when memory is freed (or not) after being allocated<br />
to hold some dynamic data structure. In C++, the programmer explicitly issues a function<br />
call to free memory. The memory manager then adds the retrieved memory to its<br />
pool of available memory; this process is termed garbage collection. When used incorrectly,<br />
two types of errors can arise:<br />
1. memory leaks – memory is no longer in use, but has not been reclaimed by the<br />
memory manager<br />
2. memory corruption (dangling pointer) – memory has been returned from use, but<br />
is still in use.<br />
In a memory leak, a program acquires some memory, uses it, but then fails to<br />
return it <strong>for</strong> garbage collection. This memory is thereby rendered useless. In a program<br />
that only runs <strong>for</strong> a short time, the memory is reclaimed when the program