- Can Java leak memory? Programmers can.
- How Plentiful Is Memory?
- Conclusion
How Plentiful Is Memory?
You might say: "Memory is always plentiful. It's not as precious as it was in the 1990s." This might be true for desktop machines, but I would argue that it's not true because resources are always limited. However, it's definitely not true for resource-constrained mobile computing devices.
Rule #1: Never assume resources are abundantly available—they aren't and never will be.
As the Web evolves from a static page delivery model to a more natural event-based operation, programmers are tasked with squeezing more and more code into smaller devices. These issues are only beginning to be considered for technologies such as Ajax. So, the safest bet is to assume that you'll always be restricted in memory use.
Java Privacy Leaks
What about other types of Java leaks, though?
A much more serious type of leakage occurs when privacy leaks out of code. This is surprisingly easy to do. Listing 2 illustrates a simple staff management system that contains a class called HRAdmin for employees and their associated details.
Listing 2 A Staff Record Management System
public class HRAdmin { public static void main(String[] args) { HRStaffDetails staffDetails = new HRStaffDetails(); // Let’s create a member of staff PermanentStaff staffMember = new PermanentStaff("Bill Gates", new Salary("50000")); staffDetails.addPermanentStaff(staffMember); System.out.println(staffDetails.toString()); } }
Listing 2 illustrates the instantiation of the HRStaffDetails class. New staff is added to this object as people join the company. Also, staff records are removed as people leave the company.
Another use for this program would be to maintain staff records. Clearly, a real system would allow many more details to be stored: home address, previous employer, and so on. You get the idea!
Figure 1 illustrates the program output. As you can see, the company contains just one staff member.
Figure 1 HR admin program output.
Private Data—Salary
While the data in the staff records system is private, the salary item is especially so. For this reason, you don't want client programs to be able to modify salary details in an unauthorized fashion. Imagine if employees were able to view and modify their salaries! Listing 3 illustrates the method from the Salary class that provides salary details.
Listing 4 A Privacy Leak
public Salary getSalary() { return new Salary(salary.getSalary()); } Listing 3 Accessing private data You might be tempted to use the following code in Listing 4 in place of that in Listing 3: public Salary getSalary() { return salary; }
The code in Listing 4 will compile and run just fine. It will produce output identical to that in Figure 1. So, why is the code in Listing 4 so bad? If you think about it, any code that calls the method in Listing 4 will now have direct access to the private salary object. Such code could be part of a client running in another country. Once this client has access to the private data, the code has suffered a privacy leak.
On the other hand, when you use the code in Listing 3, a new object is instantiated and passed to the client. The client can make merry with this object (by changing the salary amount), and this will have no effect on the salary object in the main class.
A Deliberate Error—Separation of Concerns
Notice the last line in Figure 1. It intentionally has an error. Any idea why the word Salary is repeated? It's simple enough, and involves the area commonly called separation of concerns. Let's have a look at this because if you use the principle of separation of concerns, you will generally produce safer code.
Each class you define and use has a set of capabilities. In the case of Listing 2, the class HRStaffDetails instantiated by HRAdmin serves as a container for staff member records. So, I can create new staff records and add them to the administration system. Likewise, I can modify these records and also remove them if required. Given this, the major concerns of the class HRStaffDetails are:
- Staff record creation
- Staff record storage
- Staff record modification
- Staff record retrieval and listing
- Staff record deletion
The fourth item in this list is the location of the error (the duplicate word Salary at the bottom of Figure 1). Each class that is subordinate to class HRStaffDetails provides a standard Java method called toString(). The main class (HRStaffDetails) also implements this method. Listing 5 illustrates the toString() method for HRStaffDetails.
Listing 5 The toString() Method
public String toString() { String newline = System.getProperty("line.separator"); StringBuffer buf = new StringBuffer(); buf.append("Permanent Staff: ").append(newline); for(int i = 0; i < permanentFolks.size(); i++) { buf.append(permanentFolks.elementAt(i)).append(newline); } return buf.toString(); }
The Listing 5 method just calls into the toString() method of the contained objects, which are instances of the vector permanentFolks. So, there’s no problem here because there’s no mention of Salary in Listing 5. Let's now look at Listing 6 to see the toString() method for the elements contained within the permanentFolks vector.
Listing 6 The PermanentStaff toString() Method
public String toString() { return "PermanentStaff: Name = " + personName + System.getProperty("line.separator") + "Salary = " + salary + ". " + System.getProperty("line.separator"); }
In Listing 6, notice the mention of the word Salary. As you can see, an instance of a Salary object is used in this line. Now look at Listing 7: the toString() method for the Salary class.
Listing 7 The Final Destination in the toString() Chain
public String toString() { return "Salary = " + getSalary(); }
And there you see the second mention of the word Salary. So, there's no need for the code in Listing 6 to include the word Salary. The Salary class handles this task. In other words, there is a separation of concerns—all salary-related tasks should be left to the Salary class.
Rule #2, described next, might help you to remember how to employ the separation of concerns.
Rule #2: In order to respect separation of concerns, make sure your classes mind their own business!
If you think you need some capability in a given member function, check that some other class isn't already doing it.
Safer Java Data Structures
Remember my tale from the 1990s about exceeding the boundary of an array? Here's a scheme that avoids this problem altogether and that can be extended for any programming language.
Imagine you have another simple HR staff list application that stores employee details. I've placed the names of the staff members in an enum in Listing 8. Given the staff members listed, it's an interesting company, maybe some kind of a consultancy!
Listing 8 Employee Management System
import java.util.Scanner; public class EnumValuesDemo { enum EmployeeList {PLACE_HOLDER, BILL_GATES, GEORGE_BUSH, WARREN_BUFFET, TONY_BLAIR, CARLY_FIORINA, NEXT_EMPLOYEE}; public static void main(String[] args) { // Create a human resources staff list object using the enum EmployeeList[] staffList = EmployeeList.values(); for (int i = EmployeeList.PLACE_HOLDER.ordinal() + 1; i < EmployeeList.NEXT_EMPLOYEE.ordinal(); i++) { System.out.println("Staff name " + staffList[i]); } } }
After the enum, my main() method instantiates an array of EmployeeList objects; this is a really handy feature of the enum type. I now have an array that includes a string for each member of the enum. If you think about this, the code has defined an enum, but pretty much for free I get an array of the enum elements. And these two entities are inextricably interwoven. As you'll see, this provides scope for safer code.
Remember that I want to avoid overstepping the boundary of an array of staff member names. While this can be done fairly easily in Java using the length method, I want to try to impose some of the data semantics on the code. This is the purpose of the two enum elements, PLACE_HOLDER and NEXT_EMPLOYEE.
The PLACE_HOLDER element marks the beginning of the enum type. This element is never used to define an employee. The first employee is defined in the next element following PLACE_HOLDER. The NEXT_EMPLOYEE.element is used to mark the position where you add a new employee element in the enum.
Figure 2 illustrates the elements of the staff list array displayed using the following line:
EmployeeList.PLACE_HOLDER.ordinal()
Figure 2 Staff listing.
The for loop in Listing 8 uses as limits the start and end elements of the enum (PLACE_HOLDER and NEXT_EMPLOYEE). This means that the array can have as many elements as are defined in the enum—tens, hundred, thousands, and more.
Let's now assume that it's a fairly frequent occurrence for new people to join and existing staff members to leave our little company. Imagine if the enum member TONY_BLAIR left the company. The employee list in Listing 8 would have to be updated to match that shown in Listing 9.
Listing 9 Modified Employee List
enum EmployeeList {PLACE_HOLDER, BILL_GATES, GEORGE_BUSH, WARREN_BUFFET, CARLY_FIORINA, NEXT_EMPLOYEE};
Then, when I rerun the for loop, Figure 3 illustrates the new program output.
Figure 3 Updated staff listing.
So, only a data change was required. No real programming effort. It wasn't necessary to dig into the code to modify the for loop. Indeed, if the names were read in from a database instead of being hardcoded, there would be no programming effort at all.
Because the data semantics drive the code, there is a much-reduced chance of exceeding the boundary of the array. This means that if you code carefully, you don't need to worry about an exception occurring. This in turn reduces the size of your code and to some extent improves its performance.
Running the Supplied Java Code
To run the code, download it from here, and unzip it into any old folder. Next, open a DOS console and change the directory to where the unzipped code files reside. Run the command javac *.java in each of these folders: Safer data, Memory Leak, and Other Leaks. To run the code, just type java followed by the name of the class file that contains the main() method.