首页 > 解决方案 > jconsole.exe 防止了 JVM 崩溃

问题描述

我目前正在使用 amazone corretto 8、嵌入式 SQLite DB 和 Sarxos 使用 Maven 开发的 WebCam Capture API 开发 JavaFX 应用程序。应用程序本身运行平稳,满载时需要大约 1.2 GB 的 RAM。

不幸的是,当使用视频流的执行器服务重复加载和终止线程时,我的 JDK 崩溃了。但是,当我尝试使用 jconsole 进行根本原因分析时,崩溃是不可重现的。

知道如何确定崩溃的根本原因,或者 jconsole 如何防止崩溃?很难重现大量内存,但这里是视频流的代码:

 /* Method to initialize WebCam 
 * by:
 * Closing open WebCam
 * Opening WebCam based on Index position
 * Referring to method to start WebCam feed/stream to ImageView
 */
public static void initializeWebCam(final int webCamIndex) {
    
    executorService = createFixedTimeoutExecutorService(2, 3000, TimeUnit.MILLISECONDS); 
    
    Task<Void> iniWebCamTask = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            if (webCam != null) {disposeWebCamCamera();}
            webCam = Webcam.getWebcams().get(webCamIndex);
            webCam.open();
            startWebCamStreamTask();
            //stop Camera if WebCam is no longer in scene
            webCamPane.sceneProperty().addListener((obs, oldScene, newScene) -> {
                if (newScene == null) {
                    disposeWebCamCamera();
                    executorService.shutdownNow(); 
                    mbc.ewatson.App.log.info("Shutting down camera streaming thread");
                } 
            });
            
            return null;
        }
    };



/*
 * method which starts the WebCam stream
 * by:
 * creating a updated/atomic Reference which
 * sets image to null and flushing buffer/ cache
 * updated image from if loop 
 */
protected static void startWebCamStreamTask() {

    stopCamera = false;

    Task<Void> streamImageTask = new Task<Void>() {

        @Override
        protected Void call() throws Exception {
            final AtomicReference<WritableImage> ref = new AtomicReference<>();
            BufferedImage img = null;
            while (!stopCamera) {
                try {
                    if ((img = webCam.getImage()) != null) {            
                        ref.set(SwingFXUtils.toFXImage(img, ref.get()));
                        img.flush();
                        //JavaFX thread
                        Platform.runLater(new Runnable() {

                            @Override
                            public void run() {
                                imageProperty.set(ref.get());
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    };

/*Creates an executor service with a fixed pool size, that will time 
 * out after a certain period of inactivity.
 * 
 * @param poolSize The core- and maximum pool size
 * @param keepAliveTime The keep alive time
 * @param timeUnit The time unit
 * @return The executor service
 */
public static ExecutorService createFixedTimeoutExecutorService(int poolSize, long keepAliveTime, TimeUnit timeUnit){
    ThreadPoolExecutor e = 
            new ThreadPoolExecutor(poolSize, poolSize,
                keepAliveTime, timeUnit, new LinkedBlockingQueue<Runnable>());
        e.allowCoreThreadTimeOut(true);
        return e; 
     
 }

这是错误日志:

2020 年 7 月 20 日 2:15:27 NACHM。..view.trialpoints.edit.ErprobungspunkteRightFileInput$createFileInputObjects lambda$startWebCamStreamTask$4 信息:正在运行相机 [错误] 命令执行失败。org.apache.commons.exec.ExecuteException:进程退出并出现错误:-1073740940(退出值:-1073740940)在 org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:404)在 org.apache.commons .exec.DefaultExecutor.execute(DefaultExecutor.java:166) 在 org.openjfx.JavaFXBaseMojo.executeCommandLine(JavaFXBaseMojo.java:504) 在 org.openjfx.JavaFXBaseMojo.executeCommandLine(JavaFXBaseMojo.java:394) 在 org.openjfx.JavaFXRunMojo。在 org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:

标签: javamultithreadingmavenjavafxvideo-capture

解决方案


推荐阅读