Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to optimize memory

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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 +)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report