Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach Software Engineering for Students A Programming Approach

web.firat.edu.tr
from web.firat.edu.tr More from this publisher
21.08.2013 Views

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).

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

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!