United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6798062 Memory Leak on using getFiles of FileSystemView
JDK-6798062 : Memory Leak on using getFiles of FileSystemView

Details
Type:
Bug
Submit Date:
2009-01-27
Status:
Closed
Updated Date:
2011-01-19
Project Name:
JDK
Resolved Date:
2009-12-28
Component:
client-libs
OS:
windows_vista,windows_xp
Sub-Component:
javax.swing
CPU:
x86
Priority:
P2
Resolution:
Fixed
Affected Versions:
6u10
Fixed Versions:

Related Reports
Backport:
Duplicate:
Relates:
Relates:

Sub Tasks

Description
FULL PRODUCT VERSION :
tested on jre 1.6_10

ADDITIONAL OS VERSION INFORMATION :
Windows Vista 64Bit business..

though problem seems to appear on more os

A DESCRIPTION OF THE PROBLEM :
On using FileSystemView.getFiles()  to retrieve all  children of a directory  the used memory shown by the OS rises. Though Heap and non Heap memory shown by jconsole   stay the same.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
SSCCE given below ... just run it



EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Simple demo should just print ount current  ammount of files on the disc...
And not use much memory.
ACTUAL -
Memory usage of the java process rose by about 200 MiB  per minute.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------

import java.io.File;
import javax.swing.filechooser.FileSystemView;


public class LeakTest {

	private static long counter = 0;
	
	private static FileSystemView view = FileSystemView.getFileSystemView();

	public static void main(String[] args) throws InterruptedException  {
		
		while (true) {
			for (File f : File.listRoots()) {
				if (f.isDirectory()) {
					countFiles(f);
					System.out.println("found: "+counter);
				}
			}
			System.out.println("found Total: "+counter);
			counter = 0;
			Thread.sleep(1000);
		}
		
		

	}
	
	private static void countFiles(File root) {
		
		for (File f :view.getFiles(root, false)) {
			if (f.isDirectory()) {
				countFiles(f);
			} else {
				counter++;
			}
		}
	}

}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Not a very good one:
Using the following method gives nearly the same behaviour... though FileSystemViews list files still shows different kind of files as visible then File.isHidden()
i.e. $Recycle.bin  on Windows

private static File[] getFiles(File parent,boolean useHidden) {
		File[] files = parent.listFiles();
		if (files == null) return new File[0];
			
		if (useHidden) {
			int length = files.length;
			for (int i = 0; i < length; i++) {
				if (files[i].isHidden()) {
					length--;
					System.arraycopy(files, i+1, files, i,  length-i );
				}
			}
			if (length != files.length) {
				File[] onlyVisible = new File[length];
				System.arraycopy(files, 0, onlyVisible, 0, length);
				return onlyVisible;
			}
		}
		return files;
		
	}

                                    

Comments
EVALUATION

This is something we need to look at.
Upgraded to P2, assigned to the JFileChooser team
                                     
2009-02-02
EVALUATION

Windows native code contains memory leaks. It invokes the ShellFolder.GetDisplayNameOf method without release memory.

Documentation about the GetDisplayNameOf method available here:
http://msdn.microsoft.com/en-us/library/bb775071(VS.85).aspx
                                     
2009-03-25



Hardware and Software, Engineered to Work Together