java outputstream java.lang.OutOfMemoryError: Java heap space


HOME ยป Java

I am trying to publish a large video/image file from the local file
system to an http path, but I run into an out of memory error after
some time...


here is the code


public
boolean publishFile(URI publishTo, String localPath) throws Exception
{
InputStream istream = null;
OutputStream ostream =
null;
boolean isPublishSuccess = false;
URL url =
makeURL(publishTo.getHost(), this.port, publishTo.getPath());

HttpURLConnection conn = (HttpURLConnection) url.openConnection(); /> if (conn != null) {
try {

conn.setDoOutput(true);

I get a java.lang.OutOfMemoryError: Java heap space message when
reading from an 11g Oracle database via JDBC. I have the
defaultRowPrefetch set to 10000, reducing it to 2000 seems to resolve
the issue. However, adding additional memory to the JVM (-xmx) does
NOT resolve the issue. This issue only seems to occur in 64-bit java
1.6 - I was not seeing this issue in 32-bit java.


My
question, what setting can I change to allow my application to run
with the larger defaultRowPrefetch?

Java

I am trying to publish a large video/image file from the local file
system to an http path, but I run into an out of memory error after
some time...


here is the code


public
boolean publishFile(URI publishTo, String localPath) throws Exception
{
InputStream istream = null;
OutputStream ostream =
null;
boolean isPublishSuccess = false;
URL url =
makeURL(publishTo.getHost(), this.port, publishTo.getPath());

HttpURLConnection conn = (HttpURLConnection) url.openConnection(); /> if (conn != null) {
try {

conn.setDoOutput(true);
Java

when we deploy the application on tomcat server I have been facing
java.lang.OutOfMemoryError issue because we specify large values for
"maxOccurs" attribute(i.e. maxOccurs="999999")in the schema
definition. Please find below sample snippet and the exception stack
trace. Could someone please suggest optimized solution to avoid this
scenario.


Initially we kept maxoccurs value to "unbound".
But when we scan code base with HP Fortify Static Code Analyzer we
found there is Critical issue(for more info on the issue please
visit:Fortify Link, on the left panel we have XML-->Environment-->
Weak XML Schema: Unbounded Occurrences). So HP Foritfy tool recommends
limit maxoccurs

Java

java.lang.OutOfMemoryError: Java heap space
at
SevenZip.Compression.LZ.OutWindow.Create(OutWindow.java:18)
at
SevenZip.Compression.LZMA.Decoder.SetDictionarySize(Decoder.java:162)

at
SevenZip.Compression.LZMA.Decoder.SetDecoderProperties(Decoder.java:327)

at shared.utils.Utils.lzmaUncompress(Utils.java:118)
at
common.Preloader.download(Preloader.java:164)
at
common.Preloader.redownload(Preloader.java:131)
at
common.Preloader.init(Preloader.java:58)
at
sun.applet.AppletPanel.run(AppletPanel.java:380)
at
java.lang.Thread.run(Thread.java:595)
Java

if i put more than 5 excel files in a folder i getting this
java.lang.OutOfMemoryError: Java heap space. Totally i have 40 excel
files and each file may contain 5 to 10 sheets. my code will find out
the errors in the excel files and write it down in the Error.txt
file.



Here is the code



public class Test {



public static int getExcelColumnNumber(String column) {
int result = 0;
for (int i = 0; i < column.length(); i++) {
result *= 26;
result += column.charAt(i) - 'A' + 1;
}
return result;
}

public static String getExcelColumnName(int number) {
final StringBuilder sb = new StringBuilder();

int num = number - 1;
while (num >= 0) {
int numChar = (num % 26) + 65;
sb.append((char)numChar);
num = (num / 26) - 1;
}
return sb.reverse().toString();
}


public static void main(String[] args) {
try {

File directory = new File
("C://Users//kondeti.venkatarao//Documents//Regresion_sheets");
File[] files = directory.listFiles();

File errorBW = new File
("C://Users//kondeti.venkatarao//Documents//Regresion_sheets//ErrorBW.txt");
BufferedWriter bwr = new BufferedWriter(new
FileWriter(errorBW));

StringBuffer error = new StringBuffer();

error.append("------------------------ERRORS-----------------------------------");
error.append(System.getProperty("line.separator"));
StringBuffer misMatch = new StringBuffer();
misMatch.append(System.getProperty("line.separator"));
misMatch.append("------------------------MISMATCHED
VALUES------------------------");
misMatch.append(System.getProperty("line.separator"));
for (File file : files) {
if (file.getName().endsWith(".xlsx")) {
FileInputStream fis = new FileInputStream(
file.getAbsoluteFile());
// Create Workbook instance holding reference to .xlsx
file

XSSFWorkbook workbook = new XSSFWorkbook(fis);
int i = 1;
while (i < workbook.getNumberOfSheets()) {

//
System.out.println(workbook.getNumberOfSheets());
// Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(i);

// Iterate through each rows one by one
Iterator<Row> rowIterator =
sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// For each row, iterate through all the
columns
Iterator<Cell> cellIterator =
row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
// Check the cell type and format
accordingly
switch (cell.getCellType()) {
/*
* case Cell.CELL_TYPE_NUMERIC:
*
System.out.print(cell.getNumericCellValue());
* break; case Cell.CELL_TYPE_STRING:
*
System.out.print(cell.getStringCellValue());
* break;
*/
// case Cell.CELL_TYPE_FORMULA:
case Cell.CELL_TYPE_FORMULA:

if
(cell.getCellFormula().startsWith("IF("))
if
(sheet.getRow(row.getRowNum())

.getCell(cell.getColumnIndex())

.getRawValue().equals("1")) {

HashSet<Integer> number=
new HashSet<Integer>();
ArrayList<String>
alphas=new ArrayList<String>();

String
formula=sheet.getRow(row.getRowNum()).getCell(cell.getColumnIndex
()).toString();
Matcher
digitMatcher = Pattern.compile("\d+").matcher(formula);
Matcher
alphabetMatcher = Pattern.compile("[a-zA-Z]+").matcher(formula);


while(alphabetMatcher.find()) {

if(!alphabetMatcher.group().equals("TYPE"))

alphas.add(alphabetMatcher.group());
}

int countIF =
Collections.frequency(alphas, "IF");
int countABS =
Collections.frequency(alphas, "ABS");
HashSet<String> alphaSet=new
HashSet<String>(alphas);
if(countIF!=5 &&
countIF!=6)
alphaSet.remove("IF");
if(countABS != 3 &&
countABS!=4)
alphaSet.remove("ABS");

while(digitMatcher.find()) {

if(!digitMatcher.group().equals("0") &&
!digitMatcher.group().equals("1") &&
!digitMatcher.group().equals("01"))

number.add(Integer.parseInt(digitMatcher.group()));
}


ArrayList<Integer>
numberList = new ArrayList<Integer>(number);
ArrayList<String> alphaList
= new ArrayList<String>(alphaSet);

System.out.println("alphaSet"+alphaSet);

System.out.println("numberList"+numberList);

int rowIndex=numberList.get(0)-1;
int originalColumnIndex =
getExcelColumnNumber(alphaList.get(0))-1;
int referenceColumnIndex =
getExcelColumnNumber(alphaList.get(1))-1;
if(originalColumnIndex >
referenceColumnIndex){
int temp =
referenceColumnIndex;
referenceColumnIndex
=originalColumnIndex;
originalColumnIndex=temp;
}



//System.out.println(sheet.getRow(row.getRowNum()));
System.out.println("File Name: "+
file.getName());
System.out.println("Sheet Name: "+
sheet.getSheetName());

System.out.println(sheet.getRow(row.getRowNum()).getCell(cell.getColumnIndex()).toString());

if(sheet.getRow(rowIndex).getCell(originalColumnIndex).getCellFormula().equals(""))
System.out.println("please
help me out");
System.out.println("Cell Number:
"+getExcelColumnName(originalColumnIndex+1)+numberList.get(0));
System.out.println("Function Name:
"+
sheet.getRow(rowIndex).getCell(originalColumnIndex).getCellFormula());
System.out.println("row
indext"+rowIndex);
System.out.println("original
column index"+originalColumnIndex);
System.out.println("ref column
index"+referenceColumnIndex);
/*System.out.println("File Name: "
+ file.getName());
System.out.println("Sheet Name: "
+ sheet.getSheetName());
System.out.println(cell
.getCellFormula());*/

if(sheet.getRow(rowIndex).getCell(originalColumnIndex).getCellFormula().contains("qCRA_")
&&
sheet.getRow(rowIndex).getCell(originalColumnIndex).getRawValue().contains("Error:")){

error.append(System.getProperty("line.separator"));
error.append("File Name: "+
file.getName());

error.append(System.getProperty("line.separator"));
error.append("Sheet Name: "+
sheet.getSheetName());

error.append(System.getProperty("line.separator"));
error.append("Function Name:
"+
sheet.getRow(rowIndex).getCell(originalColumnIndex).getCellFormula());

error.append(System.getProperty("line.separator"));
error.append("Cell Number:
"+getExcelColumnName(originalColumnIndex+1)+numberList.get(0));

error.append(System.getProperty("line.separator"));
error.append("Orginal Value :
"+sheet.getRow(rowIndex).getCell(originalColumnIndex).getRawValue());

error.append(System.getProperty("line.separator"));
error.append("Reference Value
: "+sheet.getRow(rowIndex).getCell(referenceColumnIndex));

error.append(System.getProperty("line.separator"));
} else {

misMatch.append(System.getProperty("line.separator"));

misMatch.append("File Name: "+ file.getName());

misMatch.append(System.getProperty("line.separator"));

misMatch.append("Sheet Name: "+ sheet.getSheetName());

misMatch.append(System.getProperty("line.separator"));

misMatch.append("Function Name: "+ sheet.getRow(rowIndex).getCell
(originalColumnIndex).getCellFormula());

misMatch.append(System.getProperty("line.separator"));
misMatch.append("Cell
Number: "+getExcelColumnName
(originalColumnIndex+1)+numberList.get(0));

misMatch.append(System.getProperty("line.separator"));

misMatch.append("Orginal Value : "+sheet.getRow(rowIndex).getCell
(originalColumnIndex).getRawValue());

misMatch.append(System.getProperty("line.separator"));
misMatch.append("Reference
Value : "+sheet.getRow(rowIndex).getCell (referenceColumnIndex));

misMatch.append(System.getProperty("line.separator"));
}
}
break;

}
}
}
System.out.println("");
i++;
fis.close();
}
}
}
error.append(misMatch.toString());
bwr.write(error.toString());
bwr.flush();
bwr.close();
// }
} catch (Exception e) {
e.printStackTrace();
}
}
}
Web Development

I'm getting OutOfMemoryError: Java heap space for the following
code:



StringBuilder obj= new StringBuilder("");
InputStream in = new FileInputStream(new File(file));
br = new BufferedReader(new InputStreamReader(in), 102400);
for (String line; (line= br.readLine()) != null;) {
for (i = 0; i < 3; i++) {
for (j = 1; j < 5; j++){
obj.append(line.charAt(j));
}
}
hashMap.put(obj.toString(), someValue);
obj.setLength(0);
}


but,



StringBuilder obj= new StringBuilder("");
InputStream in = new FileInputStream(new File(file));
br = new BufferedReader(new InputStreamReader(in), 102400);
for (String line; (line= br.readLine()) != null;) {
obj.setLength(0);
for (i = 0; i < 3; i++) {
for (j = 1; j < 5; j++){
obj.append(line.charAt(j));
}
}
hashMap.put(obj.toString(), someValue);
}


resolved the error? What is the issue in placing obj.setLength(0)
at the beginning or at the end?



The Exception:



Exception in thread "main" java.lang.OutOfMemoryError: Java
heap space
at java.util.Arrays.copyOfRange(Unknown Source)
at java.lang.String.<init>(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
Web Development

- Technology - Languages
+ Webmasters
+ Development
+ Development Tools
+ Internet
+ Mobile Programming
+ Linux
+ Unix
+ Apple
+ Ubuntu
+ Mobile & Tablets
+ Databases
+ Android
+ Network & Servers
+ Operating Systems
+ Coding
+ Design Software
+ Web Development
+ Game Development
+ Access
+ Excel
+ Web Design
+ Web Hosting
+ Web Site Reviews
+ Domain Name
+ Information Security
+ Software
+ Computers
+ Electronics
+ Hardware
+ Windows
+ PHP
+ ASP/ASP.Net
+ C/C++/C#
+ VB/VB.Net
+ JAVA
+ Javascript
+ Programming
Privacy Policy - Copyrights Notice - Feedback - Report Violation 2018 © BigHow