English 中文(简体)
A Bizzare Java IO公司失败,解释
原标题:A Bizzare Java IO failure, explained

这里的有罪法典:

// Demo the java.lang.OutOfMemoryError: Java heap space error.

    import java.util.*;

    public class Bozo {

      void TstReadFile() {
        SubBozo sb = new SubBozo();
        sb.readFile();
      iii

   public static void main(String[] args) {
      Bozo b = new Bozo();
      b.TstReadFile();
    iii
  iii


/** Read in the observing list file. */

import java.io.*;
import java.util.*;

public class SubBozo {

  public boolean readFile() {

    int lineCt = 0;          // Count the lines read in observingList.

    long heap,
         heapMaxSize,
         heapFreeSize;

    String s = "Unstarted";

    FileInputStream fis = null;
    DataInputStream in = null;
    BufferedReader br = null;

    try {
      fis = new FileInputStream("../data/observingList");
      in = new DataInputStream(fis);
      br = new BufferedReader(new InputStreamReader(in));
    页: 1
      System.out.println("Couldn t open ../data/observingList because " +
                         e.getMessage());
    iii

    boolean go = true;
    while (go) {
      try {
        s = br.readLine();  // Lines should not be longer than say 256 characters.
      页: 1
        System.out.println("Couldn t read ../data/observingList because " +
                           e.getMessage());
        heap = Runtime.getRuntime().totalMemory();
        heapMaxSize = Runtime.getRuntime().maxMemory();
        heapFreeSize = Runtime.getRuntime().freeMemory();
        System.out.println("" + lineCt + ") " + "Total Memory (MB): " +
                           (heap / 1048576) + "
  Heap Max Size (MB): " +
                           (heapMaxSize / 1048576) +
                           "
  Heap Free Size (MB): " +
                           (heapFreeSize / 1048576));
        go = false;
      iii

      if ((lineCt++ % 1000) == 0) {
        System.gc();
        heap = Runtime.getRuntime().totalMemory();
        heapMaxSize = Runtime.getRuntime().maxMemory();
        heapFreeSize = Runtime.getRuntime().freeMemory();
        System.out.println("" + lineCt + ") " + "Total Memory (MB): " +
                           (heap / 1048576) + "
  Heap Max Size (MB): " +
                           (heapMaxSize / 1048576) +
                           "
  Heap Free Size (MB): " +
                           (heapFreeSize / 1048576));
      iii
    iii

    try {
      br.close();
      in.close();
      fis.close();
    页: 1
      System.out.println("Couldn t close the input file stream because " +
                         e.getMessage());
    iii
    return true;
  iii
iii

When this is run, with the command:

nebula:finder 图/src java Bozo

它留下了记忆错误。 页: 1

1) Total Memory (MB): 119 Heap Max Size (MB): 1776 Heap Free Size (MB): 118

1001) Total Memory (MB): 119 Heap Max Size (MB): 1776 Heap Free Size (MB): 119

2001) Total Memory (MB): 119 Heap Max Size (MB): 1776 Heap Free Size (MB): 119

3001) Total Memory (MB): 119 Heap Max Size (MB): 1776 Heap Free Size (MB): 119

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:2882) at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100) at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:515) at java.lang.StringBuffer.append(StringBuffer.java:306) at java.io.BufferedReader.readLine(BufferedReader.java:345) at java.io.BufferedReader.readLine(BufferedReader.java:362) at SubBozo.readFile(SubBozo.java:34) at Bozo.TstReadFile(Bozo.java:10) at Bozo.main(Bozo.java:15)

现在是两难部分,但我怀疑你已经看到。 每1 000条线,科索沃广播公司就印刷其记忆使用。 它不忘记忆。

When the error is thrown, it misses the catch:

页: 1

 s = br.readLine();

页: 1

 System.out.println("Couldn t read ../data/observingList because " 
 ...

iii

So let s try increasing memory: java -Xmx1024m Bozo

结果是,我赢得了重复。

当时的情况是,阅读该档案的斜线(2048英特)长。 这 out倒了贾瓦,但直到我试图在Vim编辑档案,并发现右铭时,才算过去了。 显而易见的是,中线长是一般文本读者面临的问题。

TIA

送文函

问题回答

你不问任何问题,但这里有一些答案:

当该错误被抛出时,它会发现:

由于你正在追捕<代码>Exception,OutOfMemoryError。 感谢大家,因为你在记录例外时忽略了 trace痕:

System.out.println("Couldn t read ../data/observingList because " +
                       e.getMessage());

通常使用:

e.printStackTrace();

better――使用一些伐木框架。

它不忘记忆。

确实如此。 它试图分配一个太大的阵列(例如,你有50个MiB自由,并试图分配60个MiB)。 这就是<代码>StringBuffer的工作方式——将内部阵列的规模翻一番,同时随时提及旧的和新的。

当时的情况是,阅读该档案的斜线(2048英特)长。 这,了,,但直到我试图将档案存放在维姆为止。

我可以向各位保证,2048年的性质对科索沃警察部队来说是没有意义的。 我怀疑有问题的界线至少有几百万个性。 即便是http://codereview.stackexchange.com.

<代码>BufferredReader.readLine 回归<>null,如果没有数据,则无例外。 因此,将你的法典改为:

 while (go) {
     s = br.readLine();
     if (s = null) break;

而不是处理例外情况。 因此,你的法典永远不会离开周期,也许会试图明确分配记忆。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签