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

Source: Internet
Author: User

That's because your Web application has a memory leak.

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

Unless some requirements is met (*). They is stored in the permanent heap generation by the JVM, and if you redeploy a new class

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

(*) The requirement is, all classes loaded by this classloader should being able to being 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 is 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 avoid directly referencing objects,

But use special reference objects that is easily cleared by the garbage collector. The special subclasses allow for refer to objects indirectly.

For instance, Reference have three subclasses:phantomreference, SoftReference, and WeakReference.

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

Using this method is so you can clear a reference easily by setting it to null and that the reference is pretty much IMM Utable, 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 is cleared and Ultim Ately taken out of memory.
    • Phantomreference Object:garbage Collector would not being 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 is WEAKL Y 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 is types of leaks in this Case:daemon threads and static fields.

    • Static Fields is 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 is CL Eared from memory.
    • Daemon threads that is started outside the lifecycle of a WEB application is prone to memory leaks because these threads There are references to the ClassLoader that started the threads.

Source:

Tomcat Wiki:

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

Different types of leaks that TOMCAT can detect

Other Blogs:

What does about the Java Memory Leaks:tools, Fixes, and more

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.