In my project, I have a module to upload multiple image and create the thumbnails for it at once. For uploading, I am using JavaFX and for creating thumbnails, I am using Java.
I wrote upload code and call of thumbnail creation function inside a for loop. If the number of uploading images is more than five, I am getting this error:
Java heap space (java.lang.OutOfMemoryError)
I think, the code for uploading is fine, and the problem with thumbnail creation code. How can I solve this problem? How should I change the structure of my code ?
This is my JavaFX code:
fgUrl = fc.getSelectedFiles(); for(fg in fgUrl) { try { System.gc(); fileURL = "file:///{fg.toString()}"; fileURL = fileURL.replace("\", "/"); def dt = DateTime{}.instant; var datetime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS").format(dt); pic_url = datetime.replace("-", "_").replace(":", "_").replace(" ", "_"); datetime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(dt); f = new File("C:\uploaded\{pic_url}.jpg"); uploadFile(fileURL, f,save_index,datetime,pic_url); // This function will save selected image in the working directory of the system. var resize_ob = new resizeImage(url.replace("file:///", ""),"C:/thumbnails/{pic_url2}.jpg");// This will call the java thumbnail creation function. save_index++; } catch(e:Exception) { } }
Advertisement
Answer
You can try to use memory profilers to see which portion of code/class/method is consuming more memory. You can start with free JVisualVM or JConsole which comes with JDK.
Other well known profilers are:
-> Optimize IT
-> JProfiler