openGeeksLab / codenameone

Automatically exported from code.google.com/p/codenameone
0 stars 0 forks source link

GC does not seem to work any more with the iOS newVM #1323

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I tried running the following code with the iOS new VM:

public void runTest() {
    while (true) {
        List<Node> list = new ArrayList<Node>();
        for (int i = 0; i < 100; i++)
            list.add(new Node());
        try {
            Thread.sleep(10);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}

public static class Node {
    public int[] eatMem = new int[2000];
}

It runs for a few seconds and then crashes with BAD_ACCESS, apparently because 
it is out of memory, as shown by the attached screenshot (NewVM.tiff) of the 
memory profiler of Xcode.

With the oldVM, it runs without crashing indefinitely, and memory usage remains 
stable (see screenshot OldVM.tiff).

I tried to change sleep(10) to sleep(100) to give more time to the GC, but it 
does not change anything on the new VM (the app just takes several minutes 
instead of a few seconds to eat all memory and crash).

More generally, I've observed that memory usage seems to be almost constantly 
increasing with all my apps when the new VM is used.

Original issue reported on code.google.com by tachyon....@gmail.com on 29 Jan 2015 at 2:26

Attachments:

GoogleCodeExporter commented 8 years ago
The code above never releases the objects so its pretty obvious it should crash.
If you fix it to something like this it will work:
    while (true) {
        List<Node> list = new ArrayList<Node>();
        for (int i = 0; i < 100; i++) {
            list.add(new Node());
        }
        list.clear();
        try {
            Thread.sleep(10);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }

The old VM is slightly more efficient in RAM object storage but if it does run 
indefinitely this could be a bug there since this code MUST crash... E.g when 
list.size() exceeds RAM.

Original comment by shai.almog on 29 Jan 2015 at 2:58

GoogleCodeExporter commented 8 years ago
Hi

I do not agree with your answer. A new ArrayList is created and assigned 
to the 'list' variable at every iteration, meaning that the ArrayLists 
from previous iterations are no longer live and should be garbage collected.

I tested and it is true that the code works by adding a list.clear(), 
but it should also work without it: The loop is adding items
to a *different* new ArrayList at every iterations of the while loop, 
and ArrayList instances from previous iterations are not referenced and 
are hence garbage.

Nicolas

Le 1/29/2015 3:58 PM, codenameone@googlecode.com a écrit :

Original comment by tachyon....@gmail.com on 29 Jan 2015 at 3:31

GoogleCodeExporter commented 8 years ago
I missed that. Does this still happen in the current update, we made some GC 
fixes today.

Original comment by shai.almog on 29 Jan 2015 at 3:49

GoogleCodeExporter commented 8 years ago
I tried again today, and the problem has gone.
Thanks.

Le 1/29/2015 4:49 PM, codenameone@googlecode.com a écrit :

Original comment by tachyon....@gmail.com on 30 Jan 2015 at 4:10

GoogleCodeExporter commented 8 years ago

Original comment by shai.almog on 30 Jan 2015 at 7:20