Find Your Forum
  
Welcome, Guest. Please login or register.
Did you miss your activation email?

News: Do you like Samsung Instinct?      
Instinct Forum, Samsung Instinct message board, Samsung Instinct By Sprint, Samsung Instinct phone, new Samsung Instinct, Samsung Instinct phone, Sprint Instinct, Samsung Instinct phone
Best Screen Protector for Samsung Instinct                 Free Sprint Phones and Cheap Plans
                Samsung Instinct Scratch Protection
Pages: [1]
Reply Print
Author Topic: Out of Memory problem?  (Read 1418 times)
abales
[I][N][S]
***

Cookies: 1
Hometown: Lees Summit MO
Posts: 22


Email
« on: April 25, 2009, 08:08:44 PM »

I am downloading files from the internet (yea still working on that ebook app) and it keeps crashing.  It runs fine in the emulator. 

Right now I do this....

  OpenFunc()
      cc = (ContentConnection)Connector.open(URLString);
      is = cc.openInputStream();
      filelen = (int) cc.getLength();

The I call this func below in the background thread until the book is reeled in and concatenated into its final String....

  Read_CallMeUntilDone()
           int av = is.available();
           filebuffer = new byte[av];
           int actual = is.read(filebuffer);
           runninglen +=actual;

           if (runninglen==filelen) {
                bookstr += new String(filebuffer);//,"ISO8859_1" /*"utf-8"*/);
                filebuffer = null;
                // YOU ARE DONE
           } else {
               if (actual>0) bookstr += new String(filebuffer);
              // CALL THIS FUNC AGAIN AND AGAIN UNTIL runninglen==filelen
           }

What I am doing is reading the incoming buffer without locking to get the available bytes and then moving those to the big "bookstr" String that holds the entire book.  I put in some .gc's in the futile hope that would.  In the emulator I am getting 8192 byte chunks (no surprise with TCP/IP I am sure) of available data each loop thru.  What available might I be getting in the phone each 100mS loop?

What am I probably seeing on the phone?  I get an unhandled exception and the app dies.  Both funcs have IOException try/catch protection for the above code.
Logged
abales
[I][N][S]
***

Cookies: 1
Hometown: Lees Summit MO
Posts: 22


Email
« Reply #1 on: April 25, 2009, 08:19:25 PM »

Oh..I should add that the books I have trouble with are 300k+ in size.  I was going to move to tinyline's uncompressor but if its dying from lack of memory trying to cram more in there to decompress the data won't fix anything.

I used a blocking readAll and got the data, but it took 5 minutes.  So maybe its not memory?
Logged
mugwumpj
[I][N]
**

Cookies: 11
Hometown: outerspace
Posts: 14


« Reply #2 on: April 29, 2009, 04:35:37 PM »

Instead of using String concatenation, you should consider using a StringBuffer.   It is much more memory efficient.

Remember that Strings are immutable.   Everytime you perform a String concat, you are allocating an entirely new String.   It doesn't actually add to the previous String.   In your above code, if you are reading 320k single byte characters in 8k blocks, then you'll end up allocating about 14MB of Strings.   Almost all of that will be eligible for garbage collection, but almost all of that will be avoided by using a StringBuffer instead.
Logged
abales
[I][N][S]
***

Cookies: 1
Hometown: Lees Summit MO
Posts: 22


Email
« Reply #3 on: April 30, 2009, 05:02:10 AM »

yeah...i will give it a try and report back!
Logged
rappajohn
[I][N][S][T]
****

Cookies: 3
Hometown: San Antonio
Posts: 39


Email
« Reply #4 on: April 30, 2009, 05:29:29 PM »

Also, just reminder... StringBuffer's default starts you off with a relatively small number of characters... and each time it has to grow it only grows by 2*old_length + 2... so this will cost you time if you use the regular StringBuffer() constructor and will be reading in a lot.

if you have the content size (ie. your filelen var) then it may be optimal to do something like
Code:
String Buffer sb = new StringBuffer(filelen);
that is, assuming filelen is the # of characters, i don't know what getLength() returns.
Logged
abales
[I][N][S]
***

Cookies: 1
Hometown: Lees Summit MO
Posts: 22


Email
« Reply #5 on: April 30, 2009, 07:01:23 PM »

I gave up on StringBuffer becuase my read is InputStream which doesn't read into anything really other than bytes.  I never could find a real bytes to StringBuffer without passing thru "New String..." so that was probably the same problem.    I saw that java2 (instead of j2me) has some functions which are not available that would have worked between bytes and StringBuffer.  Note that I am not a java programmer so I could have missed something obvious.

So now I new a bytes array the same size as the file ( up to 500k?) and just start reading what is available.  I make a string at 8k or so to allow the chapter function to began processing the locations and Table of Contents for quick user response.

So now I :
filebytes = new bytes[filesize];
//// read the file as available
if (runningtotal>8192) bookstring = new string(filebytes,0,runningtotal);....find chapters...figure out stuff...
/// read more & done
bookstring = new string(filebytes);// copy complete book (or partial few chapters) to a string
//// display some text now....if the user has selected something while we were busy....
Logged
abales
[I][N][S]
***

Cookies: 1
Hometown: Lees Summit MO
Posts: 22


Email
« Reply #6 on: April 30, 2009, 07:04:37 PM »

oh forgot to say thanks for the ideas...and for probably pointing out another bad move on my part. 

I really need to go buy a good Java ME book and read the whole thing.  BUt then that would be doing things the right way!  Either got any recommendations?
Logged
rappajohn
[I][N][S][T]
****

Cookies: 3
Hometown: San Antonio
Posts: 39


Email
« Reply #7 on: April 30, 2009, 07:33:52 PM »

DataInputStream has a readChar() method (API for DataInputStream), you could try this with the StringBuffer.

there is also a readUTF() that looks like it returns a String from the inputstream... you could experiment with this, i'm not sure how many chars it will return, maybe it could get the whole thing into 1 string for you?

if you cant get it to work, then reading in a few screens at a time wouldn't be too bad, like have a prev, current, and next page  variable so that the user can click back and forth... then once they get to the new page set up the next page in the background again... but this would mean you have to keep your connection open the whole time i think.
Logged
abales
[I][N][S]
***

Cookies: 1
Hometown: Lees Summit MO
Posts: 22


Email
« Reply #8 on: April 30, 2009, 08:42:26 PM »

I tried byte by byte (or really char by char) but its just too slow.  I think, and its only ametuer phone coders opinion,,,I think I did it right to put it in a background thread that polls for available bytes and does reads by bigger blocks for thruput with some 100mS sleeps.  I tried char by char and it took 5-10 minutes to work.  With this method I get it done in 20 seconds and I have the chapters up in a ListFrame in a few seconds.
Logged
rappajohn
[I][N][S][T]
****

Cookies: 3
Hometown: San Antonio
Posts: 39


Email
« Reply #9 on: April 30, 2009, 08:46:52 PM »

Cool deal then, if it works - it works! Grin
You got past the out of memory error, right?
Logged
abales
[I][N][S]
***

Cookies: 1
Hometown: Lees Summit MO
Posts: 22


Email
« Reply #10 on: April 30, 2009, 08:55:54 PM »

Yes I did.  Thanks to you guys!

I am working on test spec to make sure I got all bases covered.  I am making some test files first....spent too much time coding for Uncle Sam to not have a million tests and benchmarks and of course ...a formal looking spec.
Logged
Pages: [1]
Reply Print
Jump to:  

Got a new phone? Find the forum here


Subject Started by Replies Views Last post
Memory Card Problem
How To....
TenPro 1 2023 Last post August 23, 2008, 08:46:21 AM
by Egidio
Never had a problem with the battery life until now, anyone had this problem?
General Instinct Discussions
stylez4o1 5 783 Last post September 13, 2008, 10:18:05 AM
by NicePrice
problem with my memory card
Telus
monaco313 2 441 Last post February 11, 2009, 04:55:18 PM
by monaco313
s30 in phone memory problem
Instinct s30 Discussions
abra007 1 1638 Last post November 15, 2009, 12:39:20 AM
by KCHooligan00
How do i format my memory card and retrieve my old pics from the memory card
How To....
hitchpgh35 8 2799 Last post November 13, 2009, 04:52:37 PM
by lovinlissa1984




Galaxy S3 | Galaxy Note | Galaxy Nexus | Kindle Fire | Atrix 4G | Motorola Xoom | Windows Phone 7
Nokia Lumia | Top Hosts | Samsung Galaxy Tab | Samsung Galaxy S2 | Samsung Galaxy S | Samsung Wave
HTC Evo 3D | HTC Evo 4G | HTC Incredible | HTC Incredible 2 | HTC Incredible S | HTC Thunderbolt
Motorola Droid Razr
| HTC Desire | HTC Desire HD | HTC Desire Z | HTC Desire S | HTC Wildfire
Motorola Droid | Galaxy Indulge | Nokia N8 | Droid Charge | Droid X | Droid X2 | Droid 2| Droid 3 | Fascinate
HTC Sensation | HTC Flyer | LG Revolution | Asus Transformer | Xperia Play | iPhone 4 | Nexus S | Droid Bionic
HTC One | HTC Wildfire S | HTC Droid Eris


This is an Un-Official fan based Website. The views expressed on this website are solely those of the proprietor, or contributors to the site, and do not necessarily reflect the views or opinions of the parties it covers, and is not affiliated with, endorsed or sponsored by parties involved.
If you have a problem with any of the content posted on this website, please contact "sales@verticalscope.com"
Term of Use | Privacy Policy | BlackRain 2006 by, Crip





















CopyRight 2008 www.instinct-samsung.com
Powered by SMF 1.1.11 | SMF © 2006-2007, Simple Machines LLC