Do you know the size of the data? - Don't spend too much effort to hide the members (1)

zhaozj2021-02-16  44

Do you know the size of the data?

- Don't spend too much effort to hide the members

Summary:

In the past many years, many Java developers have been asking a question: "How much memory is spent on a Java object?" In this article, Vladimir Roubtsov explained this problem with the previous solution, outside of this. Based on his experience demonstrates the use of memory, and also provides some techniques to make your Java program more efficient.

Author: Vladimir Roubtsov

Recently, we help to develop a Java server, this is a similar memory database. That is to say, we especially emphasize the design, because design should be specifically considering a large amount of data in memory to improve the performance of the query.

Once we get the prototype of running, then after the data is analyzed from the hard disk, we naturally determine the contour of the data store. Not very satisfied with the initial effect, prompted us to find a better solution.

tool:

Since Java has a purposeful to hide the details of many memory management, you have to find out how much memory is required to consume some memory. You can use the runtime.freeMemory () method to measure the change value of the heap before and after one or more objects being assigned. These methods are described in detail, for example, in detail, in detail. But unfortunately, the failure of these previous articles in the implementation uses a wrong Runtime method. Even after later articles have its incompleteness.

l The function provided by calling the runtime.freeMemory () method is not enough because JVM can decide to increase its current heap size at any time (as long as needed, especially when running garbage collection). Unless the parameter -Xmx specifies the maximum value of the pile unless you run, we should use Runtime.TotalMemory () - Runtime.FreeMemory () as the heap size used.

l Perform a single runtime.gc () method does not guarantee effective request garbage collection. For example, we can request a normal Finalizer running normal. Since Runtime.gc () cannot guarantee blocking to garbage disposal, then wait until the stack of stability is a good way.

l If the contour class creates a static data as part of the previous class initialization, the stack memory should include this data for the assignment of the first class instance. We should ignore the heap space consumed by the first class instance.

Consider these issues: We give a sizeof, as a tool to see a variety of Java cores and application classes.

Public Class Sizeof

{

Public static void main (string [] args) Throws Exception

{

// Warm Up All Classes / Methods We Will Use

RUNGC ();

USEDMEMORY ();

// array to keep strong references to allocated Objects

Final Int count = 100000;

Object [] Objects = new object [count];

Long Heap1 = 0;

// Allocate Count 1 Objects, Discard The First ONE

For (int i = -1; i

Object Object = NULL;

// instantiate your data here and assign it to object

Object = new object ();

// Object = new integer (i);

// Object = new long (i);

// Object = new string ();

// Object = new byte [128] [1]

IF (i> = 0)

Objects [i] = Object;

Else

{

Object = null; // discard the Warm Up Object

RUNGC ();

HEAP1 = UsedMemory (); // Take a Before Heap Snapshot

}

}

RUNGC ();

Long Heap2 = UsedMemory (); // Take an After Heap Snapshot:

Final int size = math.round ((Float) (Heap2 - Heap1)) / count);

System.out.println ("'Before' HEAP:" Heap1

", 'After' Heap:" Heap2);

System.out.println ("Heap Delta:" (Heap2 - HEAP1)

", {" Objects [0] .getClass () "} size =" size "bytes");

For (int i = 0; i

Objects = NULL;

}

Private static void rungc () THROWS EXCEPTION

{

// IT helps to call runtime.gc ()

// Using Several Method Calls:

For (int R = 0; r <4; r) _rungc ();

}

Private static void _rungc () THROWS EXCEPTION

{

Long usemedmem1 = usedmemory (), usedmem2 = long.max_value;

For (int i = 0; (usemedmem1

{

S_Runtime.RunFinalization ();

S_Runtime.gc ();

Thread.currentthread () .yeld ();

Usedmem2 = usedMem1;

Usedmem1 = usemedMemory ();

}

}

Private static long buyMemory ()

{

Return S_Runtime.TotalMemory () - S_Runtime.FreeMemory ();

}

Private static final runtime s_runtime = runtime.getRuntime ();

} // End of class

The key way of SIZEOF is RUNGC () and UsedMemory () methods, I used the package method such as RUNGC () to call _rungc () several times, in order to make this method more significant effect. Note that I call the RUNGC () method, you can edit your code in Heap1 and Heap2, join your example you are interested in.

Also note how the SIZEOF outputs the size of the object, and the data transfer closure requirement is used to be used by the COUNT class instance and is then removed by COUNT. For most classes, this result will be a memory size consumed by a single class instance object, including all its own member domains. The boundary of memory is different from the shadow memory boundary by some commercial tools (for example, if an object has an int [], its memory consumption will appear very special).

转载请注明原文地址:https://www.9cbs.com/read-26507.html

New Post(0)