这里的有罪法典:
// 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
送文函