Dangling TEMP files

I am trying to convert a PDF file to HTML. It works but leaves about 20 files in the TEMP folder that are never deleted.

My code:

Document pdfDocument = new Document("input.pdf");
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.SpecialFolderForAllImages = "images"
pdfDocument.save("resultant.html", saveOptions);

There are two problems:

  1. Calling MemoryCleaner.clearAllTempFiles(); removes EVERY file from the TEMP folder, even belonging to other processes.
  2. If I don’t call MemoryCleaner.clearAllTempFiles(), dangling files stay open. Meaning there is a leak and the library is not closing these files. Eventually, the program will run out of file descriptors.

@petarian

Thank you for contacting support.

Will you please specify if you are referring to temp folder which usually exists over the path %USERPROFILE%\AppData\Local\ or some other temp folder where files are created. Please share source PDF document and generated files with us so that we may try to reproduce and investigate it in our environment. Before sharing requested data, please ensure using Aspose.PDF for Java 18.9.1.

My TEMP folder is a custom location. It is set to /myTemp. The System.property java.io.tmpdir has been modified in the code.

I was using 18.9 . Just downloaded 18.9.1. I am now getting a IncompatibleClassChangeError exception and am trying to figure out why is that happening. Hopefully, once that’s done I give it another try.

As far as the input file goes, it happens with ANY file. Therefore, it does not matter if I send you a file or not. Please keep in mind that I am not able to reproduce this when Unit Testing. Therefore, most likely you won’t be able to do it either.

Could you please ask the developer(s) to see what is it looking for at that location?

@petarian

Please note that we need to reproduce this problem in order to address and investigate it. Would you please try to create a sample application containing SSCCE example so that we may reproduce and sort this out.

I have created a small project and a video demonstrating the problem. The project is a web application requiring Tomcat 7. Both files (project and video) can be [downloaded from here].

I apologize. I am mixing up two forum posting. Please ignore my last message. I will post the code to reproduce the problem shortly.

@petarian

Please take all the time you need and get back to us as per your convenience.

@Farhan.Raza

Here is the code.

File tmpDir = new File("C:\\MyTemp");
System.setProperty("java.io.tmpdir", "C:\\MyTemp");
File tmpFile = new File(imagesFolder, "finalOutput.html");
Document doc = new Document(data);
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.SpecialFolderForAllImages = imagesFolder.getAbsolutePath();
doc.save(tmpFile.getAbsolutePath(), saveOptions);
doc.dispose();
output.append(readTextFile(tmpFile));
// MemoryCleaner.clearAllTempFiles(); //This is very dangerous call.
// It deletes EVERY file from the TMP folder

System.out.println("Num files left in the TEMP folder: " + tmpDir.listFiles().length);

Please create C:\MyTemp on your end before running it

@petarian

Thank you for sharing the code snippet.

We have logged a ticket with ID PDFJAVA-38103 in our issue management system for further investigations. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

We are sorry for the inconvenience.

@petarian

Thank you for being patient.

We have investigated the ticket and would like to update you that, we use java.awt and the following method works in such way: java.awt.Font.createFont(int fontFormat, InputStream fontStream) The temp files will be deleted by JVM with using ShutdownHook when the program exits normally.

https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)

Java tracker blocks 20 the last used temp files and does not allow to delete them during process.
Java does not allow to handle this.

Also, notice please, that method: MemoryCleaner.clearAllTempFiles(); - deletes only files from temp directory that placed in the sub-folder “aspose_pdf” or have name that starts as “aspose_”.

For example the following code does not use Aspose, but blocks 20 from 40 files in temp folder. We cannot delete them even from file manager when it in the line System.out. on debug mode. But when program is finished the temp folder will be empty.

    File tmpDir = new File("C:\\MyTemp");
        System.setProperty("java.io.tmpdir", "C:\\MyTemp");

        for (int i = 0; i<40; i++) {
            InputStream stream = null;
            try {
                File file =new File("C:\\Windows\\Fonts\\arial.ttf");
                stream = new FileInputStream(file);
                java.awt.Font font = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, stream);

                System.out.println("Font Family: "+ font.getFamily() );

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (stream != null) {
                    try {
                        stream.close();
                    } catch (Exception e2) {
                    }
                }
            }
        }


        MemoryCleaner.clearAllTempFiles();

        System.out.println("Num files left in the TEMP folder: " + tmpDir.listFiles().length);

We hope this will be helpful. Please feel free to contact us if you need any further assistance.