in my program i want to read an PLSQL file
and delete the comments that starts with --
i'm putting every comment in it's own line so i could delete that specific line (sometimes i have the code and the comments in the same line that's way i'm doing "\n--").
i export my program to a jar file and it works fine in my desktop but in another computer (reading different PLSQL files ) it gives me Java heap space error even when i try
java -Xmx256m -jar myjar.jar
error :
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source)
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuffer.append(Unknown Source)
at ParserDB.ScriptNoComment(ParserDB.java:142)
at ParserDB.GetTheName(ParserDB.java:54)
at Rapport.SearchCcInDB(Rapport.java:189)
at Rapport.listDB(Rapport.java:77)
at Rapport.main(Rapport.java:472)
... 5 more
my code is :
public static String ScriptNoComment(String fileName){
String result = null ;
try{
FileInputStream fstream = new FileInputStream(fileName);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuffer strOut = new StringBuffer();
StringBuilder Out = new StringBuilder();
String strLine;
while ((strLine = br.readLine()) != null) {
if(strLine.contains("--")){
strLine = strLine.replaceAll("--","\n--");
}
strOut.append(strLine+"\n");
}
in.close();
//delete comment
String[] lines = strOut.toString().split("\\n");
for(String s: lines){
if(s.contains("--")){
s="";
}
Out.append(s+"\n");
}
result = Out.toString();
result = result.toUpperCase();
result = result.replaceAll("\"", "");
result = result.replaceAll("\\r\\n|\\r|\\n", " ");
result = result.replaceAll("\\s+", " ");
}catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
return result ;
}
is there anyway to optimize my code , thanks in advance
EDIT
1-)
i checked the heap size in the other computer with the command :
java -XX:+PrintFlagsFinal -version | findstr /i "HeapSize PermSize ThreadStackSize"
the result was : min : 16M and Maxsize : 256M so i should tape in the java -jar :-Xmx512m instead of -Xms256m
2-) i removed (just for test) the stringbuilder and all the replaceAll and still got the same error because my file was too big .
so what i did is to count the lines for each file i'm reading and try (depending on the lines ) to read only the 50 first lines for example and apply my methods to only this 50 lines
thank you all for your answers