You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2021/11/03 07:26:15 UTC

[GitHub] [dubbo] wuwen5 edited a comment on pull request #8907: Nacos unsub support and RegistryDirectory destroy

wuwen5 edited a comment on pull request #8907:
URL: https://github.com/apache/dubbo/pull/8907#issuecomment-958703542


   NacosRegistry 存在一个严重的内存泄露问题,在dubbo admin中出现,经过分析发现被这个pr解决了,建议2.7.x尽早发布一个release版本.
   
   ```
   private void subscribeEventListener(String serviceName, final URL url, final NotifyListener listener)
               throws NacosException {
           EventListener eventListener = event -> {
               if (event instanceof NamingEvent) {
                   NamingEvent e = (NamingEvent) event;
                   List<Instance> instances = e.getInstances();
   
   
                   if (isServiceNamesWithCompatibleMode(url)) {
                       /**
                        * Get all instances with corresponding serviceNames to avoid instance overwrite and but with empty instance mentioned
                        * in https://github.com/apache/dubbo/issues/5885 and https://github.com/apache/dubbo/issues/5899
                        */
                       NacosInstanceManageUtil.initOrRefreshServiceInstanceList(serviceName, instances);
                       instances = NacosInstanceManageUtil.getAllCorrespondingServiceInstanceList(serviceName);
                   }
   
                   notifySubscriber(url, listener, instances);
               }
           };
           namingService.subscribe(serviceName,
                   getUrl().getParameter(GROUP_KEY, Constants.DEFAULT_GROUP),
                   eventListener);
       }
   ```
   ```EventListener eventListener```实例会无限制存储到 nacos ```InstancesChangeNotifier#listenerMap```的ConcurrentHashSet中
   
   ```
   public class InstancesChangeNotifier extends Subscriber<InstancesChangeEvent> {
       
       private final Map<String, ConcurrentHashSet<EventListener>> listenerMap = new ConcurrentHashMap<String, ConcurrentHashSet<EventListener>>();
       
       private final Object lock = new Object();
       
       /**
        * register listener.
        *
        * @param serviceName combineServiceName, such as 'xxx@@xxx'
        * @param clusters    clusters, concat by ','. such as 'xxx,yyy'
        * @param listener    custom listener
        */
       public void registerListener(String serviceName, String clusters, EventListener listener) {
           String key = ServiceInfo.getKey(serviceName, clusters);
           ConcurrentHashSet<EventListener> eventListeners = listenerMap.get(key);
           if (eventListeners == null) {
               synchronized (lock) {
                   eventListeners = listenerMap.get(key);
                   if (eventListeners == null) {
                       eventListeners = new ConcurrentHashSet<EventListener>();
                       listenerMap.put(key, eventListeners);
                   }
               }
           }
           eventListeners.add(listener);
       }
   ...
   }
   ```
   
   ![E1E5323A-BF00-4F1D-96D3-E2A07374244B](https://user-images.githubusercontent.com/5037807/140021563-46587435-41d5-40b8-b8b5-fc49306d1242.png)
   
   ![884E8D77-67AB-417E-ABC4-7873057DDC74](https://user-images.githubusercontent.com/5037807/140021627-36c1e46a-386d-43d1-be2d-de08377567a9.png)
   
   ```
   One instance of "com.alibaba.nacos.client.naming.event.InstancesChangeNotifier" loaded by "org.springframework.boot.loader.LaunchedURLClassLoader @ 0x80000000" occupies 1,828,304,624 (94.09%) bytes. The memory is accumulated in one instance of "java.util.concurrent.ConcurrentHashMap$Node[]" loaded by "<system class loader>".
   
   Keywords
   java.util.concurrent.ConcurrentHashMap$Node[]
   com.alibaba.nacos.client.naming.event.InstancesChangeNotifier
   org.springframework.boot.loader.LaunchedURLClassLoader @ 0x80000000
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org