This article is about a thorough understanding of how references work in Android and Java.

For an Android developer, if you don't use WeakReferences, this is a problem.

To give a proper example, a few months ago, I released my last book, "Android High Performance," by co-author Diego Gincini. One of the most popular chapters is to discuss memory management for Android. In this chapter, we describe how memory works in mobile devices, how memory leaks occur, why this is important, and what techniques we can apply to avoid them. Since I have been developing Android, I have often seen such a tendency to despise or even ignore all problems related to memory leaks and memory management. I have already met the development needs. Why do you have to be self-interested? We are always eager to develop new features, and we would rather present something visible in the next Sprint demo, and we won't care about things that no one can see at a glance.

This is undoubtedly a living example of technical debt. I can even add that technical debt has some impact in the real world, which we can't measure with unit tests: disappointment, friction between developers, low-quality software and loss of enthusiasm. The reason why this effect is difficult to measure is that they often occur at some point in the long-term future. This is a bit like a politician: If I am only in politics for 8 years, why should I worry about what will happen in 12 years? Except in software development, everything goes in a faster way.

Writing design ideas that should be adopted in software development may require some big articles, and there are already many books and articles for your reference. However, a brief explanation of the different types of memory references, what they are, and how to use them in Android is a relatively short task, which I want to do in this article.

First of all: What is the reference in Java?

A reference points to an object that you can access by reference.

Java has four types of references by default: StrongReference, SoftReference, WeakReference, and PhantomReference. Some people think that there are only two types of references, strong references and weak references, while weak references have two levels of weakening. We are used to categorizing everything in life, and that perseverance is comparable to that of botanists. No matter which classification you think is better, you first need to understand these references. Then you can find out your own classification.

What do the various references mean?

StrongReference: Strong references are the most common type of reference in Java. At any time, when we create an object, a strong reference is also created. For example, when we do this:

MyObject object = new MyObject();

A new MyObject object is created, and a strong reference to it is stored in object. Are you still watching? Well, the more interesting thing is that this object can be forcibly arrived - meaning that it can be found through a series of strong references, which will prevent the garbage collection mechanism from reclaiming it, however, this is exactly what we want most. of. Now let's look at an example.

A thorough understanding of how references work in Android and Java

Take a few minutes and try to find a point where problems may occur.

What now?

The AsyncTask object is created and run in the AcTIvity onCreate() method. But there is a problem here: the inner class will access the outer class throughout its life cycle.

What happens if AcTIvity is destroyed? AsyncTask still holds a reference to AcTIvity, so AcTIvity is not reclaimable by the GC. This is what we call a memory leak.

Side note: I used to interview the right people. I asked them how to create a memory leak instead of asking the theoretical aspects of memory leaks. This is always more fun!

Memory leaks actually happen not only when the activity itself is destroyed, but also when the system needs more memory when the configuration changes (translator's note: for example, the horizontal screen switches to vertical screen). If the AsyncTask is complex (for example, holding a reference to a View on an Activity), it can even cause a crash because the view's reference is null. (Translator's Note: This is a literal translation, may not be easy for some students. I give an example, such as AsyncTask reference ProgressDialog, AsyncTask will display ProgressDialog when running, when the horizontal screen is cut into vertical screen At this time, there will be a crash. (╯^╰>)

So how do you prevent this from happening again? We next introduce another type of reference:

Galvanized Steel Pole


Product categories of Steel Tubular Pole, we are specialized manufacturers from China, galvanized steel poles, galvanized tubular poles suppliers/factory, wholesale high-quality products of Steel Tubular Pole Tower R & D and manufacturing, we have the perfect after-sales service and technical support. Look forward to your cooperation!

we also provide 30ft galvanized steel pole, hot dip galvanized pole, hot dip galvanized Steel Pole .


a) Since 2001

b) Location: located in the beach of scenic and rich Taihu Yixing with good transport service.

c) Total Staff: 136 people

d) Certificate: ISO 9001:2008, ISO 14001:2004, OHSMS idt OHSAS 18001 and 500KV power transmission line tower production license

e) Equipment: ranging from angle bar fabrication, plate fabrication, drilling hole, bending, gas/plasma cutting to welding. Besides, we can galvanize by ourselves with 2 Zinc Baths. Our annual maximum capacity amounts to 100,000 tons.

f) Anti-Corrosion: Hot-dip galvanization as per ISO 1461 or ASTM A123.

g) Welding: CO 2 welding or Submerged arc auto methods.

h) Products: power transmission steel tower, telecom steel tower, TV/radiobroadcasting steel tower, drilling well tower, and similar steel structures for substation,plant workshop etc.

i) Experiences: Bangladesh(Desco), Sudan, the Philippines(Transco), India(Reliance), SriLanka(Lanka Bell), Indonesia(PLN),Ghana, Sierra Leone, Tanzania, Ethiopia.

Our motto is: Quality is the life blood of the company, therefore, We have been making every effort to provide high quality Galvanizing Steel Pole.


Galvanized Steel Pole,Galvanized Steel Light Pole,Galvanized Steel Electric Pole,Galvanized Steel Street Pole,Galvanized Tubular Poles

YIXING FUTAO METAL STRUCTURAL UNIT CO.,LTD( YIXING HONGSHENGYUAN ELECTRIC POWER FACILITIES CO.,LTD.) , https://www.chinasteelpole.com