|
Hi all, I recently wrote some code against a J2ME device that takes image data received via a call-back method and sends it to a remote server via an http OutputStream object. I have never really paid a ton of attention to garbage collection because Java has always done it for me, but I am in a memory constrained runtime (32MB) and memory management is of the highest order. My question relates to the below asyncMethod(...) that receives in 65k chunks of data, copies it into a new byte array, and writes it the the HttpURLConnection's outStream object using write() which is immediately followed by a flush. After the "return false;" statement the newCopy byte array is no longer in scope and therefore ready to be garbage collected (from what I understand of GC). The outStream object is defined globablly and is NOT out of scope after each asyncMethod(...) call. Being that I called the outStream.flush() method I am assuming it would not be holding onto the 65k of image data for each call-back made to asyncmethod(...). The problem: As you can see I have a output statement that calls on Runtime.getRuntime().freeMemory() used to see how much memory is available at each asyncMethod(...) execution. The freeMemory() return value starts out at about 15MB and continues to go down to 2MB and at that point the JRE running on the device seems to think it is time to run GC and the freeMemory() is bumped back up to 15MB. I have attempted to put in System.gc() statements to do my own garbage collection, but that doesn't seem to address the problem (or rather it doesn't affect the value I am getting from freeMemory()). Final question, I promise :-) So, is the outStream holding onto data somehow? Should I be looking at how this company wrote the JRE for their device(assuming they would have to create their own because it isn't running on a standard OS of any kind)? I have looked at a bunch of GC articles hoping to find my answers, but have not yet found one that addresses the specific question I have. Thanks for any input, Aaron Bartell public class ResourceReader { HttpURLConnection conn; OutputStream outStream; public class ResourceListener extends OperationResultAdapter { public boolean asyncMethod(int blockNo, OutParamAcquireResource oar) { Log.out("oar.data.length=" + oar.data.length + " freemem=" + Runtime.getRuntime().freeMemory() + " blockNo=" + blockNo); byte[] newCopy = new byte[oar.data.length]; System.arraycopy(oar.data, 0, newCopy, 0, oar.data.length); outStream.write(newCopy); outStream.flush(); return false; } } } }
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.