Spring Boot默认是否有内存节省模式?Bean实例按需管理配置咨询
Hey there! Let's break down your two Spring-related questions clearly:
Spring Boot doesn’t ship with a dedicated "memory-saving mode" turned on out of the box. That said, it does come with sensible defaults that promote decent memory efficiency—things like using lightweight embedded servers (Tomcat, Jetty, Undertow) optimized for common use cases, and auto-configuring beans with reasonable scopes. But if you want aggressive RAM savings, you’ll need to tweak settings manually, like adjusting JVM heap parameters, using bean scopes strategically, or leveraging the dynamic bean features we’ll cover next.
Absolutely, Spring supports dynamic bean management to cut down on RAM usage—but this capability isn’t enabled by default. Here’s the lowdown:
- The simplest starting point is the
@Lazyannotation for singleton beans. This delays bean creation until the first time it’s actually accessed (instead of spinning it up at application startup). This works great for beans that aren’t used right away or only in specific code paths. - For more advanced control—like automatically destroying beans when they’ve been unused for a set period and recreating them later—you’ll need to combine scope proxies with custom logic or third-party extensions. For web apps,
requestorsessionscopes do this automatically (beans get destroyed when the request or session ends), but for general application-wide usage tracking, you’d need to implement a custom scope that monitors bean usage frequency and handles destruction/creation logic. - You can also hook into Spring’s lifecycle hooks using interfaces like
BeanFactoryPostProcessororSmartLifecycleto add custom logic for evicting unused beans as needed.
内容的提问来源于stack exchange,提问作者Tim Florian

