Repository navigation
Expand file tree
/
Copy pathCaculatorMemorySize.java
More file actions
48 lines (41 loc) · 1.61 KB
/
Copy pathCaculatorMemorySize.java
File metadata and controls
48 lines (41 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package memory;
import serialization.object.Person;
import sizeof.agent.SizeOfAgent;
/**
* 计算对象占用java内存
*
* 参考:https://www.cnblogs.com/magialmoon/p/3757767.html
*
* @author: zhengyong Date: 2018/6/1 Time: 下午3:25
*/
public class CaculatorMemorySize {
public static void main(String[] args) {
/**
* 1. 引入jar包:
* <pre>
* <dependency>
<groupId>com.googlecode.sizeofag</groupId>
<artifactId>sizeofag</artifactId>
<version>1.0.0</version>
</dependency>
* </pre>
* 2. 设置VM option: -javaagent:/Users/zhengyong/Downloads/sizeofag-1.0.0.jar
*/
System.out.println("String sizeOf:" + SizeOfAgent.sizeOf(new String("test")));
System.out.println("String fullSizeOf:" + SizeOfAgent.fullSizeOf(new String("test")));
System.out.println("Integer sizeOf:" + SizeOfAgent.sizeOf(new Integer(1)));
System.out.println("Integer fullSizeOf:" + SizeOfAgent.fullSizeOf(new Integer(1)));
Person person = new Person();
person.setId(1);
person.setName("test");
person.setEmail("111111111111test@qq.com");
System.out.println("Person sizeOf:" + SizeOfAgent.sizeOf(person));
System.out.println("Person fullSizeOf:" + SizeOfAgent.fullSizeOf(person));
System.out.println("B2 sizeOf:" + SizeOfAgent.sizeOf(new B2()));
System.out.println("B2 fullSizeOf:" + SizeOfAgent.fullSizeOf(new B2()));
}
static class B2 {
int b2a;
Integer b2b;
}
}