Im working on a midlet that fetches data from internet. it works fine on most cases but when heap size is less than 1 mb (or free memory), i get the lousy out of memory exception.
i tracked down the part of my code that makes this exception.
this.connection = (HttpConnection) Connector.open(url,Connector.READ); this.readerStream=this.connection.openInputStream(); this.inputStreamData=new byte[(int)this.connection.getLength()]; this.dataInputStream=new DataInputStream(this.readerStream); this.dataInputStream.readFully(this.inputStreamData); //bad code //return new String(this.inputStreamData); this.songContentContainer.add(new String(this.inputStreamData));//setting the result to the displayi tried using "sustem.gc()" at the beginning of the function and tried splitting the data into small strings and add them to the container on the fly using this code:
this.connection = (HttpConnection) Connector.open(url,Connector.READ);
this.readerStream=this.connection.openInputStream();
this.inputStreamData=new byte[50];
this.dataInputStream=new DataInputStream(this.readerStream);
int i=0;
int ch;
System.gc();
while ((ch= this.dataInputStream.read())!= -1) {
this.inputStreamData[i]=(byte)ch;
if(i==49){
this.songContentContainer.add(new String(this.inputStreamData));
this.inputStreamData=new byte[50];
i=0;
}else
i++;
}
this.songContentContainer.add(new String(this.inputStreamData));
the problem is basically that i have a really big text that i want to show, might be over 1500 character (its a song lyrics)any suggestions on how i can fix or avoid this on phones with low memory?


Sign In
Create Account


Back to top









