質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
UWP

UWPは、Universal Windows Platformの略。様々なデバイス向けに提供されているアプリケーションを共通のフレームワーク上で動作可能にする仕組みで、Windows10で導入されました。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

メモリリーク

メモリリークは、プログラムファイルがメモリの解放に失敗した時に起こります。

Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

Q&A

解決済

1回答

1503閲覧

【Uno Platform】(Android)NativePivotPresenterのメモリ解放について

net-ohkubo

総合スコア7

UWP

UWPは、Universal Windows Platformの略。様々なデバイス向けに提供されているアプリケーションを共通のフレームワーク上で動作可能にする仕組みで、Windows10で導入されました。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

メモリリーク

メモリリークは、プログラムファイルがメモリの解放に失敗した時に起こります。

Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

0グッド

0クリップ

投稿2020/11/17 10:16

知りたい事
NativePivotPresenterでのメモリ解放について

環境説明
Visual Studio 2019で、Uno Platformを用いてAndroid開発を行っております。

画面のUIに、NativePivotPresenter(Uno Platform独自のUI)
を利用しているのですが、上記を利用している画面については
画面の操作を行うごとにメモリ使用量が増加し、画面遷移後にも
メモリが解放されずにメモリリークを起こす状態となっています。

該当部位を、通常のPivotへ部品を変更すると、
画面遷移後に正しくデストラクタが呼ばれ、その後
ガベージコレクションによりメモリ解放される動きをしております。
以下、NativePivotPresenterを利用した実装箇所の抜粋です。

app.xaml.cs

C#

1new Setter<NativePivotPresenter>("Template", pb => pb 2 .Template = new Windows.UI.Xaml.Controls.ControlTemplate(() => 3 new Windows.UI.Xaml.Controls.Grid 4 { 5 RowDefinitions = 6 { 7 new Windows.UI.Xaml.Controls.RowDefinition(){ Height = Windows.UI.Xaml.GridLength.Auto}, 8 new Windows.UI.Xaml.Controls.RowDefinition(){ Height = new Windows.UI.Xaml.GridLength(1, Windows.UI.Xaml.GridUnitType.Star)}, 9 }, 10 11 Children = 12 { 13 // Header 14 new Border 15 { 16 Child = new Uno.UI.Controls.SlidingTabLayout(ContextHelper.Current) 17 { 18 LayoutParameters = new Android.Views.ViewGroup.LayoutParams(Android.Views.ViewGroup.LayoutParams.MatchParent, Android.Views.ViewGroup.LayoutParams.WrapContent), 19 }, 20 BorderThickness = new Windows.UI.Xaml.Thickness(0,0,0,1), 21 } 22 .Apply(b => b.SetBinding("Background", new Windows.UI.Xaml.Data.Binding { Path = "Background", RelativeSource = RelativeSource.TemplatedParent })) 23 .Apply(b => b.SetBinding("BorderBrush", new Windows.UI.Xaml.Data.Binding { Path = "BorderBrush", RelativeSource = RelativeSource.TemplatedParent })), 24 25 // Content 26 new ExtendedViewPager(ContextHelper.Current) 27 { 28 OffscreenPageLimit = 1, 29 PageMargin = (int)Android.Util.TypedValue.ApplyDimension(Android.Util.ComplexUnitType.Dip, 4, ContextHelper.Current.Resources.DisplayMetrics), 30 SwipeEnabled = true, 31 } 32 .Apply(v => Windows.UI.Xaml.Controls.Grid.SetRow(v, 1)) 33 } 34 }) 35)

menu.xaml

xaml

1<Page 2 x:Class="xxxxx.Menu" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:local="using:xxxxx.Display.Order" 6 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 7 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 8 mc:Ignorable="d android" 9 xmlns:android="http://uno.ui/android" 10 Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" 11 Name="MenuPage"> 12 <Grid Background="White" x:Name="MenuMainGrid" Loaded="Menu_Loaded"> 13 <StackPanel Grid.RowSpan="2" Name="titlePanel" Background="White"> 14 <Grid Height="45" VerticalAlignment="Center" Name="TitleGrid"> 15       (略) 16 </Grid> 17 <NativePivotPresenter x:Name ="Menu" Margin="0" Height="570" Background="White"> 18 <!--ここにコントロールが動的に追加される--> 19 </NativePivotPresenter> 20 </StackPanel> 21 </Grid> 22</Page>

menu.xaml.cs

C#

1// 画面表示 2private void setDisp() 3{ 4 { 5 // SQLiteで対象リスト listOfMenu を取ってくる(略) 6 () 7 8 var listOfMenu = queryMenu.ToList(); 9 for (int i = 0; i <= listOfMenu.Count() - 1; i++) 10 { 11 PivotItem MenuItem = new PivotItem(); 12 MenuItem.Height = 400; 13 MenuItem.Width = 380; 14 MenuItem.Margin = new Windows.UI.Xaml.Thickness(0); 15 MenuItem.Background = new SolidColorBrush(Colors.White); 16 MenuItem.Header = xxxx.Name; 17 18 // その他設定 19       (略) 20 } 21 MenuItem.Content = sv; 22 Menu.Items.Add(MenuItem);  23 } 24 } 25}

UnopatformのGitHubのソース(NativePivotPresenterPivot)を確認すると、
Pivotの場合はおそらくUWPのリソースのみを使用して作成していますが
NativePivotPresenterの場合、主にandroidのリソースを利用して
作成しており、この辺りで参照がMainActivity側から発生しているため
実行しているUWP上ではガベージコレクションの回収対象にならないのではないかと思います。

OS違いですがこちらのサイトも参考にし、
後述の試行錯誤の通りメモリ解放されるよう試みているのですが、
この参照を確実に切り、メモリ解放する方法が分からずに困っております。
同部品(NativePivotPresenter)を使用されたご経験がある方の
お知恵を拝借したいと思います。

以下、試行錯誤したのですが全てメモリ解放が見られなかった内容

・NativePivotPresenterの記載箇所を、Xaml側からC#側に記載しなおして試験
・画面遷移後にNativePivotPresenterを要素から明示的に削除
・NativePivotPresenter.Dispose()
・NativePivotPresenter.items.Clear()の後、null代入して参照切り
・NativePivotPresenter.items.Clear()の後、Dispose()し、null代入して参照切り
・弱参照に変更し、その後参照を削除
・NativePivotPresenterの親要素にGridを追加し、画面遷移後にGridの子要素をClear()
・Pivotを用意しておいてそれを削除し、NativePivotPresenterに置換

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

UWP上ではガベージコレクションの回収対象にならないのでは
との予想をしていましたが、NativePivotPresenterの親要素に
Gridを設定し、子要素を画面遷移後にリソース解放することで
無事にGCに回収されるようになりましたので、今後同部品利用される方へ共有です。

xaml

1<Page 2 x:Class="xxxxx.Menu" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:local="using:xxxxx.Display.Order" 6 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 7 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 8 mc:Ignorable="d android" 9 xmlns:android="http://uno.ui/android" 10 Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" 11 Name="MenuPage"> 12 <Grid Background="White" x:Name="MenuMainGrid" Loaded="Menu_Loaded"> 13 <StackPanel Grid.RowSpan="2" Name="titlePanel" Background="White"> 14 <Grid Height="45" VerticalAlignment="Center" Name="TitleGrid"> 15       (略) 16 </Grid> 17 <Grid x:NameName="PivotGrid"> ★追記★ 18 <NativePivotPresenter x:Name ="Menu" Margin="0" Height="570" Background="White"> 19 <!--ここにコントロールが動的に追加される--> 20 </NativePivotPresenter>  21 </Grid> ★追記★ 22 </StackPanel> 23 </Grid> 24</Page> 25

c#

1/// 別画面へ画面遷移後に以下を呼出しリソース解放を行う 2protected override void OnNavigatedFrom(Windows.UI.Xaml.Navigation.NavigationEventArgs e) 3{ 4 foreach (PivotItem pivotitem in pivotCategory.Items) 5 { 6 //ピボットアイテムのスタックパネルの取り出し 7 //子要素を解放する 8 StackPanel PivotStack = (StackPanel)pivotitem.Content; 9 PivotStack.Children.Clear(); 10 } 11 12 //NativePivotPresenter自体を画面から削除する 13 pivotCategory.Items.Clear(); 14 PivotGrid.Children.Clear(); 15 this.Frame.BackStack.Clear(); 16}

投稿2020/11/30 02:59

net-ohkubo

総合スコア7

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問