Get the App
SLTechnology News&Howtos  ›  Development  › 

How to optimize memory

Shulou Source: shulou.com Published: 2022-06-03 00:41:39 09月14日 Update

This article mainly explains "how to optimize memory". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to optimize memory".

Case introduction

Here, I will summarize the memory overflow situation in the usual work process, and intuitively share it with you in the form of code cases, hoping to bring substantial help to the small partners.

Next, we analyze various memory overflows in the form of code cases.

Define the main class structure

First, we create a class called BlowUpJVM, and all case experiments are based on this class.

Public class BlowUpJVM {

}

Stack depth overflow public static void testStackOverFlow () {

BlowUpJVM.testStackOverFlow ()

}

The stack is constantly recursive and unprocessed, so the virtual machine stack continues to go deeper and deeper, and the stack depth overflows.

Permanent memory overflow public static void testPergemOutOfMemory1 () {

/ / method one failed

List list = new ArrayList ()

While (true) {

List.add (UUID.randomUUID (). ToString (). Intern ())

}

}

Intend to pile up the String constant pool, but unexpectedly failed. After JDK1.7, the constant pool is put into the heap and garbage collection can be carried out.

Then, in a different way, use cglib and use Class to fill up the old age.

Public static void testPergemOutOfMemory2 () {

Try {

While (true) {

Enhancer enhancer = new Enhancer ()

Enhancer.setSuperclass (OOM.class)

Enhancer.setUseCache (false)

Enhancer.setCallback (new MethodInterceptor () {

@ Override

Public Object intercept (Object obj, Method method, Object [] args, MethodProxy proxy) throws Throwable {

Return proxy.invokeSuper (obj, args)

}

});

Enhancer.create ()

}

}

Catch (Exception e) {

E.printStackTrace ()

}

}

If the virtual machine has successfully overflowed, can the classes generated by the JDK dynamic proxy overflow?

Public static void testPergemOutOfMemory3 () {

While (true) {

Final OOM oom = new OOM ()

Proxy.newProxyInstance (oom.getClass (). GetClassLoader (), oom.getClass (). GetInterfaces (), new InvocationHandler () {

Public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {

Object result = method.invoke (oom, args)

Return result

}

});

}

}

It turns out that the poor classes of the JDK dynamic proxy will not cause memory overflow because the class information generated by the JDK dynamic proxy will not be placed in the permanent generation, but in the heap.

Local method stack overflows public static void testNativeMethodOutOfMemory () {

Int j = 0

While (true) {

Printer.println (jacks +)

ExecutorService executors = Executors.newFixedThreadPool (50)

Int iTunes 0

While (iTunes +)

Tags: Memory continuous thread method case dynamic situation agent learning host code content that is constant form depth permanent warm intuitive over time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info OPPO Reno macOS Huawei NVidia