【问题标题】:Setting JVM heap options in Azure App Service在 Azure 应用服务中设置 JVM 堆选项
【发布时间】:2019-04-20 07:53:42
【问题描述】:

我一直在尝试将应用程序部署到 Azure 应用程序服务(免费层),但由于内存限制,该应用程序由于占用过多内存而不断被终止。当然,解决这个问题最简单的方法是在启动时在 JVM 上设置-Xmx900m,但是到目前为止我尝试过的方法都没有奏效。

我尝试过的:

  1. 将-Xmx900m 添加到web.config httpPlatform 标记的arguments 属性中(在开头)。
  2. 在 JAVA_OPTS 环境变量中添加了-Xmx900m(在应用程序设置中)
  3. 使用web.config 文件将-Xmx900m 添加到JAVA_OPTS 环境变量中。

问题是我无法验证这些中的任何一个是否真的有效,因为在 Kudu 中,我看不到所使用的命令行,或者除了查看它使用了多少内存之外,我看不到它在任何时候都使用了多少内存来检查进程。给定的时刻。在每种情况下,似乎该设置都被忽略了,因为内存使用量攀升至 1024MB,然后进程因内存分配失败而崩溃。

我可以做些什么来设置 Azure App Service Java 应用程序中的最大堆设置吗?

【问题讨论】:

    标签: java azure azure-web-app-service


    【解决方案1】:

    如果你在 Kudu 中有 java 进程,你可以检查 java 进程 PID 和 jps 然后在 jdk/bin 目录中使用 jmap -heap [pid] ,你会得到堆内存的详细信息。

    如果您没有,您可以创建WebJob 以获取详细信息。我得到详细信息并将其放入队列中。这是示例代码。

     public static  final String storageConnectionString =
            "DefaultEndpointsProtocol=https;" +
                    "AccountName=******;" +
                    "AccountKey=*********;" +
                    "EndpointSuffix=core.windows.net";
    
    public static void main( String[] args )
    {
    
    
        MemoryMXBean memorymbean = ManagementFactory.getMemoryMXBean();
        MemoryUsage usage = memorymbean.getHeapMemoryUsage();
        System.out.println("INIT HEAP: " + usage.getInit()/(1024*1024));
        System.out.println("MAX HEAP: " + usage.getMax()/(1024*1024));
        System.out.println("USE HEAP: " + usage.getUsed()/(1024*1024));
        System.out.println("\nFull Information:");
        System.out.println("Heap Memory Usage: "
                + memorymbean.getHeapMemoryUsage());
        System.out.println("Non-Heap Memory Usage: "
                + memorymbean.getNonHeapMemoryUsage());
    
    
        CloudQueueMessage message=null;
    
        try {
            CloudStorageAccount storageAccount =
                    CloudStorageAccount.parse(storageConnectionString);
    
            // Create the queue client.
            CloudQueueClient queueClient = storageAccount.createCloudQueueClient();
    
            // Retrieve a reference to a queue.
            CloudQueue queue = queueClient.getQueueReference(**queuename**);
    
            // Create the queue if it doesn't already exist.
            queue.createIfNotExists();
    
            // Create a message and add it to the queue.
            CloudQueueMessage errormessage = new CloudQueueMessage("INIT HEAP: " + usage.getInit()+
                                                                            "MAX HEAP: " + usage.getMax()+
                                                                            "USE HEAP: " + usage.getUsed() +
                                                                            "Heap Memory Usage: " + memorymbean.getHeapMemoryUsage()+
                                                                            "Non-Heap Memory Usage: " + memorymbean.getNonHeapMemoryUsage());
            queue.addMessage(errormessage);
    
            // Download the approximate message count from the server.
            queue.downloadAttributes();
    
            // Retrieve the newly cached approximate message count.
            long cachedMessageCount = queue.getApproximateMessageCount();
    
            // Display the queue length.
            System.out.println(String.format("Queue length: %d", cachedMessageCount));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (StorageException e) {
            e.printStackTrace();
        }
    }
    

    如果您仍有疑问,请告诉我。

    【讨论】:

    • 我不想获取有关堆的信息;我正在尝试设置最大堆,这个设置似乎被 JVM 忽略了,因为 Kudu 报告的内存使用量超出了“java”进程用于 -Xmx 的设置。
    • 您可以使用它来检查您的更改是否有效。而你改变JVM的方式是正确的,你可以参考这个答案:stackoverflow.com/a/26155140/10383250
    猜你喜欢
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 2016-07-18
    • 2020-11-12
    • 2012-07-13
    • 1970-01-01
    相关资源
    最近更新 更多