ConcurrentLinkedQueue memory leak

157 views
Skip to first unread message

kyrylo.ko...@teamdev.com

unread,
Jul 27, 2016, 7:33:21 AM7/27/16
to j2objc-discuss
Hello,
I have in sources the code like this one:

public void testQueue() {
Queue<String> q = new ConcurrentLinkedQueue<String> ();
String t;
while (true)
{
t = q.poll();
if (t != null)
{
System.out.print(t);
}
t = null;
}
}

Long term running loop with regular calling ConcurrentLinkedQueue.poll method.

In objective-c this code very fast allocate to much memory, more then 640Mb and iOS closes application. I've found that the similar problem was already fixed https://github.com/google/j2objc/issues/609 . Could some one check this example and tell me if the problem is in my code or this is a j2objc problem. I've tried j2objc-1.0.1 and the latest one 1.1 versions.

Thanks.

Lukhnos Liu

unread,
Jul 27, 2016, 1:35:07 PM7/27/16
to j2objc-discuss, kyrylo.ko...@teamdev.com
You'll need to find a way to drain the autorelease pool every now and then. According to Apple's doc [1]:

If your application or thread is long-lived and potentially generates a lot of autoreleased objects, you should periodically drain and create autorelease pools (like the Application Kit does on the main thread); otherwise, autoreleased objects accumulate and your memory footprint grows.

One possible solution would be to extract the poll-and-consume code:

  while (true) {
     pollAndConsume(q);
  }

And the actual method would be like:

  // import com.google.j2objc.annotations.AutoreleasePool;

  @AutoreleasePool
  public static void pollAndConsume(Queue<String> q) {
    String t = q.poll();
    if (t != null)  {
      System.out.print(t);
    }
  }

kyrylo.ko...@teamdev.com

unread,
Aug 3, 2016, 10:22:10 AM8/3/16
to j2objc-discuss, kyrylo.ko...@teamdev.com
Thanks, it's really helped me.
Reply all
Reply to author
Forward
0 new messages