How big a Java object really is

Source: Internet
Author: User

Often encountering a problem, you need to cache a batch of data in memory to improve efficiency (avoid reading db every time). The question is, how much memory will these objects take up, which directly determines how many records can be cached, and whether there is enough memory to go online.

See several workarounds.

Test

Practice is the only standard for testing truth! For example, if you want to cache10w a record, you load the 10w record into memory and see how much memory is being used. As for how to look at how much memory it takes, you can

    1. Task Manager
    2. Top
    3. Java Runtime class
    4. BlaBla ....

Let's take a look at the runtime that can be obtained directly from the Java program.

Import java.util.*;/** * Created by Magicalli on 2015/2/3.    */public class Testmemory {static Class A {int A; The public static void main (string[] args) throws Interruptedexception {System.out.println ("---Memory Usage---:        ");        Runtime RT = Runtime.getruntime ();        Print total memory size//Print free memory size//print used memory size units (bytes) long usedmemory = Rt.totalmemory ()-rt.freememory (); System.out.println ("Total memory=" + rt.totalmemory () + "free Memory =" + rt.freememory () + "used memory=" + Usedmemor        y);        Put the memory-hogging code you want to test here------start--------------Final int N = 100000;        int[] arr = new Int[n];        integer[] arr2 = new Integer[n];        a[] ArrA = new A[n];        for (int i = 0; i < N; i++) {Arr[i] = i;//Arr2[i] = i;//Arra[i] = new A ();        }//list<integer> List = new arraylist<integer> (); Map<integer, string> map = new Hashmap<integer, string> ();//FOR (int i = 0; i < N; i++) {//List.add (i);//Map.put (I, Uuid.randomuuid (). toString ());/}        System.out.println (Map.size ());        Put the memory-hogging code you want to test here------end--------------Long UsedMemory2 = Rt.totalmemory ()-rt.freememory (); System.out.println ("Total memory=" + rt.totalmemory () + "free Memory =" + rt.freememory () + "used memory=" + Usedmemor        y2);        Long objmemory = usedmemory2-usedmemory;    SYSTEM.OUT.PRINTLN ("Object Use Memory:" + objmemory/1024 + "K" + "All is:" + objmemory/n); }}

The most important advantage of the above method is that it is simple and effective to get the actual memory size directly. Bad place is if the amount of data is small, may be a large deviation, and you can not explain why integer[] than int[] occupy a lot of memory, the key is experts said: this memory consumption should be calculated in mind, you have to run a program, obviously low, but also want to qualify? Let's practice! So let's see how to pinch a count!

Calculation

This requires an understanding of the memory distribution in the JVM, knowing that each object has a header,blabal. Recommend a good article here how much memory does a Java object occupy? , I will not repeat it.

Also see another way of calculation, with the unsafe, but feel no front with instrumentation good. Refer to Java here to calculate the size of an object occupying memory

View on line

If you want to see which objects in the online program are consuming a lot of memory (such as analyzing memory leaks), you can use Jmap.

Related knowledge

You may need to know jps,jinfo, pack jar,manifest, see JVM Run parameters, and more.

Refers
    1. Http://www.importnew.com/14948.html
    2. Http://www.cnblogs.com/magialmoon/p/3757767.html
    3. http://www.oschina.net/question/1_4486
    4. http://blog.csdn.net/bobpauline/article/details/20699233
    5. http://happyqing.iteye.com/blog/2013639
    6. http://sunqi.iteye.com/blog/1917802
    7. Http://www.blogjava.net/stone2083/archive/2013/06/08/400410.html
    8. http://yueyemaitian.iteye.com/blog/2033046
    9. Http://www.ibm.com/developerworks/cn/java/j-lo-jse61/index.html
    10. http://www.ibm.com/developerworks/cn/java/j-lo-instrumentation/

Written with Stackedit.

How big a Java object really is

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.