2015-08-20 23 views
10

Şu anda oldukça büyük bir WPF tabanlı Windows uygulaması oluşturmak için şu anda ReactiveUI kullanıyoruz. Her şey iyi gidiyordu, bizim uygulamamızın büyük miktarlarda bellek tüketiyor olduğunu keşfedene kadar ... temel olarak tüm görüşlerimiz, görüş model ve modellerimiz çöp toplanmıyordu. Jet Brains dotMemory gibi bellek profilleyicilerinden alınan bilgilere dayanarak, ReactiveUI birincil suçlu gibi görünmektedir. Özellikle, en iyi uygulamaları kullansak ve görünüm devre dışı bırakıldığında tüm bağların bertaraf edilmesini sağladığımız halde, görüşlerimizde yapılandırdığımız ReactiveUI bağlamalarıdır.ReactiveUI bağlantıları, çöp toplama işleminin oluşmasını engelliyor gibi görünüyor.

Aşağıdaki, oluşturduğumuz görünümlerden birinin bir örneğidir. Nerede yanlış gidebileceğimize dair herhangi bir düşünce çok takdir edilecektir. o sızıntı nereden geldiğini söylemek zor sorudan

public partial class RunbookInputsView : IViewFor<RunbookInputsViewModel> 
{ 
    public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(
     "ViewModel", typeof(RunbookInputsViewModel), typeof(RunbookInputsView)); 

    public RunbookInputsView() 
    { 
     InitializeComponent(); 

     this.WhenActivated(d => 
     { 
      d(this.OneWayBind(ViewModel, vm => vm.AddInput, v => v.AddInput.Command));     
      d(this.OneWayBind(ViewModel, vm => vm.Inputs, v => v.Inputs.ItemsSource)); 
     }); 
    } 

    object IViewFor.ViewModel 
    { 
     get { return ViewModel; } 
     set { ViewModel = (RunbookInputsViewModel)value; } 
    } 

    public RunbookInputsViewModel ViewModel 
    { 
     get { return (RunbookInputsViewModel) GetValue(ViewModelProperty); } 
     set { SetValue(ViewModelProperty, value); } 
    } 
} 
+1

Eğer devre dışı bırakma mantığı yürüterek teyit mı 'd eklemeyi deneyin (Disposable.Create (() => System.Diagnostics.Debug.WriteLine ("DEACTIVATED"))) '' WhenActivated 'öğelerinizden birine –

+0

arası kesme noktalarını kullanarak, reaktif bağların doğru şekilde atıldığını görürsünüz. –

+0

Araştırmam, şimdi CreateDerivedCollection ve türetilmiş koleksiyon için yeni öğeleri seçmek için kullanılan lambda kullanımına odaklanıyor. Lambda sınıfımın üyelerine başvurduğunda hafıza sorunlarım var gibi görünüyor. Aşağıdaki görünüyor Tamam: 'source.CreateDerivedCollection (x => ViewModel (x));' Ama bu sorunlar var gibi görünüyor: 'source.CreateDerivedCollection (x => ViewModel (x, this.Something)); –

cevap

1

. Kaçak sonra, bir süre gerçekleşmesi windbg (Debugging Tools For Windows parçası) ile işleme ekleme yapalım (Not:. Eğer işe yaraması için x86 veya x64 inşa etmek gerekebilir) Eğer ekledikten sonra

kurmak komutları girerek .net hata ayıklama için:

.symfix 
sxe clr 
sxd av 
.loadby sos clr 

Daha sonra her tür bellek kullanımını almak için !dumpheap -stat kullanabilirsiniz. Bu aşağıdaki biçimde çıktı üretir: (Sınıf adları ve okunabilirliği için liste kesildi.)

0:012> !dumpheap -stat 
Statistics: 
       MT Count TotalSize Class Name 
000007fefa55d2e8  1   24 System.[...]TransportSinkProvider 
000007fefa55ce08  1   24 System.Run[...]rtSinkProvider 
000007fee7c32df0  1   24 System.LocalDataStoreHolder 
000007fee7c2ff78  1   24 System.Colle[...] 
000007fee7c2ece0  1   24 System.Resources.FastResourceComparer 
000007fee7c2ead0  1   24 System.Resources.ManifestBasedResourceGroveler 
000007fee7c2ea70  1   24 System.[...]eManagerMediator 
000007fee4cc1b70  4   1216 System.Xml.XmlDocument 

Bellek sızan nesneleri göreceksiniz budur sızıntı varsa. (Çok fazla olmalı.) Sızan şeyin ne olduğunu belirledikten sonra, gerçek nesnelerin bir listesini almak için !dumpheap -type yapabilirsiniz. (Bu örnek için ben türü adı küçük harfe duyarlıdır. System.Xml.XmlDocument kullanacağız ve tam olmalıdır.)

0:012> !dumpheap -type System.Xml.XmlDocument 
     Address    MT  Size 
0000000002af9050 000007fee4cc1b70  304  
0000000002afa628 000007fee4cc1b70  304  
0000000002b0ea30 000007fee4cc1b70  304  
00000000037e2780 000007fee4cc1b70  304  

Statistics: 
       MT Count TotalSize Class Name 
000007fee4cc1b70  4   1216 System.Xml.XmlDocument 

Listeniz muhtemelen çok daha büyük olacak, ama olasılık diyor sızan herhangi rastgele örneği tip ilgilendiğiniz bir şey olacak biz bu adreslere birinde bir !do yaparsanız, biz şuna benzer çıktıyı alırsınız.

0:012> !do 2af9050 
Name:  System.Xml.XmlDocument 
MethodTable: 000007fee4cc1b70 
EEClass:  000007fee4ae7f00 
Size:  304(0x130) bytes 
File:  C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll 
Fields: 
       MT Field Offset     Type VT  Attr   Value Name 
000007fee4cc2b40 40004fc  8 System.Xml.XmlNode 0 instance 0000000000000000 parentNode 
000007fee4cc2258 400050a  10 ...XmlImplementation 0 instance 0000000002af9180 implementation 
000007fee4cc22f0 400050b  18 ....Xml.DomNameTable 0 instance 0000000002af92e0 domNameTable 
[Entries removed for clarity] 
000007fee4cc26f0 400052f  108 ...m.Xml.XmlResolver 0 instance 0000000000000000 resolver 
000007fee7c18c48 4000530  126  System.Boolean 1 instance    0 bSetResolver 
000007fee7c113e8 4000531  110  System.Object 0 instance 0000000002af9788 objLock 
000007fee4cc11b0 4000532  118 ....Xml.XmlAttribute 0 instance 0000000000000000 namespaceXml 

sen listelenen nesnelerin herhangi biriyle !do kullanabilirsiniz Daha fazla bilgi için tablo. System.String ve System.Boolean gibi türler gerçek değerlerini tükürecektir. Oluşturulduğu nesneden net değilse, bir sonraki adım, nesnelerimize yapılan başvuruları bulmak için !gcroot -nostacks'u kullanmak olabilir.

Oldukça fazla sayıda komut var ve bu zaten çok uzun. !help komutu güzel bir giriş sağlar. (Bunlardan herhangi kullanmak için, ! ile komut öneki gerekiyordu !help [command] Belirli bir komut hakkında ayrıntılı bilgi veren örnek !help dumpobj için:..?

0:012> !help dumpobj 
------------------------------------------------------------------------------- 
!DumpObj [-nofields] <object address> 

This command allows you to examine the fields of an object, as well as learn 
important properties of the object such as the EEClass, the MethodTable, and 
the size. 

You might find an object pointer by running !DumpStackObjects and choosing 
from the resultant list. Here is a simple object: 

    0:000> !DumpObj a79d40 
    Name: Customer 
    MethodTable: 009038ec 
    EEClass: 03ee1b84 
    Size: 20(0x14) bytes 
    (C:\pub\unittest.exe) 
    Fields: 
      MT Field Offset     Type VT  Attr Value Name 
    009038ec 4000008  4    Customer 0 instance 00a79ce4 name 
    009038ec 4000009  8     Bank 0 instance 00a79d2c bank 

Note that fields of type Customer and Bank are themselves objects, and you can 
run !DumpObj on them too. You could look at the field directly in memory using 
the offset given. "dd a79d40+8 l1" would allow you to look at the bank field 
directly. Be careful about using this to set memory breakpoints, since objects 
can move around in the garbage collected heap. 

What else can you do with an object? You might run !GCRoot, to determine what 
roots are keeping it alive. Or you can find all objects of that type with 
"!DumpHeap -type Customer". 

The column VT contains the value 1 if the field is a valuetype structure, and 
0 if the field contains a pointer to another object. For valuetypes, you can 
take the MethodTable pointer in the MT column, and the Value and pass them to 
the command !DumpVC. 

The abbreviation !do can be used for brevity. 

The arguments in detail: 
-nofields:  do not print fields of the object, useful for objects like 
        String