zoukankan      html  css  js  c++  java
  • Why does the memory usage increase when I redeploy a web application?

     

    That is because your web application has a memory leak.

    A common issue are "PermGen" memory leaks. They happen because the Classloader (and the Class objects it loaded) cannot be recycled

    unless some requirements are met (*). They are stored in the permanent heap generation by the JVM, and when you redeploy a new class

    loader is created, which loads another copy of all these classes. This can cause OufOfMemoryErrors eventually.

     

    (*) The requirement is that all classes loaded by this classloader should be able to be gc'ed at the same time.

     

    How to Avoid Java Memory Leaks

    To avoid memory leaks, you need to pay attention to how you write your code. Here are specific methods to help you stamp out memory leaks.

    1. Use reference objects to avoid memory leaks

    Using the java.lang.ref package, you can work with the garbage collector in your program. This allows you to avoid directly referencing objects,

    but use special reference objects that are easily cleared by the garbage collector. The special subclasses allow you to refer to objects indirectly.

    For instance, Reference has three subclasses: PhantomReference, SoftReference, and WeakReference.

    A referent, or an object that is referenced by these subclasses, can be accessed using that reference object’s get method. The advantage of

    using this method is that you can clear a reference easily by setting it to null and that the reference is pretty much immutable, or it cannot be

    changed. How does garbage collector act with each type of referent?

    • SoftReference object: Garbage collector is required to clear all SoftReference objects when memory runs low.
    • WeakReference object: When garbage collector senses a weakly referenced object, all references to it are cleared and ultimately taken out of memory.
    • PhantomReference object: Garbage collector would not be able to automatically clean up PhantomReference objects, you would need to clean it up manually by clearing all references to it.

    Using reference objects, you can work with the garbage collector to automate the task of removing listeners that are weakly reachable.

    WeakReference objects, especially with a cleanup thread, can help you avoid memory errors.

    2. Avoid memory leaks related to a WebApp classloader

    If you are using Jetty 7.6.6. or higher, you can prevent WebApp classloader pinning. When your code keeps referring to a webapp classloader,

    memory leaks can easily happen. There are two types of leaks in this case: daemon threads and static fields.

    • Static fields are started with the classloader’s value. Even as Jetty stops deploying and then redeploys your webapp, the static reference persists and so the object cannot be cleared from memory.
    • Daemon threads that are started outside the lifecycle of a Web application are prone to memory leaks because these threads have references to the classloader that started the threads.

    出处:

    tomcat wiki: 

    Why does the memory usage increase when I redeploy a web application?

    Different types of leaks that Tomcat can detect 

    其他博客:

    What to Do About Java Memory Leaks: Tools, Fixes, and More

  • 相关阅读:
    配置OSPF负载分担
    IPv4静态路由与NQA联动
    静态路由实现路由负载分担
    静态路由实现主备备份
    (一)非整数幂情形下的广义牛顿二项式定理
    小小知识点(二十三)被科研人员忽略的ORCID —— 如何注册和使用?意义何在?
    小小知识点(二十二)word 排版技巧大全
    小小知识点(二十一)Mathtype怎么批量更改全文的公式格式
    小小知识点(十九)如何破解安装编辑PDF文本的软件——福昕编辑器和Adobe acrobat DC
    (三十二)5G前传、中传和回传
  • 原文地址:https://www.cnblogs.com/yuyutianxia/p/7746849.html
Copyright © 2011-2022 走看看