A non-fatal heap memory overflow alarm method based on delay processing thread
Patent Information
- Application Number
- CN202611382101.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-09-08
- Publication Date
- 2026-10-09
AI Technical Summary
当前主流处理方式包括:通过 JVM 参数-XX:+ExitOnOutOfMemoryError在OOM时立即终止进程,依赖容器平台重建实例、在Thread.UncaughtExceptionHandler 中捕获 OOM 后立即发送邮件或上传堆转储文件、仅启用-XX:+HeapDumpOnOutOfMemoryError生成堆转储文件,依赖人工或离线脚本后续处理等,但无法实现既能识别JVM仍可运行的非致命OOM场景,又能完成堆转储文件检测与告警联动
[0005]与现有技术相比,本发明的有益效果为:本发明提出初始化→捕获→延迟→诊断与告警的四阶段轻量级OOM响应方法,既能识别JVM仍可运行的非致命OOM场景,又能完成堆转储文件检测与告警联动,提升了Java应用在发生OOM时的可观测性、稳定性与运维效率;在OOM捕获阶段仅执行异常类型判断及入队操作,不进行任何内存分配或I/O操作,有效防止在内存临界状态下因诊断行为加剧资源耗尽,从而避免应用雪崩或进程退出;在延迟任务执行阶段自动提取线程名称、线程组、线程状态、完整执行栈及异常堆栈等关键快照,并基于预设项目包路径前缀智能过滤无关栈帧,确保告警内容聚焦业务代码,提升根因定位效率;通过设定延迟时间固定延迟,使JVM获得充分的GC回收与堆转储文件写入时间;配合上传成功确认机制,确保告警动作仅在资源稳定后执行,大幅降低因资源竞争导致的告警失败率;OOM调度守护线程、邮件客户端与对象存储客户端均在应用启动阶段完成初始化,避免在OOM后创建线程或连接引发不可控行为,确保响应流程高度可靠;设计内存占用可控、不含原始引用的结构化快照对象,从根本上规避因快照持有大对象引用导致的 GC 压力或内存泄漏风险。
Smart Images

Figure CN122884741A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the fields of JVM anomaly monitoring and cloud-native observability technology, and in particular to a non-fatal heap memory overflow alarm method based on delayed processing threads. Background Technology
[0002] In Java application operations and maintenance, OutOfMemoryError (OOM) is a common and serious exception. An OOM not only means that the current request cannot be processed normally, but it can also lead to application process crashes, service unavailability, and serious economic losses and a degraded user experience. Therefore, it is necessary to quickly capture, diagnose, and alert when an OOM occurs, while preventing secondary memory crashes caused by the response operation itself. Current mainstream handling methods include: immediately terminating the process when an OOM occurs using the JVM parameter -XX:+ExitOnOutOfMemoryError, relying on the container platform to rebuild the instance; immediately sending an email or uploading a heap dump file after catching the OOM in Thread.UncaughtExceptionHandler; and only enabling -XX:+HeapDumpOnOutOfMemoryError to generate a heap dump file, relying on manual or offline scripts for subsequent processing. However, these methods cannot simultaneously identify non-fatal OOM scenarios where the JVM can still run, and also complete heap dump file detection and alert linkage. Summary of the Invention
[0003] The purpose of this invention is to address the shortcomings of existing technologies by proposing a non-fatal heap memory overflow alarm method based on a delayed processing thread.
[0004] To achieve the above objectives, the present invention adopts the following technical solution: a non-fatal heap memory overflow alarm method based on a delayed processing thread, comprising the following steps: S1: During the application startup phase, the initialization of the OOM scheduling daemon thread, bounded blocking queue, email client, and object storage client is completed. S11: When a Java application starts, parse the JVM startup parameters, determine whether the heap dump function is enabled, and determine the expected heap dump file path accordingly. Includes the following sub-steps: S111: If the parameter -XX:+HeapDumpOnOutOfMemoryError is detected, then the flag heapDumpEnabled=true; otherwise, the flag heapDumpEnabled=false. S112: If the heapDumpEnabled flag is true, then further check if -XX:HeapDumpPath= exists.<base_path> parameter; If it exists, then determine<base_path> Does it end with .hprof? If so, then the heap dump file path is...<base_path> Otherwise, treat it as a directory and append / java_${pid}.hprof; If it does not exist, set the heap dump file path to ${user.dir} / java_${pid}.hprof; Where ${pid} is the current Java process ID, and ${user.dir} is the current working directory when the JVM starts; S12: Create an OOM bounded blocking queue to temporarily store the exception thread object and the exception object; Furthermore, the capacity of the OOM bounded blocking queue is 32; S13: Create an OOM scheduling daemon thread, complete initialization during application startup, and set its priority to the highest. S14: Initialize the email client and object storage client; S2: Register a global uncaught exception handler and determine if the exception object is of type OutOfMemoryError; S21: Call the Thread.setDefaultUncaughtExceptionHandler method to register a global uncaught exception handler; S22: When any thread throws an uncaught exception or error during execution and the thread is about to terminate abnormally, the JVM calls back the global uncaught exception handler, which receives the thread object that caused the exception and the exception object. S23: Determine if the exception object is of type OutOfMemoryError; If not, skip; If so, the thread object and exception object are placed in the OOM bounded blocking queue, and the process terminates immediately without performing any memory allocation or I / O operations. S3: The OOM scheduling daemon thread responds and executes with a delay; S31: The OOM scheduling daemon periodically retrieves thread objects and exception objects from the OOM bounded blocking queue; S32: Submit the retrieved thread object and exception object to a delayed task and set the delay time for the delayed task; Furthermore, the delay time is 5 seconds; S4: Generate an OOM snapshot object and execute a tiered alert; S41: Read the thread name, thread group name, and thread status and attributes of the thread object; S42: Obtain the complete execution stack of the thread object. Based on the preset project package path prefix, filter out the thread frames that match the package path from the execution stack and store them in the execution stack string array. Furthermore, the preset project package path prefix is the root package path of the business project; S43: Read the exception type and error message of the exception object; S44: Obtain the complete exception stack of the exception object, and based on the preset project package path prefix, filter out the thread frames belonging to the project package from the exception stack and store them in the exception stack string array. S45: Encapsulate the above fields into an OOM snapshot object; Furthermore, the fields include at least: the thread name, thread group name, and thread state and attributes of the thread object, the complete execution stack, the execution stack string array, the exception type and error message of the exception object, the complete exception stack, and the exception stack string array; S46: Get the application name of the Java application; S47: Determine if the delayed task's heap dump file path exists and issue a tiered warning; If it exists, call the object storage client to upload the heap dump file to the cloud, obtain the download link, confirm that the heap dump file has been successfully uploaded, delete the local heap dump file, and send the OOM snapshot object, download link and application name to the operations and maintenance personnel via email client; Furthermore, the heap dump file is a binary snapshot file; If it does not exist, the OOM snapshot object, application name, and prompt message will be sent to the operations and maintenance personnel via email client; Furthermore, the prompt message indicates that the heap dump file has not been generated and / or please check if the configuration is enabled.
[0005] Compared with existing technologies, the beneficial effects of this invention are as follows: This invention proposes a four-stage lightweight OOM response method: initialization → capture → delay → diagnosis and alarm. This method can identify non-fatal OOM scenarios where the JVM can still run, and can also perform heap dump file detection and alarm linkage, improving the observability, stability, and operational efficiency of Java applications when OOM occurs. During the OOM capture stage, only exception type judgment and enqueueing operations are performed, without any memory allocation or I / O operations, effectively preventing resource exhaustion caused by diagnostic actions in critical memory states, thereby avoiding application avalanche or process exit. During the delayed task execution stage, key snapshots such as thread name, thread group, thread state, complete execution stack, and exception stack are automatically extracted. It intelligently filters irrelevant stack frames based on preset project package path prefixes, ensuring that alarm content focuses on business code and improving root cause location efficiency; by setting a fixed delay time, it allows the JVM sufficient time for GC reclamation and heap dump file writing; combined with a successful upload confirmation mechanism, it ensures that alarm actions are only executed after resources are stable, significantly reducing the alarm failure rate caused by resource contention; the OOM scheduling daemon thread, email client, and object storage client are all initialized during the application startup phase, avoiding the creation of threads or connections after OOM and causing uncontrollable behavior, ensuring a highly reliable response process; it designs structured snapshot objects with controllable memory usage and no original references, fundamentally avoiding the risk of GC pressure or memory leaks caused by snapshots holding large object references. Attached Figure Description
[0006] Figure 1 This is a flowchart of the non-fatal heap memory overflow alarm method based on delayed processing threads according to the present invention. Detailed Implementation
[0007] To provide a further understanding of the purpose, structure, features, and functions of the present invention, detailed descriptions are provided below with reference to specific embodiments.
[0008] Please refer to the reference. Figure 1 This invention provides a non-fatal heap memory overflow alarm method based on a delayed processing thread, comprising the following steps: S1: During the application startup phase, the initialization of the OOM scheduling daemon thread, bounded blocking queue, email client, and object storage client is completed. S11: When a Java application starts, parse the JVM startup parameters, determine whether the heap dump function is enabled, and determine the expected heap dump file path accordingly. Includes the following sub-steps: S111: If the parameter -XX:+HeapDumpOnOutOfMemoryError is detected, then the flag heapDumpEnabled=true; otherwise, the flag heapDumpEnabled=false. S112: If the heapDumpEnabled flag is true, then further check if -XX:HeapDumpPath= exists.<base_path> parameter; If it exists, then determine<base_path> Does it end with .hprof? If so, then the heap dump file path is...<base_path> Otherwise, treat it as a directory and append / java_${pid}.hprof; If it does not exist, set the heap dump file path to ${user.dir} / java_${pid}.hprof; Where ${pid} is the current Java process ID, and ${user.dir} is the current working directory when the JVM starts; S12: Create an OOM bounded blocking queue to temporarily store the exception thread object and the exception object; Furthermore, the capacity of the OOM bounded blocking queue is 32; A bounded blocking queue with a capacity of 32 is used to temporarily store abnormal threads and abnormal objects, which are then consumed asynchronously by a daemon thread, achieving complete decoupling between exception capture and diagnosis. The queue naturally blocks when full, preventing event loss and avoiding prolonged occupation of the producer thread. S13: Create an OOM scheduling daemon thread, complete initialization during application startup, and set its priority to the highest. The OOM scheduling daemon thread is initialized and set to the highest priority during the application startup phase, ensuring that it can respond to OOM events in a timely manner even when the system load is extremely high, thus improving the timeliness of emergency handling. S14: Initialize the email client and object storage client; The OOM scheduling daemon thread, email client, and object storage client are all initialized during the application startup phase to avoid creating threads or connections after an OOM and causing uncontrollable behavior, thus ensuring a highly reliable response process. S2: Register a global uncaught exception handler and determine if the exception object is of type OutOfMemoryError; S21: Call the Thread.setDefaultUncaughtExceptionHandler method to register a global uncaught exception handler; S22: When any thread throws an uncaught exception or error during execution and the thread is about to terminate abnormally, the JVM calls back the global uncaught exception handler, which receives the thread object that caused the exception and the exception object. S23: Determine if the exception object is of type OutOfMemoryError; If not, skip; If so, the thread object and exception object are placed in the OOM bounded blocking queue, and the process terminates immediately without performing any memory allocation or I / O operations. During the OOM capture phase, only exception type judgment and enqueue operation are performed, without any memory allocation or I / O operation. This effectively prevents resource exhaustion caused by diagnostic behavior in critical memory states, thereby avoiding application avalanche or process exit. S3: The OOM scheduling daemon thread responds and executes with a delay; S31: The OOM scheduling daemon periodically retrieves thread objects and exception objects from the OOM bounded blocking queue; S32: Submit the retrieved thread object and exception object to a delayed task and set the delay time for the delayed task; Furthermore, the delay time is 5 seconds; Set a fixed delay time to allow the JVM sufficient time for GC reclamation and heap dump file writing; S4: Generate an OOM snapshot object and execute a tiered alert; S41: Read the thread name, thread group name, and thread status and attributes of the thread object; S42: Obtain the complete execution stack of the thread object. Based on the preset project package path prefix, filter out the thread frames that match the package path from the execution stack and store them in the execution stack string array. Furthermore, the preset project package path prefix is the root package path of the business project; S43: Read the exception type and error message of the exception object; S44: Obtain the complete exception stack of the exception object, and based on the preset project package path prefix, filter out the thread frames belonging to the project package from the exception stack and store them in the exception stack string array. During the delayed task execution phase, key snapshots such as thread name, thread group, thread state, complete execution stack, and exception stack are automatically extracted, and irrelevant stack frames are intelligently filtered based on the preset project package path prefix to ensure that the alarm content focuses on the business code and improve the efficiency of root cause location. S45: Encapsulate the above fields into an OOM snapshot object; Furthermore, the fields include at least: the thread name, thread group name, and thread state and attributes of the thread object, the complete execution stack, the execution stack string array, the exception type and error message of the exception object, the complete exception stack, and the exception stack string array; Design structured snapshot objects with controllable memory usage and no original references to fundamentally avoid GC pressure or memory leak risks caused by snapshots holding large object references; S46: Get the application name of the Java application; S47: Determine if the delayed task's heap dump file path exists and issue a tiered warning; If it exists, call the object storage client to upload the heap dump file to the cloud, obtain the download link, confirm that the heap dump file has been successfully uploaded, delete the local heap dump file, and send the OOM snapshot object, download link and application name to the operations and maintenance personnel via email client; Furthermore, the heap dump file is a binary snapshot file; With the successful upload confirmation mechanism, alarm actions are ensured to be executed only after resources have stabilized, significantly reducing the alarm failure rate caused by resource contention; If it does not exist, the OOM snapshot object, application name, and prompt message will be sent to the operations and maintenance personnel via email client; Furthermore, the prompt message indicates that the heap dump file has not been generated and / or please check if the configuration is enabled.
[0009] This invention proposes a four-stage lightweight OOM response method: initialization → capture → delay → diagnosis and alerting. During application startup, pre-initialization of the daemon thread, bounded blocking queue, email client, and object storage client is completed, ensuring that subsequent response processes do not require runtime creation of large objects, which exacerbates OOM. After the global exception handler captures the OOM, only exception type judgment and enqueueing operations are performed; no diagnosis, I / O, or memory allocation operations are performed, and the process immediately returns, completely offloading the response pressure to the asynchronous thread. After the daemon thread retrieves the exception context from the blocking queue, it submits a delayed execution task, reserving sufficient time for JVM GC reclamation and heap dump file generation. After the delayed task starts, lightweight diagnosis and tiered alerting are uniformly executed, achieving atomic processing of diagnosis and alerting. Zero memory allocation is ensured throughout the exception capture stage. All dependent resources are pre-initialized during startup to avoid secondary crashes caused by dynamic resource creation after an OOM. Snapshot objects do not contain any original Thread or Throwable references, eliminating GC pressure and memory leak risks.
[0010] The present invention has been described in the above-described embodiments; however, these embodiments are merely examples for implementing the present invention. It must be noted that the disclosed embodiments do not limit the scope of the present invention. Conversely, any modifications and refinements made without departing from the spirit and scope of the present invention are within the scope of patent protection of the present invention.
Claims
1. A non-fatal heap memory overflow alarm method based on a delayed processing thread, characterized in that: Includes the following steps: S1: During the application startup phase, the initialization of the OOM scheduling daemon thread, bounded blocking queue, email client, and object storage client is completed. S11: When a Java application starts, parse the JVM startup parameters, determine whether the heap dump function is enabled, and determine the expected heap dump file path accordingly. S12: Create an OOM bounded blocking queue to temporarily store the exception thread object and the exception object; S13: Create an OOM scheduling daemon thread, complete initialization during application startup, and set its priority to the highest. S14: Initialize the email client and object storage client; S2: Register a global uncaught exception handler and determine if the exception object is of type OutOfMemoryError; S21: Call the Thread.setDefaultUncaughtExceptionHandler method to register a global uncaught exception handler; S22: When any thread throws an uncaught exception or error during execution and the thread is about to terminate abnormally, the JVM calls back the global uncaught exception handler, which receives the thread object that caused the exception and the exception object. S23: Determine if the exception object is of type OutOfMemoryError; If not, skip; If so, the thread object and exception object are placed in the OOM bounded blocking queue, and the process terminates immediately without performing any memory allocation or I / O operations. S3: The OOM scheduling daemon thread responds and executes with a delay; S31: The OOM scheduling daemon periodically retrieves thread objects and exception objects from the OOM bounded blocking queue; S32: Submit the retrieved thread object and exception object to a delayed task and set the delay time for the delayed task; S4: Generate an OOM snapshot object and issue tiered alerts; S41: Read the thread name, thread group name, and thread status and attributes of the thread object; S42: Obtain the complete execution stack of the thread object. Based on the preset project package path prefix, filter out the thread frames that match the package path from the execution stack and store them in the execution stack string array. The default project package path prefix is the root package path of the business project; S43: Read the exception type and error message of the exception object; S44: Obtain the complete exception stack of the exception object, and based on the preset project package path prefix, filter out the thread frames belonging to the project package from the exception stack and store them in the exception stack string array. S45: Encapsulate the above fields into an OOM snapshot object; S46: Get the application name of the Java application; S47: Determine if the delayed task's heap dump file path exists and issue a tiered warning; If it exists, call the object storage client to upload the heap dump file to the cloud, obtain the OOM scheduling daemon thread download link, confirm that the heap dump file has been successfully uploaded, delete the local heap dump file, and send the OOM snapshot object, the OOM scheduling daemon thread download link and the application name to the operations and maintenance personnel via email client; If it does not exist, the OOM snapshot object, application name, and prompt message will be sent to the operations and maintenance personnel via email client.
2. The non-fatal heap memory overflow alarm method based on delayed processing threads as described in claim 1, characterized in that: Step S11 includes the following sub-steps: S111: If the parameter -XX:+HeapDumpOnOutOfMemoryError is detected, then the flag heapDumpEnabled=true; otherwise, the flag heapDumpEnabled=false. S112: If the heapDumpEnabled flag is true, then further check if -XX:HeapDumpPath= exists.<base_path> parameter; If it exists, then determine<base_path> Does it end with .hprof? If so, then the heap dump file path is...<base_path> Otherwise, treat it as a directory and append / java_${pid}.hprof; If it does not exist, set the heap dump file path to ${user.dir} / java_${pid}.hprof; Where ${pid} is the current Java process ID, and ${user.dir} is the current working directory when the JVM starts.
3. The non-fatal heap memory overflow alarm method based on delayed processing threads as described in claim 1, characterized in that: In step S12, the capacity of the OOM bounded blocking queue is 32.
4. The non-fatal heap memory overflow alarm method based on delayed processing threads as described in claim 1, characterized in that: In step S45, the fields include at least: the thread name, thread group name, thread state and attributes of the thread object, the complete execution stack, the execution stack string array, the exception type and error message of the exception object, the complete exception stack, and the exception stack string array.
5. The non-fatal heap memory overflow alarm method based on delayed processing threads as described in claim 1, characterized in that: In step S47, the heap dump file is a binary snapshot file; the prompt message is that the heap dump file has not been generated and / or please check whether the configuration is enabled.