数据库数据在Java占用内存简单估算
数据库数据在Java占用内存简单估算 结论: 1.数据库记录放在JAVA里,用对象(ORM一般的处理方式)需要4倍左右的内存空间,用HashMap这种KV保存需要10倍空间; 2.如果你主要数据是text大文本,那空间一般可以按2倍估算。 以上是一个通用数据测试结论,估大家参考
数据库数据在Java占用内存简单估算
结论:
1.数据库记录放在JAVA里,用对象(ORM一般的处理方式)需要4倍左右的内存空间,用HashMap这种KV保存需要10倍空间;
2.如果你主要数据是text大文本,那空间一般可以按2倍估算。
以上是一个通用数据测试结论,估大家参考。
数据库记录占用的空间大小比较好算,比如一个int占用4字节,bigint占用8字节,date占用3字节,datetime占用8字节,varchar是变长字节等。如果不想精确计算,在数据库中通过统计信息也可以比较轻松的知道表总共占用的空间及每条记录平均行长。
当我们用JDBC访问数据库时,经常会被问到内存溢出的问题,由于java是面向对象的语言,用JVM来自动内存回收,不能按普通方法计算内存,本文给出一个估算内存的思路和参考答案
先给出普通JDBC中数据库对象与内存的映射关系
MySQL |
Oracle |
JDBC |
Int |
Integer |
|
Int unsigned |
Long |
|
BigInt |
Long |
|
BigInt unsigned |
BigInteger |
|
Decimal |
Number |
BigDecimal |
Varchar |
Varchar2 |
String |
Date |
Date |
|
Datetime |
Date |
Timestamp |
Timestamp |
Timestamp |
Timestamp |
Clob |
Clob |
String |
Blob |
blob |
Byte[] |
Text |
Clob |
String |
float |
binary_float |
float |
double |
binary_double |
double |
上面这个比较好理解,接下来我们需要JAVA常用对象的内存占用空间,这个可以通过JDK 5 开始提供的Instrumentation 接口来完成,也可以通过开源的sizeOf.jar 来测试,笔者是通过sizeOf.jar验证的。测试结果数据如下:
对象 |
64位 JVM 压缩指针 |
64位 JVM 非压缩指针 |
Integer |
16 |
24 |
Long |
24 |
24 |
Object |
16 |
16 |
Date |
24 |
32 |
Timestamp |
32 |
40 |
String_0 |
48 |
64 |
String_1 |
56 |
72 |
String_10 |
72 |
88 |
String_100 |
248 |
264 |
StringBuilder |
24 |
32 |
BigDecimal |
40 |
48 |
BigInteger |
64 |
80 |
HashMap |
128 |
216 |
HashMap_0 |
72 |
96 |
HashMap_100 |
576 |
1112 |
HashMap_10000 |
65600 |
131160 |
ArrayList |
80 |
144 |
ArrayList_0 |
40 |
64 |
ArrayList_100 |
440 |
864 |
ArrayList_10000 |
40040 |
80064 |
LinkedList |
48 |
80 |
LinkedHashMap |
96 |
144 |
ClassA |
32 |
40 |
ClassB |
40 |
48 |
ClassC |
40 |
56 |
由于现在主机一般都是64位, 64位JVM从JDK1.6.45开始,当JVM最大内存小于32GB时,自动打开压缩指针特性,这样对象的内存占用空间少很多,由上表可以看出,至少减少1/3的空间。
下面我们结合数据库数据来测试
假如mysql数据库有一张emp表,结构如下:
CREATE TABLE `emp` ( `id` int(11) NOT NULL, `create_time` datetime DEFAULT NULL, `modify_time` datetime DEFAULT NULL, `name` varchar(16) DEFAULT NULL, `address` varchar(256) DEFAULT NULL, `age` smallint(6) DEFAULT NULL, `height` decimal(10,2) DEFAULT NULL, `weight` decimal(10,2) DEFAULT NULL, `phone` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
样本数据如下:
hm.put("id", 1988); hm.put("createTime", new Date()); hm.put("modifyTime", new Date()); hm.put("name", "张三丰"); hm.put("address","浙江杭州市西湖大道188号808室"); hm.put("age",88); hm.put("weight",new BigDecimal(88)); hm.put("height",new BigDecimal(188)); hm.put("phone","1388888888");
按上面样本数据计算,有效数据约80字节,在MySQL里占用空间约120字节
在java里转换为HashMap和Emp对象测试空间如下
对象 |
64位 JVM 压缩指针 |
64位 JVM 非压缩指针 |
HashMap_empty |
128 |
216 |
HashMap_full |
1360 |
1832 |
Emp_empty |
72 |
112 |
Emp_full |
464 |
600 |
从上面测试结果看,数据到JAVA里占用的空间增加了许多,在64位压缩指针下,如果存到HashMap,需要1360字节,空间是数据库约11.3倍,如果存为Emp普通对象,需要464字节,是数据库的3.8倍。
如果我们是一个分页从数据库读取emp信息,每页显示50条记录,用List保存,HashMap需要68KB,emp对象需要23KB。
根据这个简单测试,我们可以总结一个结论:
数据库记录放在JAVA里,用对象(ORM一般的处理方式)需要4倍左右的内存空间,用HashMap这种KV保存需要10倍空间。
如果你的数据和参考数据差异非常大,如主要数据text大文本,那空间一般可以简单的按2倍估算。
以上是一个通用数据测试结论,估大家参考。
下面是测试代码:
import net.sourceforge.sizeof.SizeOf; import java.io.IOException; import java.math.BigDecimal; import java.math.BigInteger; import java.sql.SQLException; import java.sql.Timestamp; import java.util.*; public class TestSize { static { SizeOf.skipStaticField(true); //java.sizeOf will not compute static fields //SizeOf.skipFinalField(true); //java.sizeOf will not compute final fields //SizeOf.skipFlyweightObject(true); //java.sizeOf will not compute well-known flyweight objects } public static void main(String[] args) throws SQLException, IOException, IllegalAccessException { TestSize ts=new TestSize(); ts.testObjectSize(); ts.testDataSize(); System.out.println("ok"); } public void testObjectSize() { System.out.println("Integer:"+SizeOf.deepSizeOf(new Integer(56))); System.out.println("Long:"+SizeOf.sizeOf(new Long(56L))); System.out.println("Object:"+SizeOf.sizeOf(new Object())); System.out.println("Date:"+SizeOf.sizeOf(new Date())); System.out.println("Timestamp:"+SizeOf.sizeOf(new Timestamp(System.currentTimeMillis()))); System.out.println("String_0:"+SizeOf.deepSizeOf(new String())); System.out.println("String_1:"+SizeOf.deepSizeOf(new String("1"))); System.out.println("String_10:"+SizeOf.deepSizeOf(new String("0123456789"))); System.out.println("String_100:"+SizeOf.deepSizeOf("0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789")); System.out.println("StringBuilder:"+SizeOf.deepSizeOf(new StringBuilder())); System.out.println("BigDecimal:"+SizeOf.deepSizeOf(new BigDecimal("34535643.23"))); System.out.println("BigInteger:"+SizeOf.deepSizeOf(new BigInteger("34535643"))); System.out.println("HashMap:"+SizeOf.deepSizeOf(new HashMap())); System.out.println("HashMap_0:"+SizeOf.deepSizeOf(new HashMap(0))); System.out.println("HashMap_100:"+SizeOf.deepSizeOf(new HashMap(100))); System.out.println("HashMap_10000:" + SizeOf.deepSizeOf(new HashMap(10000))); System.out.println("ArrayList:"+SizeOf.deepSizeOf(new ArrayList())); System.out.println("ArrayList_0:"+SizeOf.deepSizeOf(new ArrayList(0))); System.out.println("ArrayList_100:"+SizeOf.deepSizeOf(new ArrayList(100))); System.out.println("ArrayList_10000:"+SizeOf.deepSizeOf(new ArrayList(10000))); System.out.println("LinkedList:"+SizeOf.deepSizeOf(new LinkedList<Object>())); System.out.println("LinkedHashMap:"+SizeOf.deepSizeOf(new LinkedHashMap<Object,Object>())); System.out.println("ClassA:" + SizeOf.deepSizeOf(new ClassA())); System.out.println("ClassB:"+SizeOf.deepSizeOf(new ClassB())); System.out.println("ClassC:"+SizeOf.deepSizeOf(new ClassC())); } public void testDataSize() throws IOException, IllegalAccessException { HashMap hm=new HashMap(); System.out.println("HashMap_empty:"+SizeOf.deepSizeOf(hm)); hm.put("id", 1988); hm.put("createTime", new Date()); hm.put("modifyTime", new Date()); hm.put("name", "张三丰"); hm.put("address","浙江杭州市西湖大道188号808室"); hm.put("age",88); hm.put("weight",new BigDecimal(88)); hm.put("height",new BigDecimal(188)); hm.put("phone","1388888888"); System.out.println("HashMap_full:" + SizeOf.deepSizeOf(hm)); Emp emp=new Emp(); System.out.println("Emp_empty:"+SizeOf.deepSizeOf(emp)); emp.setId(1988); emp.setCreateTime(new Timestamp(System.currentTimeMillis())); emp.setModifyTime(new Timestamp(System.currentTimeMillis())); emp.setName("张三丰"); emp.setAddress("浙江杭州市西湖大道188号808室"); emp.setAge(28); emp.setWeight(new BigDecimal("88")); emp.setHeight(new BigDecimal("188")); emp.setPhone("13888888888"); System.out.println("Emp_full:"+SizeOf.deepSizeOf(emp)); } class ClassA{ } class ClassB extends ClassA{ } class ClassC extends ClassB{ } class Emp{ private Integer id; private Timestamp createTime; private Timestamp modifyTime; private String name; private String address; private Integer age; private BigDecimal height; private BigDecimal weight; private String phone; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Timestamp getCreateTime() { return createTime; } public void setCreateTime(Timestamp createTime) { this.createTime = createTime; } public Timestamp getModifyTime() { return modifyTime; } public void setModifyTime(Timestamp modifyTime) { this.modifyTime = modifyTime; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public BigDecimal getHeight() { return height; } public void setHeight(BigDecimal height) { this.height = height; } public BigDecimal getWeight() { return weight; } public void setWeight(BigDecimal weight) { this.weight = weight; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } } }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.
