0

I used a while loop to fetch message from Amazon SQS. Partial code is as follows:

ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl);
while (true) {
    List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();

     if (messages.size() > 0) {

             MemcachedClient c = new MemcachedClient(new BinaryConnectionFactory(), AddrUtil.getAddresses(memAddress));

             for (Message message : messages) {

                 // get message from aws sqs
                 String messageid = message.getBody();
                 String messageRecieptHandle = message.getReceiptHandle();
                 sqs.deleteMessage(new DeleteMessageRequest(myQueueUrl, messageRecieptHandle));

                 // get details info from memcache 
                 String result = null;
                 String key = null;
                 key = "message-"+messageid;
                 result = c.get(key);

             }
             c.shutdown();
          }
       }

Will it cause memory leak in such case? I checked using "ps aux". What I found is that the RSS (resident set size, the non-swapped physical memory that a task used) is growing slowly.

7
  • You are defining the variables in the loop. Move your definitions above the loop and reuse/reassign in the loop. Commented Jul 27, 2011 at 16:09
  • it just increase the overhead, they are using the same pointer, aren't they? Commented Jul 27, 2011 at 16:11
  • 1
    @D.N.: That shouldn't make a significant difference. The compiler is smart enough to allocate the necessary locals on the stack only once. Commented Jul 27, 2011 at 16:11
  • 2
    @D.N.: that could only worsen memory usage. If you keep the variables in the narrowest scope possible you always have the best chance of the memory falling out of scope when no longer needed. Commented Jul 27, 2011 at 16:13
  • are you getting an OOM exception? The JVM freely decides how much of the memory it uses before doing a GC. Commented Jul 27, 2011 at 16:13

2 Answers 2

2

You can't evaluate whether your Java application has a memory leak simply based on the RSS of the process. Most JVMs are pretty greedy, they would rather take more memory from the OS than spend a lot of work on Garbage Collection.

That said your while loop doesn't seem like it has any obvious memory "leaks" either, but that depends on what some of the method calls do (which isn't included above). If you are storing things in static variables, that can be a cause of concern but if the only references are within the scope of the loop you're probably fine.

The simplest way to know if you have a memory leak in a certain area of code is to rigorously exercise that code within a single run of your application (potentially set with a relatively low maximum heap size). If you get an OutOfMemoryError, you probably have a memory leak.

Sign up to request clarification or add additional context in comments.

Comments

1

Sorry, but I don't see here code to remove message from the message queue. Did you clean the message list? In case that DeleteRequest removes message from the queue then you try to modify message list which you itereate.

Also you can get better memory usage statistic with visualvm tool which is part of JDK now.

4 Comments

References to the objects should be removed for each iteration. So the list size should not cause the problem
I do not clean List<Message> messages. I get a message from the list, then delete the message from SQS. But I do not touch List<Message> messages. In the next iteration, List<Message> messages get a list of new messages from SQS. Is it a problem?
@chnet: I wonder if your SQS cause problem. messages should be another references pointing to the objects received from SQS. It should be ok
@chnet: YOu can dump out the size of the queue, such that you know if there is leak or not

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.