Writing a byte array image to disk with an automatically generated filename I have the following code that that writes large images to disk. My application server is running out of memory and I'm wondering if I could somehow optimize the following:

public void writeImgToDisk(byte[] base64AttachmentInBytes, String dmxi){

        String destinationAndFileName  = "";
        String fileNameMinusExtension = getCurrentTimeStampForFileNaming();
        String extension="";
        try {


               TikaConfig config = TikaConfig.getDefaultConfig();
               InputStream stream = new ByteArrayInputStream(base64AttachmentInBytes);

               MediaType mediaType = config.getMimeRepository().detect(stream, new Metadata());
               MimeType mimeType;


            try {
                mimeType = config.getMimeRepository().forName(mediaType.toString());
                extension = mimeType.getExtension();
                System.out.println("Extension is:"+ extension);

                File folder  = new File("Z:\\images\\"+ sref);
                if (!folder.exists()){
                    folder.mkdir();
                }
                destinationAndFileName  = "Z:\\images\\"+ dmxi + "\\" + fileNameMinusExtension+ extension;

                System.out.println("destinationAndFileName is:"+ destinationAndFileName);
            } catch (MimeTypeException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            InputStream input = new ByteArrayInputStream(base64AttachmentInBytes);
            OutputStream output = new FileOutputStream(destinationAndFileName);
            IOUtils.copy(input, output);


            System.out.println("It was written..");  
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


This works but its the cause of a bottleneck during high volume calls.
You're not closing the output stream! If that doesn't fix your issue, you'll have to post more contextual code, because after you free the output stream, the rest of this method should just free its resources after it is done.