Help us understand the problem. What is going on with this article?

[Java]オブジェクトの使用メモリを測る

More than 3 years have passed since last update.

オブジェクトサイズを測りたい

近年の分散ブームによりますます memory layout が重要になってきているらしい。twitter commons には ObjectSizeCalculator.java が用意されているし、OpenJDK に組み込まれている jolPresto から呼ばれていたりする。

それはさておき、パフォーマンスチューニング等でオブジェクトサイズをよく測っていたので、メモ。上2つに java.sizeOf を加えて3つを記載。先に言っておくと結果の考察はなし。気になる人は Google 先生が教えてくれると思う。

String s = "";
String s2 = "0123456789";
List<String> list = new ArrayList<>();
list.add(s);
list.add(s2);

SizeOf

SizeOf.skipFinalField(false);
SizeOf.skipStaticField(true);
SizeOf.setMinSizeToLog(40);
long sSize = SizeOf.deepSizeOf(s);
long s2Size = SizeOf.deepSizeOf(s2);
long listSize = SizeOf.deepSizeOf(list);

※ VM引数に -javaagent:./lib/SizeOf.jar が必要

Twitter Commons

long sSize = ObjectSizeCalculator.getObjectSize(s);
long s2Size = ObjectSizeCalculator.getObjectSize(s2);
long listSize = ObjectSizeCalculator.getObjectSize(list);

jol

long sSize = GraphLayout.parseInstance(s).totalSize();
long s2Size = GraphLayout.parseInstance(s2).totalSize();
long listSize = GraphLayout.parseInstance(list).totalSize();

結果

string (empty) string (10chars) list (2size)
sizeOf 40 64 184
twitter commons 40 64 184
jol 40 64 184

単位:byte

※ OSX(10.10.1)(64bit)、Java7u45 で起動
※ フルコードはこちら→ disktnk / ObjectSizeTest.java

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away