About the location of data structure and its inner objects

Lijie Xu csxulijie at gmail.com
Mon Aug 26 01:20:30 PDT 2013


Hi, folks. I’m confused with the concrete locations of the data structure
and its inner objects in the heap. The questions are below.

A general question:

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.


Two concrete questions.

Q1: Can an array such as byte[], String[] and Object[] span two generations?

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[].



Q2: Can ArrayList, LinkedList, HashMap span two generations?

For example, I initialize some data structures as follows.

--------------------------------------------------------------------------------------------------

*import* java.util.ArrayList;

*import* java.util.HashMap;

*import* java.util.LinkedList;

*import* java.util.List;

*import* java.util.Map;



*public* *class* ObjectTest {

    *public* *static* *void* main(String[] args) {

       List<Obj> arrayList = *new* ArrayList<Obj>();

       List<Obj> linkedList = *new* LinkedList<Obj>();

       Map<String, Obj> hashMap = *new* HashMap<String, Obj>();



       *for*(*int* i = 0; i < 10000; i++) {

           Obj arrayObj = *new* Obj();

           arrayList.add(arrayObj);

       }



       *for*(*int* i = 0; i < 10000; i++) {

           Obj linkedObj = *new* Obj();

           linkedList.add(linkedObj);

       }



       *for*(*int* i = 0; i < 10000; i++) {

           String str = i + "";

           Obj hashObj = *new* Obj();

           hashMap.put(str, hashObj);

       }

    }

}



*class* Obj {

    *byte*[] bytes;

    *public* Obj() {

       bytes = *new* *byte*[16];

    }

}

--------------------------------------------------------------------------------------------------

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.

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/hotspot-gc-dev/attachments/20130826/a747d694/attachment.html 


More information about the hotspot-gc-dev mailing list