SORU
23 EKİM 2008, PERŞEMBE


Bellekte bir bayt - Java boyutu

Bir bayt bir java programı içinde ele alır bellek miktarını üzerinde karışık görüşler duydum.

Ben haberdar edebilirsiniz deposu en fazla 127 bir java bayt, ve documentation yazan bir bayt sadece 8 bit ama here olduğumu söyledi aslında alır, aynı miktarda bellek olarak int, ve bu nedenle sadece bir Tür yardımcı kod anlama ve verimlilik.

Herkes bu kadar net ve bu uygulama belirli bir sorun olur mu?

CEVAP
23 EKİM 2008, PERŞEMBE


Tamam, bir çok tartışma ve bir sürü kod değil:) oldu

Burada hızlı bir kriter. Bu tür bir şey geldiğinde normal uyarılar var - hafıza testi tuhaflıklar vb JİTting nedeniyle vardır, ama uygun çok sayıda yararlı zaten. İki çeşidi vardır, 80 üye - LotsOfBytes her biri 80 bayt, LotsOfİnts 80 değer vermez. Onları seviyoruz yapı, GC değiller emin olun ve bellek kullanımını kontrol edin:

class LotsOfBytes
{
    byte a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab, ac, ad, ae, af;
    byte b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf;
    byte c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, ca, cb, cc, cd, ce, cf;
    byte d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, da, db, dc, dd, de, df;
    byte e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, ea, eb, ec, ed, ee, ef;
}

class LotsOfInts
{
    int a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab, ac, ad, ae, af;
    int b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf;
    int c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, ca, cb, cc, cd, ce, cf;
    int d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, da, db, dc, dd, de, df;
    int e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, ea, eb, ec, ed, ee, ef;
}


public class Test
{
    private static final int SIZE = 1000000;

    public static void main(String[] args) throws Exception
    {        
        LotsOfBytes[] first = new LotsOfBytes[SIZE];
        LotsOfInts[] second = new LotsOfInts[SIZE];

        System.gc();
        long startMem = getMemory();

        for (int i=0; i < SIZE; i  )
        {
            first[i] = new LotsOfBytes();
        }

        System.gc();
        long endMem = getMemory();

        System.out.println ("Size for LotsOfBytes: "   (endMem-startMem));
        System.out.println ("Average size: "   ((endMem-startMem) / ((double)SIZE)));

        System.gc();
        startMem = getMemory();
        for (int i=0; i < SIZE; i  )
        {
            second[i] = new LotsOfInts();
        }
        System.gc();
        endMem = getMemory();

        System.out.println ("Size for LotsOfInts: "   (endMem-startMem));
        System.out.println ("Average size: "   ((endMem-startMem) / ((double)SIZE)));

        // Make sure nothing gets collected
        long total = 0;
        for (int i=0; i < SIZE; i  )
        {
            total  = first[i].a0   second[i].a0;
        }
        System.out.println(total);
    }

    private static long getMemory()
    {
        Runtime runtime = Runtime.getRuntime();
        return runtime.totalMemory() - runtime.freeMemory();
    }
}

Kutudan çıktı:

Size for LotsOfBytes: 88811688
Average size: 88.811688
Size for LotsOfInts: 327076360
Average size: 327.07636
0

Bir şekilde LotsOfİnts için sadece 7 ancak görünüşe göre havai - 8 bazı veriler var yani açıkçası (? dediğim gibi, orada tuhaflıklar burada) ama mesele o bayt alanları görünüyor paketlenmiş için LotsOfBytes böyle sürer (sonra yük kaldırma) sadece dörtte biri kadar bellek olarak LotsOfİnts.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • CasinoRoyaleMovie's channel

    CasinoRoyale

    1 AĞUSTOS 2006
  • Jaclyn W

    Jaclyn W

    5 Mayıs 2006
  • natescamp

    natescamp

    30 NİSAN 2009