简体   繁体   English

无限 while 循环给出 java.lang.OutOfMemoryError: Java heap space

[英]infinite while loop gives java.lang.OutOfMemoryError: Java heap space

if i want to run a while loop up to infinity and i am getting Java heap space but i am using "system garbage cleaner " to clean up memory.如果我想将 while 循环运行到无穷大并且我正在获得 Java 堆空间,但我正在使用“系统垃圾清理器”来清理内存。 so how can i solve this problem.那么我该如何解决这个问题。

try{
    while(true){
        try{
            attach=sample.readMails();         
            System.gc();
        }
        catch (IOException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("Error In  readMails-->downloads");
        }
    }
}catch (Exception e){
    // TODO Auto-generated catch block
    e.printStackTrace();
    System.out.println("Error In  Infinite While");
}

System.gc only works for unused objects. System.gc仅适用于未使用的对象。 According to docs of System#gc根据System#gc 的文档

Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse.调用 gc 方法表明 Java 虚拟机花费精力来回收未使用的对象,以使它们当前占用的内存可用于快速重用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM