Without using the image upload library, I upload images as shown below:
final Image img = new Image(); img.setVisible(false); RootPanel.get().add(img); img.addErrorHandler(new ErrorHandler() { @Override public void onError(ErrorEvent event) { System.out.println(event); } }); img.addLoadHandler(new LoadHandler() { @Override public void onLoad(LoadEvent event) { ImageElement data = ImageElement.as(img.getElement()); RootPanel.get().remove(img); obj.setData(data); System.out.println("Arrived:" + (System.currentTimeMillis()-startTime) + "ms"); render(); } });
Without use . RootPanel.get () add (IMG); the image will never be added to the DOM. Therefore, onLoad will not start. But when you add an image to the DOM, if you do not delete it, it will be a memory leak.
My observations on the above code:
- If I do not add the image to the DOM, the load handler will not start.
- If I do this, but donβt delete it after loading, a memory leak will occur.
- I do, and I delete, Chrome will cause garbage collection after 256 MB. Firefox never called garbage collection. So there was a memory leak. Maybe Firefox will call the garbage collector when it needs it. I do not know this problem.
My questions are: are there any image upload procedures (libraries) to handle image upload?
Do they automatically delete the image from the DOM after loading the image?
Is there a more suitable way to deal with this problem when you draw a lot of images but don't need an image after painting?
Also, are there any quick approaches to adding and removing images to / from the DOM?
source share