Clean Up Your Mess: Managing Temp Files in Java Apps

Clean Up Your Mess: Managing Temp Files in Java Apps

was always taught to leave a place cleaner than when I got there, and I hold my applications to the same standard. However, a recent Java project I worked on was doing just the opposite, dumping temporary files on the user’s system and never cleaning them up. Fearing the inevitable tech support call after the application filled the user’s hard disk, I had to fix the problem.

The scenario was simple enough: like many applications, mine had to create temporary files on the user’s system during its execution and it then—in theory—would clean them up at exit. The requirement for temporary files (short-lived files normally created in a designated temporary directory) varies from project to project. Applications requiring large batch processing may use temporary files as an intermediate step, while other apps use them to buffer input and output (I/O) from a network or a device. In my case, the temporary files, commonly called temp files, were actually Java Archives (JARs) created at runtime and loaded dynamically into the application (which I later found exasperated the situation). As the project deadline approached, I had to find the problem and, more importantly, develop a solution.



Due to two open Java bugs, a Java application that utilizes a combination of the JVM design and the Win32 operating system does not allow temp files to be deleted if they are open at exit. As a result, the application dumps temporary files on the user’s system and never cleans them up.



The JVM can perform a quick cleanup step at startup to delete any temporary files that are lying around from the application’s previous run. However, because the temp files must be marked as locked when they are created, put all of the temp files for a given instance of the application into a single directory and lock the directory.

The Little Problems That Cause Big Headaches
The Java Foundation Classes (JFC) provide a mechanism for applications to create and destroy temporary files. Using the java.io.File class, an application can create a temporary file and mark it for deletion quite simply, as seen in the following code:

File tempFile =	File.createTempFile("myApp", ".tmp");tempFile.deleteOnExit();

The method deleteOnExit() is designed to allow an application to clean up files when the Java Virtual Machine (JVM) exits. In a perfect world, this would handle all cases. However, I didn’t see this behavior in my application. Although the file was being marked for deletion, it definitely wasn’t being deleted. To complicate matters, I soon discovered that the file was being deleted when the application was run on Linux. It was time to do some research.

After some detective work, I finally had an answer to the problem. Two open Java bugs were to blame. First was bug 4171239: “java.io.File.deleteOnExit does not work on open files (win32).” Unfortunately, a combination of the JVM design and the Win32 operating system does not allow the files to be deleted if they are open at exit. So all I would have to do is ensure I close all the files before exit, right? Wrong. This leads to bug 4950148: “ClassLoader should have an explicit disposal mechanism.” It turns out that an URLClassLoader, which is used to load classes from a JAR file, keeps the stream to the JAR open during the lifetime of the JVM. I was stuck. A Win32 JVM can’t delete an open file, and an URLClassLoader always keeps the file open. I had to think outside the box.

A simple solution would be to have my application always create temporary files with the same prefix, then delete all files with that prefix at some other time. The problem is that solution would not allow multiple instances of the application to run, since there would be a race condition between one instance creating temporaries and another deleting them. I also could unwittingly choose the same prefix as a different application, accidentally deleting its temp files. I needed something more robust and reusable, so I could make sure that all my projects cleaned up like they should.

Design: Building a Better File Trap
What I needed was a temporary file manager: a class that could create temporary files for me, and ensure that they were destroyed at some point in the future. Due to the bugs in the JVM, I was willing to accept that the temporary files may not get cleaned up until the next run of the application. Even with this relaxation in the requirements, I could still guarantee that the user would have only one set of temporary files on their system between runs.

In most cases, the best solution is the simplest one. Since the JVM could already delete files as long as the file was not open, I wanted to leverage this functionality as much as possible. I knew that when a JVM first starts up, it doesn’t have any temp files open. Therefore, it can perform a quick cleanup step to delete any files that may be lying around from the last run. However, when creating a temporary file, the file must be marked as locked, so no other application using the same temporary file management scheme will try to clean up active files. To reduce the number of lock files, I decided to put all of the temp files for a given instance of the application into a single directory and lock the directory.

Now that I had a solution designed, it was time to code it up.

Implementation: Plugging a Hole, Fixing a Leak
To keep things simple, I wanted an API similar to the File class already in the JFC. I created a class, TempFileManager, to handle the creation of temporary files as well as the cleanup of existing files. The first method, createTempFile(String prefix, String suffix), shown in Listing 1, behaves just like the File‘s method to any callers. The internals are a bit different, however. The manager first checks to see if it has been initialized. If it hasn’t, the manager generates a directory name relative to the system temporary directory and then creates a lock file for it. Finally the temp directory is created. Performing the steps in this order ensures that there is no race condition allowing another manager to delete the temp directory before the lock is created. The lock file is marked for deletion at exit, which will work since nothing in the JVM is holding an open reference to it. Finally, the method creates the temporary file that the user requested in the new directory.

The next important method in the TempFileManager isn’t really a method at all, but rather a static initialization block. The JVM runs this block when the class is loaded for the first time. This ensures that the manager will have a chance to clean up any old temp files as soon as the application requests the class, and it does not require the application to tell the manager to perform this cleanup.

Within the static {} block in Listing 1, the manager obtains a list of the files in the temporary directory, using a file filter to get only directories that begin with the magic temp file manager prefix. For each directory found, a check is performed to find the lock file. If no lock file is found, the directory is recursively deleted using the utility method recursiveDelete(File rootDir). With this implementation of the manager, a single instance would clean up temporary files from any number of other applications using the same manager implementation.

Using the TempFileManager in an application is quite simple. To ensure that the temp file manager cleans up leaked files from a previous run, force the JVM to load the class:

Class.forName(TempFileManager.class.getName());

This step is not required since generating a new temporary file will cause the manager to be loaded, but explicitly loading the manager can be useful for documentation purposes. To create a new temporary file, the call is almost identical to the JFC API:

File myTmp = TempFileManager.createTempFile("foo",    ".bar");

Leak Plugged—Headache Avoided
With a solution designed and implemented, I was able to replace all calls to java.io.File#createTempFile(...) with calls to TempFileManager#createTempFile(...) and plug the file leak. No other code changes were necessary, and I didn’t have to wait for Sun to close the open bugs. Although not perfect, this solution does prevent temporary files from growing to the point of being a problem on a user’s system.

If you are running your application from a shell script or batch file, you could build upon this solution by also including a simple main(...) method in your implementation of TempFileManager and call it directly after your application’s JVM exits. This would clean up the temp files instantly and still maintain the lock file safety required.

This is another example of a little bug that could cause big headaches if it is not solved before a project is released. Your users will thank you and you’ll be able to sleep a little better at night knowing you won’t get a call complaining of a full hard disk—at least not one caused by this application.

devx-admin

devx-admin

Share the Post:
5G Innovations

GPU-Accelerated 5G in Japan

NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in

AI Ethics

AI Journalism: Balancing Integrity and Innovation

An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial

Savings Extravaganza

Big Deal Days Extravaganza

The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this

5G Innovations

GPU-Accelerated 5G in Japan

NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will

AI Ethics

AI Journalism: Balancing Integrity and Innovation

An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These

Savings Extravaganza

Big Deal Days Extravaganza

The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this autumn sale has already created

Cisco Splunk Deal

Cisco Splunk Deal Sparks Tech Acquisition Frenzy

Cisco’s recent massive purchase of Splunk, an AI-powered cybersecurity firm, for $28 billion signals a potential boost in tech deals after a year of subdued mergers and acquisitions in the

Iran Drone Expansion

Iran’s Jet-Propelled Drone Reshapes Power Balance

Iran has recently unveiled a jet-propelled variant of its Shahed series drone, marking a significant advancement in the nation’s drone technology. The new drone is poised to reshape the regional

Solar Geoengineering

Did the Overshoot Commission Shoot Down Geoengineering?

The Overshoot Commission has recently released a comprehensive report that discusses the controversial topic of Solar Geoengineering, also known as Solar Radiation Modification (SRM). The Commission’s primary objective is to

Remote Learning

Revolutionizing Remote Learning for Success

School districts are preparing to reveal a substantial technological upgrade designed to significantly improve remote learning experiences for both educators and students amid the ongoing pandemic. This major investment, which

Revolutionary SABERS Transforming

SABERS Batteries Transforming Industries

Scientists John Connell and Yi Lin from NASA’s Solid-state Architecture Batteries for Enhanced Rechargeability and Safety (SABERS) project are working on experimental solid-state battery packs that could dramatically change the

Build a Website

How Much Does It Cost to Build a Website?

Are you wondering how much it costs to build a website? The approximated cost is based on several factors, including which add-ons and platforms you choose. For example, a self-hosted

Battery Investments

Battery Startups Attract Billion-Dollar Investments

In recent times, battery startups have experienced a significant boost in investments, with three businesses obtaining over $1 billion in funding within the last month. French company Verkor amassed $2.1

Copilot Revolution

Microsoft Copilot: A Suit of AI Features

Microsoft’s latest offering, Microsoft Copilot, aims to revolutionize the way we interact with technology. By integrating various AI capabilities, this all-in-one tool provides users with an improved experience that not

AI Girlfriend Craze

AI Girlfriend Craze Threatens Relationships

The surge in virtual AI girlfriends’ popularity is playing a role in the escalating issue of loneliness among young males, and this could have serious repercussions for America’s future. A

AIOps Innovations

Senser is Changing AIOps

Senser, an AIOps platform based in Tel Aviv, has introduced its groundbreaking AI-powered observability solution to support developers and operations teams in promptly pinpointing the root causes of service disruptions

Bebop Charging Stations

Check Out The New Bebob Battery Charging Stations

Bebob has introduced new 4- and 8-channel battery charging stations primarily aimed at rental companies, providing a convenient solution for clients with a large quantity of batteries. These wall-mountable and

Malyasian Networks

Malaysia’s Dual 5G Network Growth

On Wednesday, Malaysia’s Prime Minister Anwar Ibrahim announced the country’s plan to implement a dual 5G network strategy. This move is designed to achieve a more equitable incorporation of both

Advanced Drones Race

Pentagon’s Bold Race for Advanced Drones

The Pentagon has recently unveiled its ambitious strategy to acquire thousands of sophisticated drones within the next two years. This decision comes in response to Russia’s rapid utilization of airborne

Important Updates

You Need to See the New Microsoft Updates

Microsoft has recently announced a series of new features and updates across their applications, including Outlook, Microsoft Teams, and SharePoint. These new developments are centered around improving user experience, streamlining

Price Wars

Inside Hyundai and Kia’s Price Wars

South Korean automakers Hyundai and Kia are cutting the prices on a number of their electric vehicles (EVs) in response to growing price competition within the South Korean market. Many

Solar Frenzy Surprises

Solar Subsidy in Germany Causes Frenzy

In a shocking turn of events, the German national KfW bank was forced to discontinue its home solar power subsidy program for charging electric vehicles (EVs) after just one day,

Electric Spare

Electric Cars Ditch Spare Tires for Efficiency

Ira Newlander from West Los Angeles is thinking about trading in his old Ford Explorer for a contemporary hybrid or electric vehicle. However, he has observed that the majority of

Solar Geoengineering Impacts

Unraveling Solar Geoengineering’s Hidden Impacts

As we continue to face the repercussions of climate change, scientists and experts seek innovative ways to mitigate its impacts. Solar geoengineering (SG), a technique involving the distribution of aerosols

Razer Discount

Unbelievable Razer Blade 17 Discount

On September 24, 2023, it was reported that Razer, a popular brand in the premium gaming laptop industry, is offering an exceptional deal on their Razer Blade 17 model. Typically

Innovation Ignition

New Fintech Innovation Ignites Change

The fintech sector continues to attract substantial interest, as demonstrated by a dedicated fintech stage at a recent event featuring panel discussions and informal conversations with industry professionals. The gathering,

Import Easing

Easing Import Rules for Big Tech

India has chosen to ease its proposed restrictions on imports of laptops, tablets, and other IT hardware, allowing manufacturers like Apple Inc., HP Inc., and Dell Technologies Inc. more time