Wildfly 10.1.0.FINAL上的内存泄漏(java.lang.ref.Finalizer / ActiveMQConnection)

时间:2017-08-31 10:13:27

标签: java memory-leaks finalizer finalize activemq-artemis

我们有一个java Web应用程序,它通过JMS发送(JobsController.java)并接收消息(JMSMessageListener.java)。在恒定负载下运行应用程序24小时并进行堆转储后,我观察到应用程序在处于空闲状态时不会释放的内存使用量不断增加。我知道这会导致java堆内存不足问题。

JobsController是一个ejb无状态bean,每次调用后它的资源都会被正确销毁。 JMSMessageListener由ejb全局bean池处理,其实例被重用。

我可以从java堆转储中看到的嫌疑人

  1. EJB bean注入导致内存泄漏 https://blog.akquinet.de/2017/01/04/dont-get-trapped-into-a-memory-leak-using-cdi-instance-injection/
  2. ActiveMQConnection.finalize()。如果它是罪魁祸首而不是必须的 发生在所有那些wildfly activemq部署中。任何提示都是 赞赏。
  3. ActiveMQConnection.java

    @Override  
    protected final void finalize() throws Throwable {  
       if (!closed) {  
           if (this.factoryReference.isFinalizeChecks()) {  
               ActiveMQJMSClientLogger.LOGGER.connectionLeftOpen(creationStack);  
           }  
           close();  
    }
    

    enter image description here enter image description here enter image description here

    JobsController

    @Stateless
    公共类JobsController {

    @Inject  
    private JMSContext jmsContext;  
    private Connection connection;  
    private Session session;  
    private MessageProducer jmsProducer;  
    
    @Resource(lookup = "java:/ConnectionFactory")  
    private ConnectionFactory connectionFactory;  
    
    @Resource(lookup = JAVA_JMS_JOB_QUEUE)  
    private Queue jobQueue;  
    
    @Resource(lookup = JAVA_JMS_QUEUE)  
    private Queue progressQueue;  
    
    @PreDestroy  
    void release() {  
        try {  
            if (jmsProducer != null) {  
                jmsProducer.close();  
            }  
            if (session != null) {  
                session.close();  
            }  
            if (jmsContext != null) {  
                jmsContext.close();  
            }  
            if (connection !=null) {  
                connection.close();  
            }  
        } catch (JMSException e) {  
            LOG.warn("failed to close JMS resources: {}", e.getMessage());  
        }  
    }  
    
    public synchronized MessageProducer getJmsProducer() {  
        if (jmsProducer == null) {  
            try {  
                connection = connectionFactory.createConnection();  
                session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);  
                jmsProducer = session.createProducer(jobQueue);  
    
    
                connection.start();  
            } catch (JMSException e) {  
                LOG.error("failed to setup JMS message producer: {}", e.getMessage());  
            }  
        }  
        return jmsProducer;  
    }
    public void addMessageToProgressQueue(ProgressMessage progressMessage) {
        ObjectMessage objectMessage = jmsContext.createObjectMessage(progressMessage);
        try {
            getJmsProducer().send(progressQueue, objectMessage);
        } catch (JMSException e) {
            LOG.error("failed to send progress message {}: {}", objectMessage, e.getMessage());
        }
    }  
    

    }

    JMSMessageListener.java

    @MessageDriven(name = "JMSMessageListener", mappedName = JAVA_JMS_QUEUE, activationConfig = {  
            @ActivationConfigProperty(  
                    propertyName = "acknowledgeMode",  
                    propertyValue = "Auto-acknowledge"),  
            @ActivationConfigProperty(  
                    propertyName = "destinationType",  
                    propertyValue = "javax.jms.Queue"),  
            @ActivationConfigProperty(  
                    propertyName = "destination",  
                    propertyValue = JAVA_JMS_QUEUE)  
    
    
    })  
    public class JMSMessageListener implements MessageListener {  
    
        private static Logger LOG = LoggerFactory.getLogger(JMSMessageListener.class);  
    
        @EJB  
        private JobsController jobsController;  
    
        private final ObjectMapper progressMessageMapper;  
    
        public JMSMessageListener() {  
            progressMessageMapper = new ObjectMapper();  
            progressMessageMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);  
        }  
    
        @Override  
        public void onMessage(Message message) {  
            ProgressMessage progressMessage = null;  
            try {  
                if (message instanceof BytesMessage) {  
                    BytesMessage bytesMessage = (BytesMessage) message;  
                    int TEXT_LENGTH = new Long(bytesMessage.getBodyLength()).intValue();  
                    byte[] textBytes = new byte[TEXT_LENGTH];  
                    bytesMessage.readBytes(textBytes, TEXT_LENGTH);  
    
    
                    String progressText = new String(textBytes, "UTF-8");  
    
                    progressText = progressText.replaceAll("'totalSteps': None", "'totalSteps': 0");  
                    progressMessage = progressMessageMapper.readValue(progressText, ProgressMessage.class);  
                } else if (message instanceof ObjectMessage) {  
                    progressMessage = message.getBody(ProgressMessage.class);  
                }  
                if (progressMessage != null) {  
    
                    jobsController.sendProgressMessage(progressMessage);  
                } else {  
                    LOG.error("An empty progress message was received");  
                }  
            } catch (JMSException | IOException e) {  
                LOG.error("failed to process progress message: {}", e.getMessage(), e);  
            }  
        }  
    }
    

1 个答案:

答案 0 :(得分:1)

一些事情:

  • 您正在注入JMSContext但从不使用它(至少在您粘贴的代码中)。这似乎是一个错误。
  • 如果您不打算使用注入的JMSContext而是使用注入的ConnectionFactory,那么您应该注入“java:/ JmsXA”而不是“java:/ ConnectionFactory”,因为它是& tl; pooled-connection -factory取代。为使用“java:/ ConnectionFactory”发送的每条消息创建连接是一种反模式,因为它没有被合并。另外,我假设您要使用XA事务,以便消息消耗和MDB中的发送是原子的,并且不适用于“java:/ ConnectionFactory”。