stein Posted June 17, 2009 at 07:30 PM Report Share #273168 Posted June 17, 2009 at 07:30 PM Cumprimentos, eu tenho este metodo que me provoca o "java.lang.OutOfMemoryError: Java heap space" e ja experimentei aumentar a memoria disponível no projecto mas este acaba sempre por dar o mesmo erro, alguém tem alguma ideia de como optimizar o código de forma não "rebentar" a pilha? public String readProperty() throws IOException { StringBuffer buffer = new StringBuffer(); String line; try { while (true) { line = readLine().trim(); if ((line.length() != 0) && (line.charAt(0) != '#')) { if (line.endsWith("\\")) { line = line.substring(0, line.length() - 1); buffer.append(line); } else { buffer.append(line); break; } } } } catch (NullPointerException e) { return null; } return buffer.toString(); } } thanks:) Link to comment Share on other sites More sharing options...
bruno1234 Posted June 17, 2009 at 08:42 PM Report Share #273179 Posted June 17, 2009 at 08:42 PM Isso chega a parar? Não devias ter uma condição q verifica se encontrou EOF? Matraquilhos para Android. Gratuito na Play Store. https://play.google.com/store/apps/details?id=pt.bca.matraquilhos Link to comment Share on other sites More sharing options...
stein Posted June 18, 2009 at 03:06 PM Author Report Share #273380 Posted June 18, 2009 at 03:06 PM Nos ficheiros mais pequenos não é necessario, nos maiores ele nao consegue chegar ao fim por falta de memoria 😛 Link to comment Share on other sites More sharing options...
edsousa Posted June 18, 2009 at 03:55 PM Report Share #273390 Posted June 18, 2009 at 03:55 PM Troca pela minha versão... embora dum ponto de vista lógico o teu ciclo tem as condições certas ? troquei o StringBuffer por Builder, e não faço o line=line.substring... já agora, põe um -Xmx grande mesmo, tipo -Xmx1024m correr o código debaixo do profiler tb dava jeito. A SDK do Java 6 tem um chamado jvisualvm public String readProperty() throws IOException { StringBuilder buffer = new StringBuilder(); //Builder pq não preciso de sincronização, i.e., pq é só uma thread a lhe mexer String line; boolean endsWithBackslash; boolean isComment; boolean emptyLine; line = readLine().trim(); emptyLine = (line.length()==0); if(!emptyLine) { endsWithBackslash = line.endsWith("\\"); isComment = line.charAt(0)=='#'; } while(emptyLine || isComment || endsWithBackslash) { if(endsWithBackslash && !isComment && !emptyLine) buffer.append(line.substring(0,line.length()-1)); line = readLine().trim(); emptyLine = (line.length()==0); if(!emptyLine) { endsWithBackslash = line.endsWith("\\"); isComment = line.charAt(0)=='#'; } } buffer.append(line); return buffer.toString(); } Tharis Fan ClubMay Tharis bless you Link to comment Share on other sites More sharing options...
M6 Posted June 22, 2009 at 10:14 AM Report Share #274074 Posted June 22, 2009 at 10:14 AM -Xmx1024m é capaz de ser um exagero... Experimenta com valores mais baixos, começa em 128 e avança de 128 em 128... Edit: não pude deixar de reparar que parece que estás a ler um ficheiro de settings, o Java já tem um objecto para ler e gravar de forma simples... Já agora, um ficheiro de settings que rebenta com a memória, cheira-me a esturro... 10 REM Generation 48K! 20 INPUT "URL:", A$ 30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50 40 PRINT "404 Not Found" 50 PRINT "./M6 @ Portugal a Programar." Link to comment Share on other sites More sharing options...
stein Posted June 27, 2009 at 06:31 PM Author Report Share #275262 Posted June 27, 2009 at 06:31 PM Boas, Eu estou a tentar ler as propriedades dos ficheiros, mas ja percebi que rebenta em ficheiros grandes como imagens(iso), mas no projecto que estou a desenvolver não preciso de ler esse tipo de ficheiros:D Mas se conhecerem alguma forma de não rebentar a aplicação enquanto estou a aberto a sugestoes:) obrigado!!! Link to comment Share on other sites More sharing options...
Triton Posted June 27, 2009 at 06:34 PM Report Share #275264 Posted June 27, 2009 at 06:34 PM http://java.sun.com/javase/6/docs/api/java/nio/channels/FileChannel.html <3 life Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now