Why does Clipboard.getData() use huge amount of heap memory?
Brian Hinz
bphinz at users.sourceforge.net
Sat Jan 25 09:03:25 PST 2014
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());
}
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/hotspot-gc-use/attachments/20140125/a10371e5/attachment-0001.html
More information about the hotspot-gc-use
mailing list