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
- Task Manager
- Top
- Java Runtime class
- 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
- Http://www.importnew.com/14948.html
- Http://www.cnblogs.com/magialmoon/p/3757767.html
- http://www.oschina.net/question/1_4486
- http://blog.csdn.net/bobpauline/article/details/20699233
- http://happyqing.iteye.com/blog/2013639
- http://sunqi.iteye.com/blog/1917802
- Http://www.blogjava.net/stone2083/archive/2013/06/08/400410.html
- http://yueyemaitian.iteye.com/blog/2033046
- Http://www.ibm.com/developerworks/cn/java/j-lo-jse61/index.html
- http://www.ibm.com/developerworks/cn/java/j-lo-instrumentation/
Written with Stackedit.
How big a Java object really is