2012-09-06 31 views
1

我的問題或更確切的問題如下:內存分配和變量的使用壽命

1)靜態變量駐留在哪裏。一些文章說,他們駐留在堆上,有些人說他們是類屬性,因此他們在類定義中使用了perm gen區域。我明白第二個選項可能是正確的,因爲它是一個類屬性。
2)在哪裏最終變量駐留和什麼是它的壽命,如果: 一個)及其類型原語 B)及其類型原語 c的方法的局部變量)及其類型參考實例變量的一個實例變量 b)它是參考類型的方法的局部變量
3)參考局部變量在哪裏存儲,如果它們是本地的。
4)如果數組在內存分配方面有任何不同,就像它們是實例變量或本地線程變量一樣。

由於

+1

對於4個不同的問題,4個不同的線程如何? – CodeBlue

回答

3

Where does the static variable reside

-靜態變量駐留在Method Area,和PermGen的是方法區域內。

Where does the final variable reside and what is its life if 
Its an instance variable of type primitive 

-如果其實例變量,它停留在Heap inside the Object其所屬和超出範圍,因爲是將其保持它的對象沒有提到..

​​

-它停留在堆棧,並超出範圍,因爲截至收盤達到方法撐...

Its an instance variable of type reference

-它停留在Heap inside the Object其所屬,並超出範圍,因爲對持有它的對象沒有提到..

Its a local variable of a method of type reference

-它停留在堆棧上,並且因爲那裏到達範圍而落在範圍之外,因爲到達支架的方法已經到達...

Where are the reference local variables stored if they are local.

-在棧...

In case of arrays is there any difference in memory allocation as in they are instancevariable or local thread variable.

-好視數組是存儲在Heap對象 ....但是從Java 6u23版本,一直存在引入Escape Analysis,根據這個如果JVM認定該對象不能逃避該方法,它會嘗試在線程堆棧上創建對象,而不是在堆上....

+0

非常感謝..dat非常有幫助 – user1649415

+0

你是最受歡迎的... –