我有一个递归方法,遍历包含数千个音乐文件的大型目录.每次扩展符合条件时,它都会将音乐文件添加到observableList <>.在递归方法执行之前,该列表被挂接到另一个线程中的TableView <>,以便用户可以实时查看正在添加到TableView <>的文件.
问题是我对如何在java中管理内存知之甚少,并认为我可能会妨碍垃圾收集.在大约3,000首歌曲之后,递归方法会占用近6 GB的内存,然后开始忽略它应该能够读取的文件.此外,在"完成"逐步通过目录结构之后,ram不会减少,(即,递归方法的堆栈没有被破坏,我认为所引用的所有对象仍然在堆内存中).
它更进一步..我将播放列表导出到XML文件并关闭程序.当我重新启动它时,内存是完全合理的,所以我知道它不是包含文件的大型列表,它必须与递归方法有关.
这是位于音乐处理程序中的recusive方法:
/**
* method used to seek all mp3 files in a specified directory and save them
* to an ObservableArrayList
*
* @param existingSongs
* @param directory
* @return
* @throws FileNotFoundException
* @throws UnsupportedEncodingException
*/
protected ObservableList<FileBean> digSongs(ObservableList<FileBean> existingSongs,
File directory) throws FileNotFoundException,
UnsupportedEncodingException {
/*
* Each directory is broken into a list and passed back into the digSongs().
*/
if (directory.isDirectory() && directory.canRead()) {
File[] files = directory.listFiles();
for …Run Code Online (Sandbox Code Playgroud)