java - 使用 linux-redhat 的 java 上的内存不足异常

标签 java linux out-of-memory redhat

我在 linux/redhat 上遇到内存不足问题,但同样的程序在我的 windows 机器上运行。

我的 linux 机器配置是 15Gb RAM。

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.sql.ResultSet;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/**
 *
 * @author ndoshi
 */
public class Dwnld {

    BufferedImage bi8 = null, bi16 = null;
    ImageIcon ii = null;
    ResultSet rs, rsDwnld;
    String OG = "ogImage\\";
    String CROP8 = "Crop8\\";
    String CROP16 = "Crop16\\";
    String TIME = "", ErrorLog = "", ErrorLogPro = "";
    int hashInc8 = 0;
    int hashInc16 = 0;
    int totalround = 0;
    int countProcess = 0;
    boolean download_new = false;
    private int row = 0;
    int Dwnld = 0, NotDwnld = 0;
    final String OP_Log = "Log", OP_Error = "ErrorLog", OP_ErrorPro = "ErrorLogProcess";

    int r, g, b, k, ih, j;
    int sr = 0, sg = 0, sb = 0, sk = 0;
    int rg, gg, bg, kg;
    String s = "", s1 = "", hash16, hash8;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Dwnld();
    }

    public Dwnld(){
    try {
            BufferedImage image = null;
            InputStream is = null;
            OutputStream os = null;
            URL url = new URL("https://rukminim1.flixcart.com/image/312/312/t-shirt/u/g/k/33solidblackmelangeblack-sayitloud-m-original-imaehfytzzzazfyf.jpeg?q=70");
            is = url.openStream();
            os = new FileOutputStream(OG + "1.jpg");
            byte[] b = new byte[2048];
            int length;
            while ((length = is.read(b)) != -1) {
                os.write(b, 0, length);
            }
            image = ImageIO.read(new File(OG + "1.jpg"));
            is.close();
            os.close();
            System.out.println("Hash 16 = "+hash16);
            System.out.println("Hash 8 = "+hash8);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

    }

}

我通过使用 XMS 和 XMX 增加内存来运行相同的

java -Xms2048m -Xmx6096m Dwnld 

错误:

Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
        at java.lang.Thread.start0(Native Method)
        at java.lang.Thread.start(Thread.java:714)
        at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1056)
        at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1332)
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1359)
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1343)
        at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1301)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
        at java.net.URL.openStream(URL.java:1037)
        at Dwnld.<init>(Dwnld.java:53)
        at Dwnld.main(Dwnld.java:43)

最佳答案

首先获取线程转储并尝试对其进行分析。

或者,还有许多其他方法可以作为解决方法:

  1. /proc/sys/kernel/threads-max 文件为线程数提供了系统范围的限制。 root 用户可以根据需要更改该值:

    $ echo 100000 >/proc/sys/kernel/threads-max

  2. 在 linux 的情况下,线程只是具有共享地址空间的进程,因此您应该检查每个用户的进程数。

  3. 检查线程 PID 限制,因为存在基于 kernel.pid_max 限制参数值的最大 PID 限制。 使用此 命令 $ sysctl -a | grep kernel.pid_max 获取最大允许 PID。
  4. 如果您无法修改操作系统设置,则减少堆栈大小。使用类似 JAVA_OPTS="-Xss256k"
  5. 的东西

关于java - 使用 linux-redhat 的 java 上的内存不足异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38992252/

相关文章:

linux - 在 Docker Alpine 容器中启动 shell

linux - 如何在 lunux 中找到进程及其所有子进程的总内存和 cpu 使用情况

android - android模拟器上的内存不足错误,但在设备上没有

java - OutofMemory 链表添加错误

java - 将java中的arraylist转换为c

Java循环链表,删除无法正常工作的节点

java - 将 java 对象发送到 rest WebService

java - 创建 jdbc odbc 连接而不在 MS ACCESS 中创建 DSN

objective-c - Apple 平台是唯一适合 Objective-C 的地方吗?

android - 有没有办法预测和防止 OOM 异常?