Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Xamarin.Android.Build.Tasks] should Dispose AssemblyDefinition #2148

Merged
merged 1 commit into from
Sep 6, 2018

Conversation

jonathanpeppers
Copy link
Member

Reviewing our codebase, we found a place where the BuildApk task was
calling an overload for MonoAndroidHelper.IsReferenceAssembly.

This presented two problems:

  1. It was using the InMemory option
  2. We were not calling Dispose()!

Case No.1 was bad, since we basically load every assembly into memory!

foreach (ITaskItem assembly in ResolvedUserAssemblies) {
    if (MonoAndroidHelper.IsReferenceAssembly (assembly.ItemSpec)) {
//...
foreach (ITaskItem assembly in ResolvedFrameworkAssemblies) {
    if (MonoAndroidHelper.IsReferenceAssembly (assembly.ItemSpec)) {

Likewise, we need to call Dispose here, since a lock could be held
on the file here on Windows.

The changes here made BuildApk slower (and more correct!), but the
overall build time better.

Before:

5890 ms  BuildApk                                   1 calls
Time Elapsed 00:01:02.59

After:

6377 ms  BuildApk                                   1 calls
Time Elapsed 00:00:59.37

I think the AssemblyDefinition instances must have been getting
cleaned up later in the build by the GC (if we were lucky), and also
used a lot more memory. This would contribute to overall build time.

Reviewing our codebase, we found a place where the `BuildApk` task was
calling an overload for `MonoAndroidHelper.IsReferenceAssembly`.

This presented two problems:
1. It was using the `InMemory` option
2. We were not calling `Dispose()`!

Case No.1 was bad, since we basically load every assembly into memory!

    foreach (ITaskItem assembly in ResolvedUserAssemblies) {
        if (MonoAndroidHelper.IsReferenceAssembly (assembly.ItemSpec)) {
    //...
    foreach (ITaskItem assembly in ResolvedFrameworkAssemblies) {
        if (MonoAndroidHelper.IsReferenceAssembly (assembly.ItemSpec)) {

Likewise, we need to call `Dispose` here, since a lock could be held
on the file here on Windows.

The changes here made `BuildApk` slower (and more correct!), but the
overall build time better.

Before:

    5890 ms  BuildApk                                   1 calls
    Time Elapsed 00:01:02.59

After:

    6377 ms  BuildApk                                   1 calls
    Time Elapsed 00:00:59.37

I think the `AssemblyDefinition` instances must have been getting
cleaned up later in the build by the GC (if we were lucky), and also
used *a lot* more memory. This would contribute to overall build time.
@jonathanpeppers
Copy link
Member Author

Logs here: AssemblyDefinitionDispose.zip

@jonpryor jonpryor merged commit fc6b7e9 into dotnet:master Sep 6, 2018
@jonathanpeppers jonathanpeppers deleted the isreferenceassembly branch September 10, 2018 13:48
@github-actions github-actions bot locked and limited conversation to collaborators Feb 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants