Tenho um projeto que carrega imagens TIFF, mas antes disso ele precisa converter o file para bytes[]....eu utilizo o codigo
Filefile=newFile(path+".tif");byte[][]images=newbyte[1][];FileInputStreamfis=newFileInputStream(file);FileChannelfc=fis.getChannel();longlen=fc.size();if(len>Integer.MAX_VALUE)thrownewException("The file is too big");byte[]dataFile=newbyte[(int)len];ByteBufferbb=ByteBuffer.wrap(dataFile);fc.read(bb);images[0]=dataFile;
Acontece que quando o tive ultrapassa +- 50 megas, da erro de memória quando ele esta na linhas fc.read(bb);
ja aumentei a memoria com -Xmx512m -Xms512m.....tem alguma forma de melhorar a perfomance ou tratar esse erro para não travar a aplicação?
Pergunta imbecil número um - o que ocorre se você NÃO usar NIO? O consumo de memória continua alto?
H
hideekii
A unica coisa que eu preciso é do array de bytes da imagem, e não sei outra forma de fazer isso, aceito sugestões
Grato,
T
thingol
Escrevi o programa abaixo, e com ou sem NIO o consumo parece igual. (É o que deveria ser, mas sabe como é que é…)
importjava.io.*;importjava.nio.*;importjava.nio.channels.*;classTesteFIS{publicstaticvoidmain(String[]args)throwsIOException{// Primeiramente vamos criar um arquivo de 100 MBSystem.out.println("Criando um arquivo de teste...");OutputStreamos=newFileOutputStream("teste.bin");intfileLength=100*1024*1024;intblockCount=1024;byte[]buf=newbyte[fileLength/blockCount];for(inti=0;i<blockCount;++i){os.write(buf);}os.close();buf=null;// Agora vamos medir a quantidade de memória. Vou usar "System.gc"// só para ter uma medida confiável.for(inti=0;i<10;++i){System.out.println("Lendo o arquivo sem NIO");System.gc();try{Thread.sleep(1000);}catch(InterruptedExceptionex){}System.gc();try{Thread.sleep(1000);}catch(InterruptedExceptionex){}// Lendo o arquivo em memória só com FileInputStreamInputStreamis=newFileInputStream("teste.bin");longm1=Runtime.getRuntime().freeMemory();byte[]bytes=newbyte[fileLength];is.read(bytes);is.close();System.gc();try{Thread.sleep(1000);}catch(InterruptedExceptionex){}System.gc();try{Thread.sleep(1000);}catch(InterruptedExceptionex){}longm2=Runtime.getRuntime().freeMemory();System.out.println("Diferença de memória: "+((m1-m2)/1024)+" kbytes");bytes=null;// Lendo o arquivo em memória com NIO System.out.println("Lendo o arquivo com NIO");System.gc();try{Thread.sleep(1000);}catch(InterruptedExceptionex){}System.gc();try{Thread.sleep(1000);}catch(InterruptedExceptionex){}m1=Runtime.getRuntime().freeMemory();FileInputStreamfis=newFileInputStream("teste.bin");FileChannelfc=fis.getChannel();byte[]dataFile=newbyte[fileLength];ByteBufferbb=ByteBuffer.wrap(dataFile);fc.read(bb);fis.close();System.gc();try{Thread.sleep(1000);}catch(InterruptedExceptionex){}System.gc();try{Thread.sleep(1000);}catch(InterruptedExceptionex){}m2=Runtime.getRuntime().freeMemory();System.out.println("Diferença de memória: "+((m1-m2)/1024)+" kbytes");}}}
H
hideekii
vlw thingol, vo testa aqui com o seu exemplo e depois eu comento…
engraçado uma coisa…se eu abro a imagem em algum visualizador, a imagem aparece corretamente…agora…no meu visualizador ela aparece espelhada! alguem ja teve esse problema???
T
thingol
O ponto 0,0 (origem das coordenadas) fica no canto superior esquerdo ou no campo inferior esquerdo?
Isso faz diferença, hein!
H
hideekii
mas, onde que isso é passado para acontecer isso???