how to free memory of byte array

 
0
 

how to free memory of byte array...


code

for(int j=0;j<count;j++){

buffer=null; //free the memory by set as a null but not working????? shows array index out of bound exception for the second time
buffer= new byte[size]; //byte mem alloc

temp2=di.read(buffer,total,size);// read buffer

total=total+temp2;
fo=null;
ds=null;
fo= new FileOutputStream(flist[j]);
ds= new DataOutputStream(fo);
ds.write(buffer,0,size);
System.out.println("write: "+(j+1));
fo.close();
ds.close();

}

exception occur at run time...

Exception in thread "main" java.lang.IndexOutOfBoundsException
        at java.io.FileInputStream.readBytes(Native Method)
        at java.io.FileInputStream.read(Unknown Source)
        at java.io.DataInputStream.read(Unknown Source)
        at filesplitandjoin.main(filesplitandjoin.java:57)

src code attached:(

Attachments filesplitandjoin.java (1.1 KB)
import java.io.*;


public class filesplitandjoin{

public static void main(String args[])throws Exception{

System.out.println("hi split and join..");

File f=new File("Test$33.java");
FileInputStream fi= new FileInputStream(f);
DataInputStream di= new DataInputStream(fi);


int flen=(int)f.length();

System.out.println(flen);


int size=1000;

int temp=flen;
int count=0;
while(temp>0){
count++;
temp=temp-size;


}

String temp1="";
System.out.println(count);


//byte[] buffer= new byte[(int)f.length()];

byte[] buffer;


String[] flist= new String[count];
for(int i=0;i<count;i++){

flist[i]="split"+temp1.valueOf(i+1)+".part";
System.out.println(flist[i]);
}


int total=0;
int temp2;

FileOutputStream fo;
DataOutputStream ds;

for(int j=0;j<count;j++){

buffer=null;
buffer= new byte[size];

temp2=di.read(buffer,total,size);

total=total+temp2;
fo=null;
ds=null;
fo= new FileOutputStream(flist[j]);
ds= new DataOutputStream(fo);
ds.write(buffer,0,size);
System.out.println("write: "+(j+1));
fo.close();
ds.close();

}




}






}

Featured Replies in this Discussion

  • ... Uhm, Yes, you can. You cannot assign null to an element of the array but you sure can assign null to the array reference (which is what the OP did). @OP your problem is that the second argument in your "read" call is the offset into the byte array (that is the first argument) which tells read where to start adding the bytes it reads, and "total" is probably already as large as size…
    Post Contains: Quotes
 
0
 

You cannot set a null value to a primitive array. You could initialize an array size 0 instead...

buffer = new byte[0];

This mean any previous declaration/used will be marked as garbage. That means the GC (Garbage Collector) will free up the memory whenever it runs.

 
1
 

You cannot set a null value to a primitive array. You could initialize an array size 0 instead...

Uhm, Yes, you can. You cannot assign null to an element of the array but you sure can assign null to the array reference (which is what the OP did).

@OP your problem is that the second argument in your "read" call is the offset into the byte array (that is the first argument) which tells read where to start adding the bytes it reads, and "total" is probably already as large as size making that write beyond the end of the array.

Question Answered as of 3 Years Ago by masijade and Taywin
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: