..
Java
HashSet?
HashSet?
:
4 * 10.000.000 = 40
* , ,
JVM :
1)
2)
3)
4) /
.
JVM(Hotspot, openJVM) .
32-
64-
16
:
Mark Word
Hash Code
Garbage Collection Information
Lock
Type Information Block Pointer
Array Length
Mark Word Hash Code, Garbage Collection
Information, Lock
Hash Code .
Object.hashCode()
.
Garbage Collection Information java
.
Lock
.
.
Type Information Block Pointer
( ,
,
).
Array Length ,
4 .
/
, ,
.
,
. :
, java
8 .
Type
byte
short
2 byte
int
4 byte
long
8 byte
char
2 byte
float
4 byte
double
8 byte
boolean
1 bit
?
, ,
.
,
JVM .
8 bytes
12 bytes
@ 32 bit JVM
@ 64 bit JVM
//
int a = 300;
4- , .
- sizeOf(int)
//
Integer b = 301;
,
.
- sizeOf(reference) + sizeOf(Integer)
Integer
int
16
String:
private final char value[];
private final int offset;
private final int count;
private int hash;
int
4 3 = 12
24
new String(a)
new String()
int
4 3 = 12
24
new char[1]
8 + 4 = 12
char
2 1 = 2
16
, new String("a") = 40
new String("a") new String("ab")
class Cursor {
String icon;
Position pos;
Cursor(String icon, int x, int y) {
this.icon = icon;
this.pos = new Position(x, y);
}
}
class Position {
int x;
int y;
Position(int x, int y) {
this.x = x;
this.y = y;
}
}
class Cursor2 {
String icon;
int x;
int y;
Cursor2(String icon, int x, int y) {
this.icon = icon;
this.x = x;
this.y = y;
}
}
Cursor2 30%
Cursor ( Cursor + Position).
100 .
.
,
.
class A
{
int a;
}
class B
{
int a;
int b;
}
B
. 3 :
?
,
.
( hash
String).
byte
int,
.
:
Object[2][1000]
Object[1000][2]
.
2
.
!
998 . 7.
.
Boolean boolean
JVM.
, Oracle HotSpot JVM
4 , int.
1 31 .
boolean
BitSet boolean,
,
( ).
BitSet long,
long
.
BitSet bits = new BitSet();
bits.set(0); // set the 0th bit
bits.set(6); // set the 6th bit
JMV cost
Type
JLS
JMV cost
byte
1 byte
1..8 bytes
short
2 byte
2..8 bytes
int
4 byte
4..8 bytes
long
8 byte
8..16 bytes
char
2 byte
2..8 bytes
float
4 byte
4..8 bytes
double
8 byte
8..16 bytes
boolean
1 bit
1..8 bytes
JLS
JMV cost
Wrapper*
byte
1 byte
1..8 bytes
16 bytes
short
2 byte
2..8 bytes
16 bytes
int
4 byte
4..8 bytes
16 bytes
long
8 byte
8..16 bytes
24 bytes
char
2 byte
2..8 bytes
16 bytes
float
4 byte
4..8 bytes
16 bytes
double
8 byte
8..16 bytes
24 bytes
boolean
1 bit
1..8 bytes
16 bytes
public class
FooClass
{
Object x = null;
Object y = null;
}
16 bytes
24 bytes
36 bytes
?
32 bit
8 header + 4 ref + 4 ref = 16 bytes
64 bit + Compressed OOPs (Xmx < 32 gb)
12 header + 4 ref + 4 ref = 20 (align) 24 bytes
64 bit Compressed OOPs (Xmx > 32 gb)
12 header + 8 ref + 8 ref = 28 (align) 32 bytes
Compressed OOPs
32-
32 . ,
32- 232 =
4294967296 4 .
64- 264
.
.
- XX:+UseCompressedOops.
64-
JVM 32 .
Compressed OOPs
Profit
, 4
.
4 .
.
.
Loss
232.
.
. JVM
.
Flyweights
int myVar = new Integer(5)
<PrimitiveWrapper>.valueOf()
Byte
Short
Integer
Long
Character
String.intern()
1 byte is
cached
for -128127
Collections
Collection (10 mln ints)
Overhead
Pure data
0 (40 mb)
int[]
Integer[]
5x (200 mb)
Integer[] (valueOf)
ArrayList<Integer>()
ArrayList<Integer>(10 mln)
HashSet<Integer>()
HashSet<Integer>(10 mln)
+16 bytes
+10 mln * 4 bytes
+10 mln * 16 bytes
Preallocation
Preallocation
Collections
500
1 10000.
.
* ()
ConcurrentHashSet
Array
,
.
4 (8)
(int)
Header
8 (12)
16 (24)
ArrayList
.. ,
( 10 )
, :
int newCapacity = (oldCapacity * 3)/2 + 1
6 (12) .
4 (8)
Array
16 (24)
Header
8 (12)
ArrayList
80 (144)
,
.
LinkedList
.
java.util.LinkedList.Entry.
:
Previous Entry
Next Entry
- 32bit 4 ,
24 (40) Entry.
TreeSet
LinkedList .
java.util.TreeMap.Entry, :
, , , .
, ,
. Entry 32 (64) .
TreeMap :
entrySet, navigableKeySet, descendingMap
64 (104) .
HashSet
HashSet HashMap.
java.util.HashMap.Entry, , ,
Entry, .
Entry 24 (48) .
Entry -, Entry,
16 ,
75% (default) .
, 12, 24, 48, 96 . .
(2n*3, 6144 ).
HashSet 136 (240) .
LinkedHashSet
java.util.LinkedHashMap.Entry,
java.util.HashMap.Entry,
, 8 (16) ,
HashSet, 37.33 (74.67),
42.67 (85.33).
, LinkedList
Entry, .
LinkedHashSet 176 (320) .
IdentityHashMap
IdentityHashMap ==, equals
System.identityHashCode.
Entry
,
.
IdentityHashMap ,
, 2/3. 32
. 21, 42, 85, 170 . .
3 ,
IdentityHashMap, 6 .
, 12 (24) 24 (48)
.
344 (656) ,
.
ConcurrentHashMap
ConcurrentHashMap ,
( -).
( 16),
HashMap.
-
, .
HashMap.
,
: ,
.
1304 (2328)
, 16 ,
16 .
10000 ConcurrentHashSet HashSet
0.3%.
Array
ArrayList
LinkedList
TreeSet
HashSet
LinkedHashSet
IdentityHashMap
ConcurrentHashMap
16(24)
80 (144)
24 (40)
64 (104)
136 (240)
176 (320)
344 (656)
1306 (2428)
,
- .
.
Solutions. Trove
Collection (10 mln ints)
Size
Pure data
40 mb
TIntArrayList
TIntArrayList(10 mln)
TIntHashSet
TIntHashSet(10 mln)
Solutions. More
Collection (10 mln ints)
Size
Pure data
40 mb
fastutils IntOpenHashSet
org.a.c.c.p. ArrayIntList
hppc IntIntOpenHashMap
Set,
Map,
overhead
Solutions. MapDB
JVM 64bit Oracle Java 1.7.0_09-b05 HotSpot 23.5-b02
(-XX: + UseCompressedOops)
map 16
?
Solutions. MapDB
,
.
MapDB TreeMap HashMap
.
,
.
Solutions. MapDB
Map m = new TreeMap();
for(long counter=0;;counter++)
{
m.put(counter,"");
if(counter%1000000==0)
System.out.println(""+counter);
}
LongHashMap
172 mln
276 mln
980 mln
DB db = DBMaker.newDirectMemoryDB().transactionDisable().make();
Map m = db.getTreeMap(test);
for(long counter=0;;counter++)
{
m.put(counter,"");
if(counter%1000000==0)System.out.println(""+counter);
}
+ ,
1738 mln
Memory leak
,
, .
Links
http://habrahabr.ru/post/76481/
http://habrahabr.ru/post/158451/
http://habrahabr.ru/post/159557/
http://habrahabr.ru/post/134102/
http://habrahabr.ru/post/117274/
http://habrahabr.ru/post/136883/
http://habrahabr.ru/post/84165/
http://habrahabr.ru/post/134910/
http://habrahabr.ru/post/136136/
http://habrahabr.ru/post/132500/
http://habrahabr.ru/post/51107/
http://habrahabr.ru/post/142409/
http://habrahabr.ru/post/71704/
http://habrahabr.ru/post/112676/
http://juravskiy.ru/?p=1369
http://www.devclub.eu/2012/12/03/video-nikita-salnikov-java-objects/
http://docs.oracle.com/javase/specs/#22909
http://ru.wikipedia.org/wiki/_