.NET

2016-03-22 31 views
0

İçindekiler için özel yordamlar belleği nasıl kullanır? Özel türün başlatılması, içindeki tüm aksesuarlar için başlatılıyor mu? Bunu istemcideki bellek performansı ve tüketimiyle ilgili olarak soruyorum. dot net olarak Ben.NET

public class myCustomType 
    { 
    public string prop1 {get;set;} 
    public string prop2 {get;set;} 
    public string prop3 {get;set;} 
    public string prop4 {get;set;} 
    } 

gibi özel tip bir şey yaratmak eğer zaman kodda başka bir yere ben belleğin

var parOfmyCustomType = new myCustomType(){ prop1 = "stingToHold" }; 
var listOfCustom = new list<myCustomType>(){ parOfmyCustomType }; 

regrading tüketilen yapın yapar ben myCustomType oluştururken bütün acı sahne için bellek kullanarak uygulama ve yalnızca bir tane

+0

ne alabilirim myCustomType mt = yeni myCustomType(); Debug.WriteLine (mt.prop1); ? – Paparazzi

cevap

2

setinin prop vale'sini boş bir get ile oluştururken; set; tanım sınıf özelliği aşağıdaki temel olarak eşittir:

public class myCustomType { 
    private string _prop1; 
    private string _prop2; 
    private string _prop3; 
    private string _prop4; 

    public string prop1 { 
    get { return _prop1; } 
    set { _prop1 = value; } 
    } 
    public string prop1 { 
    get { return _prop1; } 
    set { _prop1 = value; } 
    } 
    public string prop3 { 
    get { return _prop; } 
    set { _prop1 = value; } 
    } 
    public string prop4 { 
    get { return _prop4; } 
    set { _prop4 = value; } 
    } 
} 

destek dize alanları (_prop1 - _prop4) (C# tüm referans türleri gibi) null başlatılır. Bu nedenle, temel olarak, bu sınıfın veri kısmı, başlangıçta tümüyle null (4 * 32 bit veya 4 * 64 bit veya 16/32 bayt olacak şekilde, özel türde her örnek için) işaret eden dizelere 4 "işaretçi" içerir.

Oluşturduğunuz her dize kendi belleğini kullanır (iyi, her zaman değil - "string interning" için arama yapmak isteyebilirsiniz). Ancak, bu dize verilerini saklamak için özel türde bir bellek ayrılmaz. 'endişe re.

1

myCustomType her örneği dört dize referansları artı the usual per instance overhead içerecektir. örneğine ise noktalarını bu hafızayı yanı kaplar dört dizeleri.