0

I have a question that Can we use Object pooling concept instead of declaring large size byte array as 20MB. If yes then How? Actually I have a statement as byte[] fileData = new byte[2097152]; because I have to read that much data from a video file which returns OutOfMemory Exception frequently in the app after we are trying to hit the same java file 20 to 24 times continuously. Means this is not the error, in this case heap memory gets full and GC is unable to clear the same in the given time span that's why it returns OutOfMemory Exception. So, Can we use here ObjectPooling concept of JAVA for higher memory utilization.

Thanks in advance.

Sanat Pandey
  • 4,081
  • 17
  • 75
  • 132
  • It's usual to process such files in chunks, yes. If you are not processing the file sequentially, you may have to resort to a pooled loader. You looking for patterns using multiple threads? – Martin James Jan 20 '12 at 10:38

1 Answers1

0

You are trying to solve the wrong problem... you should focus on not loading the entire 2MB file into RAM, because you really don't need to do that.

Create a small window in RAM, 64KB say, and load the file piece by piece. That's what video players etc do.

(That said, 2MB isn't that much. If it's easier, just make sure you only allocate it once and reuse that allocation thereafter.)

Reuben Scratton
  • 38,595
  • 9
  • 77
  • 86
  • Hey Reuben, I am new in Android Development. Can you tell me How to make a window in RAM for 64 KB each and load data piece by piece. – Sanat Pandey Jan 20 '12 at 10:47
  • byte[] buff=new byte[65536]; File file = new file("my_video_file.mp4"); InputStream is = new FileInputStream(file); int bytes_to_read = file.length(); while (bytes_to_read>0) { int num_bytes_read = is.read(buffer, 0, Math.min(buffer.size(), bytes_to_read)); if (num_bytes_read<=0) break; // DO SOMETHING WITH THE VIDEO CONTENT HERE; bytes_to_read -= num_bytes_read; } is.close(); – Reuben Scratton Jan 20 '12 at 10:55
  • Something like that anyway. I did that from memory and didn't include any exception handling. Should be enough to get you started though. – Reuben Scratton Jan 20 '12 at 10:56
  • Thanks for your Help Reuben, but I think there is some problem because my objective is something like different which you have understand. Actually I have to read that much data from video file and after changing on each 64KB data I have to write the whole again in the same video file. So can this mechanism will help me in the same concept? – Sanat Pandey Jan 20 '12 at 11:21
  • Why? What *exactly* are you trying to do? – Reuben Scratton Jan 20 '12 at 11:23
  • Actually me video file is encrypted with SHA-256 and I have decrypt it, for the same I have to xoring the key value to each 64KB data till the end of 2MB and replace the same with the xored result. So please help me regarding the same. – Sanat Pandey Jan 20 '12 at 11:47