{"id":22399777,"url":"https://github.com/yaqinking/lmm","last_synced_at":"2025-03-27T00:16:41.747Z","repository":{"id":36269462,"uuid":"40573918","full_name":"yaqinking/LMM","owner":"yaqinking","description":"Learn memory management","archived":false,"fork":false,"pushed_at":"2015-08-12T12:19:21.000Z","size":140,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-01T05:41:30.808Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yaqinking.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-12T01:38:48.000Z","updated_at":"2015-08-12T11:54:34.000Z","dependencies_parsed_at":"2022-09-04T12:20:27.159Z","dependency_job_id":null,"html_url":"https://github.com/yaqinking/LMM","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaqinking%2FLMM","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaqinking%2FLMM/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaqinking%2FLMM/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaqinking%2FLMM/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yaqinking","download_url":"https://codeload.github.com/yaqinking/LMM/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245755679,"owners_count":20667027,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-05T08:09:59.643Z","updated_at":"2025-03-27T00:16:41.727Z","avatar_url":"https://github.com/yaqinking.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LMM\n學習一下 Objective-C 的內存管理 w\n\n原则\n看到 `alloc` `new` `retain` 就要 `release` or `autorelease`\n\n产生内存泄漏的原因是，对象并不再使用，然而 `retainCount` 不为 0。（也就是一直存在于内存中）\n\n防止内存泄漏的一个方法是，一个 `alloc` or `new` or `retain` 配对一个 `release` or `autolease` 使用。\n\n`@property` 默认生成简单 `setter` 和 `getter` 其修饰符的作用\n\n- 内存相关\n\n1. `assign` 直接赋值（应用于 基本数据类型 or 对象）\n2. `retain` 生成符合内存管理原则的 setter（应用于 对象）\n3. `copy`\n\n- 多线程相关\n\n1. `nonatomic` 不生成多线程相关的代码（iOS 中一般要声明成这个）\n2. `atomic` 生成 （默认生成的 w）\n\n- set get 生成相关\n\n1. `readonly` 只生成 `getter`\n2. `readwrite` 全部生成\n\n- set get 命名相关\n\n1. `setter` = `XXX`\n2. `getter` = `XXX`\n\n在用 `property` 时使用符合内存管理的修饰符后，在 MRC 中一定要在 `dealloc` 中 `release` 掉相应的对象。\n\n`release` 的作用是让 `retainCount` - 1 而 `autorelease` 的作用是把对象添加到 `autoreleasepool` 中去\n\n`autoreleasepool` 会在自身消失的时候，把添加到 `pool` 里的对象发送一次 or n次 `release` 消息（n次的来源是这个对象在 `autoreleasepool` 中发送了几次 `autorelease` 消息。）\n\nARC 指针类型 ARC -\u003e MRC 对应\n1. `strong` -\u003e 「`retain` (默认情况下生成的，应用于对象类型)」 只要有强指针指向对象，系统就不会回收对象\n2. `weak` -\u003e 「`assign` （应用于基本数据类型）」只要没有强指针指向对象，系统立即回收该对象\n \n当然虽然说的是对应，但不要混用 w\n\n在 ARC 下一般使用的有 `strong` `weak` （用于对象） `assign`（用于基本数据类型）\n\n当类中出现循环引用的时候，需要将一个的修饰符 `strong` 改为 `weak` 具体改哪个，看谁拥有谁，且在改为 `weak` 里面使用 `@class` 声明使用 `weak` 修饰的数据类型（类名），要调用方法的话，在 .m 文件中引入那个类的头文件。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyaqinking%2Flmm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyaqinking%2Flmm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyaqinking%2Flmm/lists"}