Why does Clipboard.getData() use huge amount of heap memory?
Bernd Eckenfels
bernd-2014 at eckenfels.net
Sat Jan 25 11:35:18 PST 2014
Not sure about the clipboard API, but with your reading from a StringReader you essentially double the amount of Space used. (And UTF16 Chars also doubeld the byte count for single byte text). Did you try to get a byte[] Array instead? You can place a ByteBuffer on top of it with no additional copy.
Bernd
> Am 25.01.2014 um 18:03 schrieb Brian Hinz <bphinz at users.sourceforge.net>:
>
> Hi,
>
> Apologies in advance if this is not the right place, but I maintain an open source java VNC viewer (TigerVNC) and I'm pretty much stumped over an OOM exception that gets thrown when I try to access the system clipboard and it contains a large amount of text data. It seems that the heap size jumps more than 10x the actual size of the clipboard data. For example, if I select the whole contents of a 20Mb text file and copy it all to the clipboard (outside the java app) then try to access the clipboard from my app while monitoring the heap size using jconsole, I see the heap size jump by 200-400Mb. I've isolated the source of the exception to the call to Clipboard.getData() (see code below). I've tried using different DataFlavors, etc., but all have the same result. Is this just an inefficiency in the implementation of getData that I'll have to live with? Any suggestions?
>
> TIA,
> -brian
>
> public synchronized void checkClipboard() {
> SecurityManager sm = System.getSecurityManager();
> try {
> if (sm != null) sm.checkSystemClipboardAccess();
> Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
> DataFlavor flavor = DataFlavor.stringFlavor;
> if (cb != null && cb.isDataFlavorAvailable(flavor)) {
> StringReader reader = null;
> try {
> reader = new StringReader((String)cb.getData(flavor));
> reader.read(clipBuf);
> } catch(java.lang.OutOfMemoryError e) {
> vlog.error("Too much data on local clipboard for VncViewer to handle!");
> } finally {
> if (reader != null) reader.close();
> }
> clipBuf.flip();
> String newContents = clipBuf.toString();
> if (!cc.clipboardDialog.compareContentsTo(newContents)) {
> cc.clipboardDialog.setContents(newContents);
> if (cc.viewer.sendClipboard.getValue())
> cc.writeClientCutText(newContents, newContents.length());
> }
> clipBuf.clear();
> // clear out the heap memory used by cb.getData() or else it starts to accumulate
> System.gc();
> }
> } catch(java.lang.Exception e) {
> vlog.debug("Exception getting clipboard data: " + e.getMessage());
> }
> }
> _______________________________________________
> hotspot-gc-use mailing list
> hotspot-gc-use at openjdk.java.net
> http://mail.openjdk.java.net/mailman/listinfo/hotspot-gc-use
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/hotspot-gc-use/attachments/20140125/460d8bc3/attachment.html
More information about the hotspot-gc-use
mailing list