Understanding Memory Allocation List!

majomamajoma DEUniversity ✭✭

Hi all,

since yesterday I got the Xamarin Profiler running within my Visual Studio 2013. I’m using the Xamarin Profiler 0.18 with Xamarin 3.11.666.0
I’m working on a Xamarin Android Project where I want to do some quality improvements by checking for memory leaks and performance issues.

Yes ,I read through the manuals on the xamarin webpage:
https://developer.xamarin.com/guides/cross-platform/deployment,testing,_and_metrics/xamarin-profiler/
https://developer.xamarin.com/guides/cross-platform/deployment,_testing,_and_metrics/xamarin-profiler/walkthrough
-_using_the_xamarin_profiler/
http://developer.xamarin.com/guides/cross-platform/deployment,_testing,_and_metrics/memory_perf_best_practices/
and I have searched through a lot of forums.

The problem I currently investigate is that I have a custom made Dialog derived from DialogFragment, which probably causes a memory leak, but perhaps I just do wrong interpretation of the Xamarin Profiler data.

Here my questions about the Xamarin Profiler (for better understanding I have added 2 screen shots)

**Allocation List: **

a) What is the meaning of the “Live” column?
Sometimes there is a dot in the cell, which probably means alive and sometimes the cell is empty.
But what means “Live” at the end?
Sometimes this dot comes up without a reason at later point in time, when the dialog related to this row was already closed for a long time.

b) When will new allocation rows be added?
When I open my Dialog I expected that immediately after the Dialog opens a new row will be added in the allocation list.
This does mostly not happen, also if I wait for a long time. Mostly a new row is just added after closing the dialog and sometimes just when I close it and reopen it again.
In that case two new entries (rows) will be added at once (the previous and the actual one).

c) The “allocation list” rows for my dialog fragments never disappear.
I expected that the gc will clean up all the dialog fragment instances which were dismissed and not referenced anymore from time to time, but they never disappear.
In the 1st screen shot you can see that this Dialog Instance is still referenced from my NavDrawer-Activity.
All the other rows have no references anymore (because already dismissed) as shown in the 2nd screen shot.
So from Xamarin Profiler it looks like these dialogs are causing a memory leak, because they will never be cleaned up. Is that right?
I also tried to force the clean up by calling GC.Collect(); but they do not disappear.

Note:
I have searched through a lot of forums regarding DialogFragment’s causing memory leaks, but I think my implementation regarding .show(), dismiss() and so on is O. K.
So I could not find a reason how they could cause a memory leak.
I’m using the following support library: package id="Xamarin.Android.Support.v4" version="22.2.0.0" targetFramework="MonoAndroid44"

I will be very thankful for a view hints or advices.

Thanks in Advance and Best Regards
Marc

Best Answer

  • RodrigoMoyaRodrigoMoya ES Xamurai
    Accepted Answer

    So:

    a) 'Live' means the object was in the last snapshot you took. We only have information about an object being alive or not on snapshots, so maybe we should hide that column if we don't. So, answering your question, you should take a snapshot once you close that dialog, and thus, the allocation for that should display as 'dead' (so no dot).

    b) We display the information when we get them from the runtime. That said, I've noticed on Android the runtime reports data only after some events (like snapshots being taken, etc), not continously like it does for iOS, so that might be the reason.

    c) Allocations list view displays all the allocations that ever happened in your application, and, as I said for a), 'live' means the object was in the last snapshot taken, so if you never take another snapshot, the object will always display as 'live', since we don't know if it's dead or not until we take another snapshot. It doesn't matter if you call GC.Collect in your code, the object might have been GC'ed, but the xamarin profiler doesn't know anything about that, until you take another snapshot.

    Hope this answers your questions.

Answers

  • majomamajoma DEUniversity ✭✭

    Hi again,

    has really nobody an idea or a hint for me?
    At least what's the meaning of the "Live" column or why the allocation rows never disappear although they are not referenced anymore.

    Some helping comments will be great.

    Thanks in Advance and Best Regards
    Marc

  • RodrigoMoyaRodrigoMoya ESXamarin Team Xamurai
    Accepted Answer

    So:

    a) 'Live' means the object was in the last snapshot you took. We only have information about an object being alive or not on snapshots, so maybe we should hide that column if we don't. So, answering your question, you should take a snapshot once you close that dialog, and thus, the allocation for that should display as 'dead' (so no dot).

    b) We display the information when we get them from the runtime. That said, I've noticed on Android the runtime reports data only after some events (like snapshots being taken, etc), not continously like it does for iOS, so that might be the reason.

    c) Allocations list view displays all the allocations that ever happened in your application, and, as I said for a), 'live' means the object was in the last snapshot taken, so if you never take another snapshot, the object will always display as 'live', since we don't know if it's dead or not until we take another snapshot. It doesn't matter if you call GC.Collect in your code, the object might have been GC'ed, but the xamarin profiler doesn't know anything about that, until you take another snapshot.

    Hope this answers your questions.

  • majomamajoma DEUniversity ✭✭
    edited July 2015

    Hello Rodrigo,

    thanks very much. That is very useful information to me :-)
    I think it would be helpful for all users, if you can add it to your Xamarin Profiler manual webpage some point in time.

    I did not know at all that there is a relation between the 'Live' column and taking snapshots.
    So as far as I understood from your explanation, entries will never disappear from allocation list, just the dot goes away and that's the information to me that the object is cleaned up = no memory leak.

    I have two more questions related the theme 'Taking Snapshots':

    d) The Camera Icon in the menu is always disabled. I assume it is there for manually taking a snap shot by clicking it. Is there a way to enable it, or is this feature not implemented up to now?

    e) The cyclic Auto Snapshot under Options does not work like I expect. When I adjust 'Every 1 seconds' I expect a snapshot within that timeframe. Is it not implemented or is my expectation wrong? With that adjustment I just get snapshots very rarely, probably caused by some other events.

    'Take SnapShot when GC occurs' works, so I used this option for my tests and added a GC.Collect() call within my C# code which I execute by button click. By doing this I can force disappearing dots in the Live column for obsolete objects which were not referenced anymore.

    I will be very happy for clarification of this two things.

    Thanks in Advance and Best Regards
    Marc

  • RodrigoMoyaRodrigoMoya ESXamarin Team Xamurai

    @MarcMaurer

    d) yes, it's for manually taking snapshots, so it should be enabled when live profiling (not when reading a static MLPD). So, under which circumstances is it disabled for you? Are you live profiling? What kind of app? If it shows disabled for you when live profiling, it's a bug.

    e) yes, automatic snapshots don't work as expected in the runtime. In some cases, they work great, but in others, as your case, just take snapshots randomly. We are working with the runtime team to improve the situation, and in fact we thought about disabling it in the UI until it works, but since it works ok in some cases, and we're still a preview for the profiler, we thought it'd be better to leave it. But if it gives too many headaches to users, we might disable that until it works reliably.

  • majomamajoma DEUniversity ✭✭
    edited July 2015

    Hello Rodrigo,

    d) I'm doing just live profiling. I have not used MLPD stuff up to now, so I think it is a bug. Icon is always disabled. I Never saw the icon enabled. I have an Android App build for Android >= 4.4 and a Google Nexus 5 mobile running Android 5.1.1. My App uses Android Support Library V4. Today I have installed Xamarin Profiler 0.19. Now the camera icon changed to a dot but it is still disabled (see screenshot)

    e) For me it is fine if you leave it enabled in the UI. At least the "Take Snapshot when GC occurs" is very useful for me.

    By the way it is great, that the time profiler now shows the correct "Running Time" :-)

    Thanks and Best Regards
    Marc

  • RodrigoMoyaRodrigoMoya ESXamarin Team Xamurai

    @MarcMaurer can you then please file a bug to bugzilla.xamarin.com for d), so that it doesn't get lost?

    About e), yes, that's why we didn't disable it, as timed snapshots work on some scenarios, and GC-based snapshots work and, as you said, are very useful to find leaks.

  • majomamajoma DEUniversity ✭✭

    I have added it to bugzilla.xamarin.com under https://bugzilla.xamarin.com/show_bug.cgi?id=32659

    Thanks and have a nice weekend
    Marc

  • SoftSavageSoftSavage GBXamarin Team Xamurai

    Hi Marc,
    Do you have autosnap shots ticked in the Tools->Options->Snapshots screen? If so, then this is by design the camera icon will not be enabled when autosnap shot is on.

    Dominique.

  • DougJonesDougJones USMember, University

    Hi Dominique,

    I am experiencing the same issue as the original poster with the Windows version 0.20. Un-ticking the "Auto Snapshots" option does not enable the "Record" button and the profiler still performs periodic snapshots on it's own. I have never seen the button enabled with any configuration I have tried. I updated the bug to include a couple of snapshots of the behavior. Let me know if I can provide any additional information.

    Doug

  • CaioshinCaioshin ITMember ✭✭

    Hello,I'm currenty using the profiler but I have some doubt.
    I just want to evaluate how much memory my app is using on the device, I see that under the statistics panel the memory allocated value continue to grow, is it the total memory currently allocated or is it the memory allocated from the moment when I lauch it?
    And the "working set" and "virtual bytes" values, what mean?

    thank you

  • RodrigoMoyaRodrigoMoya ESXamarin Team Xamurai

    The memory allocated value reflects the total memory allocated, since the beginning of the app, unless you filter, in which case, it reflects the total of memory allocated by the allocations filtered.

    About Working set and virtual bytes, here's, I think, a good explanation -> http://getgreenshot.org/2010/07/24/a-few-words-on-memory-usage-or-working-set-vs-private-working-set/

Sign In or Register to comment.