java outputstream java.lang.OutOfMemoryError: Java heap space


Justin Bowers
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);
medianet_width='336'; medianet_height= '280'; medianet_crid='202258688';

Related to : java outputstream java.lang.OutOfMemoryError: Java heap space
java outputstream java.lang.OutOfMemoryError: Java heap space
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);
getting Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
15/12/2014 12:00 pm 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();
}
}
}

lcdui.Image.createImage -" java.lang.OutOfMemoryError: Java heap space
Java

Hi all
I'm on a program of reading incoming images which comes as byte arrays through a socket and convert them back to images at j2me client using the following method.
[CODE]javax.microedition.lcdui.Image.createImage();[/CODE]
The programs works fine for some time. But after few minutes it prints the error
[QUOTE]
java.lang.OutOfMemoryError: Java heap space
at java.awt.image.BufferedImage.getRGB(Unknown Source)
at com.sun.kvem.midp.GraphicsBridge.loadImage(Unknown Source)
at com.sun.kvem.midp.GraphicsBridge.createImageFromData(Unknown Source)
at sun.reflect.GeneratedMethodAccessor
For Loop with millions of records - java.lang.OutOfMemoryError: Java heap space
18/12/2014 12:10 pm Java

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)



HD Wallpapers
3D
3D Abstract
City
Celebrities
Indian Celebrities
Cars
Travel
Girls
Animals Birds
Movies
Sports
Black White
Nature
Planes
Anime
Food Drink
Dreamy Fantasy
Architecture
Games
Space
Holidays
Flowers
Love
Artistic
Baby
Beach
Bikes Motorcycles
Macro
Computers
Vector
Funny
Army
Textures
Brands
Misc
Music
Other
Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © bighow.org All Rights Reserved .