<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Java always conceptually holds a reference rather than the whole
object body, but you can directly see and use the body in C++.<br>
<br>
Below let me illustrate on the HashMap example?<br>
<br>
Tao<br>
<br>
On 8/26/13 1:20 AM, Lijie Xu wrote:
<blockquote
cite="mid:CAEyFhSe2EAhUtnxOvmU_2aBAdMbbNDB7mGNLVrrQdN_-JrRY2g@mail.gmail.com"
type="cite">
<div dir="ltr">
<p class="">Hi, folks. I’m confused with the concrete
locations of the data structure and its inner objects in the
heap. The questions
are below.</p>
<p class=""><span lang="EN-US">A general question: </span></p>
<p class=""><span lang="EN-US">If an object X is decided to be
copied into
old from new gen by GC, all the objects which can be reached
from X are copied into old too.
Or X’s retained set. Or this statement is wrong.</span></p>
<p class=""><span lang="EN-US"><br>
</span></p>
<p class=""><span lang="EN-US">Two concrete questions.</span></p>
<p class=""><span lang="EN-US">Q1: Can an array such as byte[],
String[]
and Object[] span two generations?</span></p>
<p class=""><span lang="EN-US">I think primitive arrays such as
byte[] and
int[] cannot span (e.g., a part of the array exists in eden
and the other part exists
in old space). For reference arrays such as Object[], the
array itself cannot
span but the items in the arrays can span (i.e., some items
exist in new gen
while others exist in old gen). I’m not sure if I’m right
and if String[] is as
same as byte[].</span></p>
<p class=""><span lang="EN-US"> </span></p>
<p class=""><span lang="EN-US">Q2: Can ArrayList, LinkedList,
HashMap span
two generations?</span></p>
<p class=""><span lang="EN-US">For example, I initialize some
data structures
as follows. </span></p>
</div>
</blockquote>
Say, hashMap = {str1: obj1; str2: obj2; ...; strn: objn;}<br>
<br>
The object body of hashMap is in the same generation, including the
data structure containing references of str(i) and obj(i); however,
the object bodies of str(i) and obj(i) may span different
generations.<br>
<br>
BTW, hashMap itself (not its referenced object body) is a reference
and, hence, on stack (not on java heap) since it's a local variable.<br>
<br>
Hope this helps build a concrete picture of memory layout.<br>
<br>
<blockquote
cite="mid:CAEyFhSe2EAhUtnxOvmU_2aBAdMbbNDB7mGNLVrrQdN_-JrRY2g@mail.gmail.com"
type="cite">
<div dir="ltr">
<p class=""><span lang="EN-US">--------------------------------------------------------------------------------------------------</span></p>
<p class="" align="left"><b><span style="font-family:'Courier
New';color:rgb(127,0,85)" lang="EN-US">import</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">
java.util.ArrayList;</span><span style="font-family:'Courier
New'" lang="EN-US"></span></p>
<p class="" align="left"><b><span style="font-family:'Courier
New';color:rgb(127,0,85)" lang="EN-US">import</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">
java.util.HashMap;</span><span style="font-family:'Courier
New'" lang="EN-US"></span></p>
<p class="" align="left"><b><span style="font-family:'Courier
New';color:rgb(127,0,85)" lang="EN-US">import</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">
java.util.LinkedList;</span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class="" align="left"><b><span style="font-family:'Courier
New';color:rgb(127,0,85)" lang="EN-US">import</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">
java.util.List;</span><span style="font-family:'Courier
New'" lang="EN-US"></span></p>
<p class="" align="left"><b><span style="font-family:'Courier
New';color:rgb(127,0,85)" lang="EN-US">import</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">
java.util.Map;</span><span style="font-family:'Courier New'"
lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier New'"
lang="EN-US"> </span></p>
<p class="" align="left"><b><span style="font-family:'Courier
New';color:rgb(127,0,85)" lang="EN-US">public</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">
</span><b><span style="font-family:'Courier
New';color:rgb(127,0,85)" lang="EN-US">class</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">
ObjectTest {</span><span style="font-family:'Courier New'"
lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US"> </span><b><span
style="font-family:'Courier New';color:rgb(127,0,85)"
lang="EN-US">public</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">
</span><b><span style="font-family:'Courier
New';color:rgb(127,0,85)" lang="EN-US">static</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">
</span><b><span style="font-family:'Courier
New';color:rgb(127,0,85)" lang="EN-US">void</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">
main(String[] args) {</span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US"> List<Obj>
arrayList = </span><b><span style="font-family:'Courier
New';color:rgb(127,0,85)" lang="EN-US">new</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">
ArrayList<Obj>();</span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US"> List<Obj>
linkedList = </span><b><span style="font-family:'Courier
New';color:rgb(127,0,85)" lang="EN-US">new</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">
LinkedList<Obj>();</span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US"> Map<String, Obj>
hashMap = </span><b><span style="font-family:'Courier
New';color:rgb(127,0,85)" lang="EN-US">new</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">
HashMap<String, Obj>();</span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US"> </span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US"> </span><b><span
style="font-family:'Courier New';color:rgb(127,0,85)"
lang="EN-US">for</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">(</span><b><span
style="font-family:'Courier New';color:rgb(127,0,85)"
lang="EN-US">int</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">
i = 0; i < 10000; i++) {</span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US"> Obj arrayObj = </span><b><span
style="font-family:'Courier New';color:rgb(127,0,85)"
lang="EN-US">new</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">
Obj();</span><span style="font-family:'Courier New'"
lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US">
arrayList.add(arrayObj);</span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class="" align="left">
<span style="font-family:'Courier New';color:black"
lang="EN-US"> }</span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US"> </span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US"> </span><b><span
style="font-family:'Courier New';color:rgb(127,0,85)"
lang="EN-US">for</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">(</span><b><span
style="font-family:'Courier New';color:rgb(127,0,85)"
lang="EN-US">int</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">
i = 0; i < 10000; i++) {</span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US"> Obj linkedObj = </span><b><span
style="font-family:'Courier New';color:rgb(127,0,85)"
lang="EN-US">new</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">
Obj();</span><span style="font-family:'Courier New'"
lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US">
linkedList.add(linkedObj);</span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class="" align="left">
<span style="font-family:'Courier New';color:black"
lang="EN-US"> }</span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US"> </span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US"> </span><b><span
style="font-family:'Courier New';color:rgb(127,0,85)"
lang="EN-US">for</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">(</span><b><span
style="font-family:'Courier New';color:rgb(127,0,85)"
lang="EN-US">int</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">
i = 0; i < 10000; i++) {</span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US"> String str = i + </span><span
style="font-family:'Courier New';color:rgb(42,0,255)"
lang="EN-US">""</span><span style="font-family:'Courier
New';color:black" lang="EN-US">;</span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US"> Obj hashObj = </span><b><span
style="font-family:'Courier New';color:rgb(127,0,85)"
lang="EN-US">new</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">
Obj();</span><span style="font-family:'Courier New'"
lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US"> hashMap.put(str,
hashObj);</span><span style="font-family:'Courier New'"
lang="EN-US"></span></p>
<p class="" align="left">
<span style="font-family:'Courier New';color:black"
lang="EN-US"> }</span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US"> }</span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US">}</span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier New'"
lang="EN-US"> </span></p>
<p class="" align="left"><b><span style="font-family:'Courier
New';color:rgb(127,0,85)" lang="EN-US">class</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">
Obj {</span><span style="font-family:'Courier New'"
lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US"> </span><b><span
style="font-family:'Courier New';color:rgb(127,0,85)"
lang="EN-US">byte</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">[]
</span><span style="font-family:'Courier
New';color:rgb(0,0,192)" lang="EN-US">bytes</span><span
style="font-family:'Courier New';color:black" lang="EN-US">;</span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US"> </span><b><span
style="font-family:'Courier New';color:rgb(127,0,85)"
lang="EN-US">public</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">
Obj() {</span><span style="font-family:'Courier New'"
lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US"> </span><span
style="font-family:'Courier New';color:rgb(0,0,192)"
lang="EN-US">bytes</span><span style="font-family:'Courier
New';color:black" lang="EN-US"> = </span><b><span
style="font-family:'Courier New';color:rgb(127,0,85)"
lang="EN-US">new</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">
</span><b><span style="font-family:'Courier
New';color:rgb(127,0,85)" lang="EN-US">byte</span></b><span
style="font-family:'Courier New';color:black" lang="EN-US">[16];</span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US"> }</span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class=""><span lang="EN-US">
</span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US">}</span><span
style="font-family:'Courier New'" lang="EN-US"></span></p>
<p class="" align="left"><span style="font-family:'Courier
New';color:black" lang="EN-US"><span
style="color:rgb(34,34,34);font-family:arial">--------------------------------------------------------------------------------------------------</span><br>
</span></p>
<p class=""><span lang="EN-US"></span><span lang="EN-US"></span></p>
<p class=""><span lang="EN-US">If new gen cannot hold all the
objects, GC
will occur. I want to know if all the items in the data
structure are copied
into old gen. </span></p>
<p class=""><span lang="EN-US">For example, arrayList itself
exists in old
while some of its arrayObjs exist in new gen. A arrayObj
exists in old gen
while its bytes exists in new gen.</span></p>
<p class=""><span lang="EN-US"> </span></p>
</div>
</blockquote>
</body>
</html>