Recently, due to the technical reform, a lot of problems have occurred, the cache penetration mentioned in the previous article is just one of them, think about it, although they are relatively simple problems, but there should be a lot of people actually encountered, these problems seem to be very simple, but you should definitely step over.

= = and equals

As for the difference between equals and equals, I’m sure those of you who have done a bit of development for a year or two are well aware of it. However, this pit still occurs frequently in many development situations. Why? Because sometimes some students think there is no difference, just use ==, however, some unexpected things always come.

Not long ago, we had a small issue due to an online RPC frame switch.

Originally, the interface on the line was defined like this:

Then, an enumeration type is used in the interface query to get the enumeration value based on the ID, except that the == sign is used here.

The caller is written as follows:

Originally, this code ran online for two years, no problem, how suddenly not work?

However, after switching the frame, this interface reported an error. At that time, I also looked at this place for a long time, guessing that there is a problem here, but after thinking about it, it seems that it should not be ah.

In the end, it was found that valueOf was used in the transmission of the original RPC framework. The value was obtained from the cache and automatic packing and unpacking, so it could pass the test. However, the new framework uses New Byte(), so this old code will never pass because it is a new object.

Look at the results of this test.

Later, the Alibaba Java Coding Guidelines plug-in was installed to uniformly scan all codes, and another problem was found.

This is written differently. This enumeration simply defines code member variables as byte base types, not wrapper types. So the code is OK again with ==.

Imagine that, since it is an underlying data type, unboxing the == judgment is of course passed.

There are even more bizarre ways to write it, the member variable is a Byte wrapper, and getEnumByCode is the base type, but of course, this is also valid.

So, tired heart… .

Finally, I want to add a little more information about the underlying data type cache. The reason you can use == is also because it depends on the cache.

The data type Packing type Cache type Cache value range
byte Byte ByteCache – 128 ~ 127
short Short ShortCache – 128 ~ 127
int Integer IntegerCache – 128 ~ 127
long Long LongCache – 128 ~ 127
char Character CharacterCache 0 ~ 127

Finally, a word of advice: never, never use equals to determine the wrapper data type in your project, because even if you are sure that this code is correct right now, you never know what will happen next! Don’t take any chances.

Log played

Shortly after the project went online, it was found that the interface success rate directly dropped by 0 (the alarm monitoring of the drop of 0 must be there, otherwise I don’t know how to die). After checking for a long time, I found that everything else was normal. Finally, I found that the GC time increased rapidly. When I logged in to the server, I found that the hard disk was full.

Then look at the log decisively, because our hard drive is actually small, first suspect the log, sure enough, the log exploded. Run the ls-lHT command to check the file size.

After the rm -rf command is used to delete the disk, the disk space is not released. Normally this is not a problem, but if the file is locked or another process is writing data to the file it is.

In Linux, a file stored on the file system consists of two parts:

  1. Pointer part: The pointer is in the meta-data of the file system and is cleared from the meta-data after the data is deleted.
  2. Data part: The data part is stored on disk.

As in the above case, although service.log is deleted, the pointer part is not deleted from meta-data due to process lock, so we can see that the storage space is not freed.

There are two solutions:

  1. Use lsof – n | grep delete check what process in writing service. The log, is our Java process has been found by command write files, and then restart the application, directly by the background tool found that returned to normal after restart.

  2. To clear the log file, run the echo “”>/service.log command. This method immediately frees up disk space and the process continues writing to the log without being affected.

This article was automatically published by ArtiPub