Java 堆空间(java.lang.OutOfMemoryError)
在我的项目中,我有一个模块可以上传多个图像并立即为其创建缩略图。对于上传,我使用 JavaFX,对于创建缩略图,我使用 Java。
我在 for 循环中编写了上传代码和缩略图创建函数的调用。如果上传图像的数量超过五个,我会收到此错误:
Java heap space (java.lang.OutOfMemoryError)
我认为上传的代码很好,但缩略图创建代码有问题。我该如何解决这个问题?我应该如何更改代码结构?
这是我的 JavaFX 代码:
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) { }
}
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) { }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用内存分析器来查看代码/类/方法的哪一部分消耗了更多内存。您可以从免费的 JVisualVM 或 JConsole 是 JDK 自带的。
其他众所周知的分析器是:
-> 优化 IT
-> JProfiler
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
只要您引用了这些图像(在变量中或列表中或其他内容中),Java 的自动垃圾收集器就不会清理它。您应该仅在需要时加载它们,然后在完成每个图像后立即将变量设置为 null。
图像非常大,Java 可能会将它们解压为位图(如 bmp 文件,巨大),所以这并不奇怪。
垃圾收集不会持续运行(它会经常清理),因此如果您想要求它立即运行(您不能强制它),您可以调用 System.gc();。但垃圾收集速度很慢,因此可能会减慢您的处理速度。
如果需要,您可以增加最大内存,但最好的做法是执行上述操作。
有了更多信息,我可以提供更详细的答案。
As long as you have a reference to these images(in a variable or inside List or something) the Java's automatic Garbage Collector wont clean it up. You should only load them when you need them, then set the variables to null as soon as you're finished with each image.
Images are pretty big, and Java probably unpacks them to bitmaps(like bmp files, huge) so this is not a surprise.
Garbage collection doesn't run constantly (it cleans up every so often), so if you want to ask it to run now (you can't force it) you can call System.gc();. Garbage collection is slow though, so it may slow your processing.
If needed you can increase your maximum memory, but its better practice to do the above.
With more information i can supply a more detailed answer.