preface

The company’s project, there are many classes of 14 years of code, it can be said to witness the continuous brilliant project, the results of a search, some classes have not been called, it seems that the developer did not have the heart to delete, so it continues to now. I believe that many projects have this situation, either first comment out, or first put, and then worry about deleting, when the requirements come back, go to Git and slowly retrieve. But some are too long, and it’s time to cut them out.

Useless photos

LSUnusedResources

A Mac application for finding unused images and resources in Xcode projects.

There are 3.7K stars on Github. I first posted the address: github.com/tinymind/LS…

usage

Open LSUnusedResources, fill in the Project Path for the Project we want to Search, and select Search.

We can see the search results, unexpectedly 314 pages are not used, the size of about 3400KB, is it true? Let’s optimize.

Ignore similar name is chosen to match regular expressions that use resources, such as @ “image_%d” that Ignore images with suffixes.

Don’t assume that this is a guarantee that all images are useless, but we can basically delete images around these and optimize the 2M size.

FengNiao

FengNiao is a simple command-line utility for removing unused image resource files from your Xcode project.

There are 2.8K stars on Github, please first post the address: github.com/onevcat/Fen…

How to use it here? You can go to the link above, and it says how to use it. Here I posted a big guy wrote, can be used as a reference.

Juejin. Cn/post / 684490…

Useless code

LinkMap

This tool is specially used to analyze the LinkMap file of the project and get the space size (code segment + data segment) occupied by each class or library, which is convenient for developers to quickly locate the class or static library that needs to be optimized.

There are 1.6K stars on GitHub, which can be directly posted to github.com/huanxsd/Lin…

After we download it, we can run it and see the following interface.

steps

  1. openwrite link Map File.

  1. Open the ~ / Library/Developer/Xcode/DerivedData – XXXXXXXXXXXXX/Build/Intermediates / / XXX XXX. Build/Debug – iphoneos/XXX. Build/XXX – Lin KMap – normal – arm64. TXT.

You can see that the LinkMap file has three parts:

Object Files, which contains all the code project Files.

Sections describe the offset position and size of the code segment in the generated Mach-O.

Symbols, lists each method, class, block, and their size.

3. Open the TXT file using LinkMap and click Output.

The full library size and library name can be seen. Which libraries take up the most, we can see if there is room for optimization.

4. Use in combination with Mach-O

We open the xxx. app package and display the contents of the package. There are a lot of resource files in the package. We can also see there if there is any optimization space.

See this article: Mach-O Exploration for details.

There are three important ones:

__objc_classrefs keeps track of which classes are referenced. This keeps track of all instantiated classes. Some classes are in the package, but we don’t use them, so we don’t have them.

__objc_selrefs keeps track of which SEL strings are referenced, in both systematic and custom ways.

__objc_superrefs records the class that calls the super method.

appCode

Open appCode and select Code -> Inspect Code for static analysis.

Once the compilation is complete, we should see the following information

We’re looking at Code that is Unused, Unused Code.

  • Unused class: a class that is not used;

  • Unused global declaration: an Unused global declaration;

  • Unused import statement: The import statement of an Unused class;

  • Unused instance variable: a useless instance variable;

  • Unused local variable: a local variable that is Unused;

  • Unused macro: a useless macro;

  • 2. Unused method;

  • Unused parameter: an Unused parameter;

  • Unused property: a property of no use;

  • Unused value: Indicates a Unused value.

So this is a straightforward card, and it shows you what you didn’t use. Of course, we still need to check carefully in the project, so as not to delete by mistake.

Anyway, it’s time to optimize the code.