spring-cloud-consul icon indicating copy to clipboard operation
spring-cloud-consul copied to clipboard

Memory leak with Consul Config Watch

Open nickcodefresh opened this issue 10 years ago • 71 comments

I raised the following issue on Stackoverflow because I was experiencing my Spring Boot app just eating away at the heap

https://stackoverflow.com/questions/37137358/huge-org-springframework-boot-loader-launchedurlclassloader-memory-usage

After investigation is appears as though Consul is returning a changing X-Consul-Index header value when requesting KVs that do not exist.

For example http://consul:8500/v1/kv/mio/application,server/?recurse&wait=2s&index=281722, where mio/application,server is the default content and one of the active Spring profiles. 281722 was the index value returned from the previous call, and Consul may now return a completely different value.

A changing X-Consul-Index value causes the Spring Cloud to refresh the context (via org.springframework.cloud.consul.config.ConfigWatch#watchConfigKeyValues), which in turn creates a new instance of org.springframework.cloud.consul.config.ConsulPropertySourceLocator. For whatever reason, the old instance of ConsulPropertySourceLocator aren't garbage collected, leading to a memory leak.

I'm using Spring Boot c1.3.2.RELEASE, Spring Cloud vBrixton.RC2 and Consul v0.6.4.

At the moment the only way round this for us is disable config watch via spring.cloud.consul.config.watch.enabled

nickcodefresh avatar May 10 '16 14:05 nickcodefresh

Can you debug your app and put a break point in the ConsulPropertySourceLocator constructor. Capture the stack when that is called (after startup), and attach it here. Or we could do that if you send us a simple sample to reproduce the issue. I don't see any calls to the constructor once my sample app is up and running.

dsyer avatar May 10 '16 17:05 dsyer

Yep sure, I'll do that tomorrow. It'll have to be the stack, as i don't have the time to create a sample app alas.

nickcodefresh avatar May 10 '16 21:05 nickcodefresh

Actually, nevermind about the breakpoint: your analysis already points at the config watch as a problem (but our samples don't get pinged with that X-Consul-Index thing), and you can simulate the same thing just by POSTing to /refresh. What we need is to check your classpath for old versions of stuff, because we don't think there is a leak in the latest spring-cloud-context.

This issue looks like it belongs in spring-cloud-commons, but we can keep pushing on it here till we understand what's going on.

dsyer avatar May 11 '16 06:05 dsyer

I'm not sure how you want me to check the classpath, but here's the Maven dependency tree output

output.txt

nickcodefresh avatar May 11 '16 07:05 nickcodefresh

Thanks, that's useful, but rules out a classpath issue I think. Still it would be a good idea for you to try with the latest code (Brixton.RELEASE is all but done, so try a snapshot and tell us what happens).

dsyer avatar May 11 '16 07:05 dsyer

You should be able to reproduce a fairly quickly changing X-Consul-Index by using Consul as a docker overlay network backend - we've had it set up with a single (unused) overlay network and I think this is what was causing the constant index increase (since I've removed it the index hasn't changed for 12+ hrs).

edit: you don't even need an overlay network, just connect one docker daemon to Consul using --cluster-store=consul://x.x.x.x:8500/network --cluster-advertise=eth0:2375

choppedpork avatar May 11 '16 09:05 choppedpork

We tried using Spring Cloud Brixton.BUILD-SNAPSHOT but we had the same issue of an ever increasing classes loaded metric.

nickcodefresh avatar May 11 '16 10:05 nickcodefresh

Did you try triggering a manual garbage collection? Is this with or without the X-Consul-Index forcing a refresh all the time?

dsyer avatar May 11 '16 10:05 dsyer

I noticed a similar memory leak, with lots of cglib enhanced MethodProxy classes but using the Eureka, every Eureka registry update / healthcheck created some more classes, one for each registered instance in Eureka.

I was going to raise an issue but the updating to the latest version of spring boot 1.3.6 fixed the problem for me. Not sure if it was 1.3.4 or 1.3.5 where I was seeing the issue.

nissel avatar Jul 18 '16 14:07 nissel

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

spencergibb avatar Sep 07 '16 12:09 spencergibb

We have run into something that may be similar. I've put together a simple service to reproduce.

https://github.com/dahamr/springCloudConsulTest

If I understand the additional information you are requesting:

  • yes I forced a manual gc in visualvm before taking the heap dump
  • I did not use the /refresh endpoint to reproduce

dahamr avatar Sep 08 '16 14:09 dahamr

@dahamr can you be more detailed about what you can "reproduce"? Do you see unbounded memory growth that never gets reclaimed?

spencergibb avatar Sep 08 '16 15:09 spencergibb

Yes, I see memory growth that never gets reclaimed. For every refresh generated by the consul update, I see a new instance of AnnotationConfigApplicationContext being created and the old one is never gc'd. It isn't a substantial amount of memory but we ultimately hit the "java.lang.OutOfMemoryError: GC overhead limit exceeded" exception.

dahamr avatar Sep 08 '16 16:09 dahamr

@dahamr I had to comment out your tests as they were written for boot 1.4.

Also, I changed the build.gradle to use the BOM rather than managing versions by hand (there were mismatched versions).

dependencyManagement {
    imports {
        mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Brixton.SR5'
    }
}

dependencies {
    compile "org.springframework.cloud:spring-cloud-starter-consul-all"
//    runtime "org.springframework.cloud:spring-cloud-consul-core:1.0.0.RELEASE"
//    runtime "org.springframework.cloud:spring-cloud-consul-config:1.0.2.RELEASE"
//    runtime "org.springframework.cloud:spring-cloud-consul-discovery:1.0.0.RELEASE"
//    runtime "org.springframework.cloud:spring-cloud-context:1.1.1.RELEASE"
//    compile "org.springframework.cloud:spring-cloud-commons:1.1.0.RELEASE"
//    runtime "com.ecwid.consul:consul-api:1.1.9"
//    runtime 'joda-time:joda-time:2.8.2'

    // dependencies stated in https://cloud.spring.io/spring-cloud-consul for consul retry
    compile 'org.springframework.retry:spring-retry'
    compile("org.springframework.boot:spring-boot-starter-aop")

    compile("org.springframework.boot:spring-boot-starter-web")
    // tag::actuator[]
    compile("org.springframework.boot:spring-boot-starter-actuator")
    // end::actuator[]
    // tag::tests[]
    testCompile("org.springframework.boot:spring-boot-starter-test")
    // end::tests[]
}

I only ever see one AnnotationConfigApplicationContext.

applicationcontextcount

Going to let this run (updated property every second) for a while to see if I see a leak. If there is it doesn't match what you've reported.

spencergibb avatar Sep 08 '16 20:09 spencergibb

visual vm was borked, I'm seeing the increate in AnnotationConfigApplicationContext instances

spencergibb avatar Sep 08 '16 20:09 spencergibb

@dahamr what are your memory settings? how long until the OOM error? Each instance is just over 100b.

spencergibb avatar Sep 08 '16 21:09 spencergibb

Agreed. Its pretty small even when you take into account what visualvm reports as the retained space. The services that failed first were basically running with about 30-40M free normally but would OOM in half a day with very little load. Turned out the reason the small memory leak turned into a problem was way too many unnecessary refresh events. I didn't really know how X-CONSUL-INDEX worked until this surfaced. We did not create service specific prefixes for every service. This caused those particular services to see every update to consul in any KV under the config prefix. These services also had the smallest heapsize so they ate dirt first.

We moved back to spring boot 1.3.5 for now but also plan to add a prefix for every service to prevent the unnecessary refreshes.

dahamr avatar Sep 08 '16 21:09 dahamr

Wait, moving to boot 1.3.5 fixes the issue?

spencergibb avatar Sep 08 '16 21:09 spencergibb

@dsyer @philwebb When I moved the sample above to boot 1.3.5 I get the familiar sawtooth heap graph. With boot 1.3.6 I see steady heap growth. What changed then that would cause the difference. With many refreshes, we create new contexts with ContextRefresher (same version with boot 1.3.5 and 1.3.6).

spencergibb avatar Sep 08 '16 21:09 spencergibb

Ha! I prefer "1.3.5 did not show the same symptom". Definitely both changes together have improved the stability of our environment.

dahamr avatar Sep 08 '16 21:09 dahamr