测试提交

This commit is contained in:
qiuqiu 2025-03-26 17:54:55 +08:00
parent 6837ced1a1
commit c6c5bccc91
13 changed files with 312 additions and 4 deletions

38
.idea/workspace.xml generated
View File

@ -6,7 +6,11 @@
</configurations> </configurations>
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="fec10672-acda-4616-894b-a4b6f93aea6f" name="Default Changelist" comment="" /> <list default="true" id="fec10672-acda-4616-894b-a4b6f93aea6f" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/笔记文件/日记/2025_03_23_星期日.md" beforeDir="false" afterPath="$PROJECT_DIR$/笔记文件/日记/2025_03_23_星期日.md" afterDir="false" />
<change beforePath="$PROJECT_DIR$/笔记文件/日记/2025_03_26_星期三.md" beforeDir="false" afterPath="$PROJECT_DIR$/笔记文件/日记/2025_03_26_星期三.md" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
@ -33,7 +37,7 @@
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" /> <property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" /> <property name="WebServerToolWindowFactoryState" value="false" />
<property name="cf.first.check.clang-format" value="false" /> <property name="cf.first.check.clang-format" value="false" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" /> <property name="last_opened_file_path" value="$PROJECT_DIR$/../code/git/plink" />
</component> </component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" /> <component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager"> <component name="TaskManager">
@ -54,7 +58,35 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1742956649478</updated> <updated>1742956649478</updated>
</task> </task>
<option name="localTasksCounter" value="2" /> <task id="LOCAL-00002" summary="测试提交">
<created>1742957284781</created>
<option name="number" value="00002" />
<option name="presentableId" value="LOCAL-00002" />
<option name="project" value="LOCAL" />
<updated>1742957284781</updated>
</task>
<task id="LOCAL-00003" summary="测试提交">
<created>1742957291895</created>
<option name="number" value="00003" />
<option name="presentableId" value="LOCAL-00003" />
<option name="project" value="LOCAL" />
<updated>1742957291895</updated>
</task>
<task id="LOCAL-00004" summary="测试提交">
<created>1742958527917</created>
<option name="number" value="00004" />
<option name="presentableId" value="LOCAL-00004" />
<option name="project" value="LOCAL" />
<updated>1742958527917</updated>
</task>
<task id="LOCAL-00005" summary="测试提交">
<created>1742960760863</created>
<option name="number" value="00005" />
<option name="presentableId" value="LOCAL-00005" />
<option name="project" value="LOCAL" />
<updated>1742960760863</updated>
</task>
<option name="localTasksCounter" value="6" />
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">

View File

@ -0,0 +1,3 @@
#unity/日常积累
参考链接https://www.cnblogs.com/zouqiang/p/9936104.html

View File

@ -0,0 +1,249 @@
#ios
#unity/日常积累
ios相关逻辑
头文件:
## MemoryInfoPlugin.h
``` c
#import <Foundation/Foundation.h>
@interface MemoryInfoPlugin : NSObject
+ (NSString *)getMemoryInfo;
+ (double)getUsedMemoryMB;
+ (double)getTotalMemoryMB;
+ (double)getMemoryUsagePercentage;
@end
```
## MemoryInfoPlugin.mm
``` cpp
#import "MemoryInfoPlugin.h"
#import <mach/mach.h>
#import <sys/sysctl.h>
@implementation MemoryInfoPlugin
+ (NSString *)getMemoryInfo {
double used = [self getUsedMemoryMB];
double total = [self getTotalMemoryMB];
double percentage = [self getMemoryUsagePercentage];
return [NSString stringWithFormat:@"已使用: %.2f MB, 总共: %.2f MB, 使用率: %.2f%%",
used, total, percentage];
}
+ (double)getUsedMemoryMB {
task_vm_info_data_t taskInfo;
mach_msg_type_number_t infoCount = TASK_VM_INFO_COUNT;
kern_return_t kernReturn = task_info(mach_task_self(),
TASK_VM_INFO,
(task_info_t)&taskInfo,
&infoCount);
if (kernReturn != KERN_SUCCESS) {
return -1.0;
}
return (double)taskInfo.phys_footprint / 1024.0 / 1024.0;
}
+ (double)getTotalMemoryMB {
int64_t memorySize = 0;
size_t size = sizeof(memorySize);
sysctlbyname("hw.memsize", &memorySize, &size, NULL, 0);
return (double)memorySize / 1024.0 / 1024.0;
}
+ (double)getMemoryUsagePercentage {
double used = [self getUsedMemoryMB];
double total = [self getTotalMemoryMB];
if (total > 0) {
return (used / total) * 100.0;
}
return -1.0;
}
// 为Unity导出C函数
extern "C" {
const char* _GetMemoryInfo() {
NSString *info = [MemoryInfoPlugin getMemoryInfo];
return strdup([info UTF8String]);
}
double _GetUsedMemoryMB() {
return [MemoryInfoPlugin getUsedMemoryMB];
}
double _GetTotalMemoryMB() {
return [MemoryInfoPlugin getTotalMemoryMB];
}
double _GetMemoryUsagePercentage() {
return [MemoryInfoPlugin getMemoryUsagePercentage];
}
}
@end
```
调用 测试用例
## IOSMemoryInfo.cs
``` cs
using System.Runtime.InteropServices;
using UnityEngine;
public class IOSMemoryInfo : MonoBehaviour
{
#if UNITY_IOS && !UNITY_EDITOR
[DllImport("__Internal")]
private static extern string _GetMemoryInfo();
[DllImport("__Internal")]
private static extern double _GetUsedMemoryMB();
[DllImport("__Internal")]
private static extern double _GetTotalMemoryMB();
[DllImport("__Internal")]
private static extern double _GetMemoryUsagePercentage();
#endif
// 获取完整内存信息字符串
public static string GetMemoryInfo()
{
#if UNITY_IOS && !UNITY_EDITOR
return _GetMemoryInfo();
#else
return "仅支持iOS设备";
#endif
}
// 获取已使用内存(MB)
public static double GetUsedMemoryMB()
{
#if UNITY_IOS && !UNITY_EDITOR
return _GetUsedMemoryMB();
#else
return -1;
#endif
}
// 获取总内存(MB)
public static double GetTotalMemoryMB()
{
#if UNITY_IOS && !UNITY_EDITOR
return _GetTotalMemoryMB();
#else
return -1;
#endif
}
// 获取内存使用百分比
public static double GetMemoryUsagePercentage()
{
#if UNITY_IOS && !UNITY_EDITOR
return _GetMemoryUsagePercentage();
#else
return -1;
#endif
}
// 使用示例
void Start()
{
InvokeRepeating("ShowMemoryInfo", 1.0f, 5.0f);
}
void ShowMemoryInfo()
{
Debug.Log("内存信息: " + GetMemoryInfo());
Debug.Log("已使用: " + GetUsedMemoryMB() + " MB");
Debug.Log("总内存: " + GetTotalMemoryMB() + " MB");
Debug.Log("使用率: " + GetMemoryUsagePercentage() + "%");
}
}
```
相关目录结构:
![[Pasted image 20250326153420.png]]
![[Pasted image 20250326153443.png]]
最终xcode运行效果
![[Pasted image 20250326153542.png]]
## 相关逻辑解析
这个获取iOS内存占用的方法有效主要基于以下几个关键点
1. Mach API的使用
```
task_info(mach_task_self(), TASK_VM_INFO, ...)
```
这是iOS/macOS底层Mach内核提供的API可以直接访问进程的内存信息。Mach是XNU内核的核心组件专门负责内存管理等基础服务。
2. phys_footprint字段
```
taskInfo.phys_footprint
```
这是Apple在iOS 9+引入的专用字段,表示:
- 实际物理内存使用量(非虚拟内存)
- 包含压缩内存Compressed Memory
- 包含IOKit使用的内存
- 符合Xcode Memory Report的统计标准
3. 与系统监控的一致性:
这个数值与Xcode Debug Gauges显示的内存值完全一致是Apple官方推荐的内存统计方式。相比过时的resident_size字段phys_footprint能更准确地反映真实内存使用情况。
4. 权限处理:
```
mach_task_self()
```
通过获取当前进程的task端口避免了越权访问的问题符合iOS的沙盒安全机制。
5. 单位转换:
```
/ 1024.0 / 1024.0
```
将字节转换为MB的经典处理方式1MB = 1024KB = 10241024B
需要注意的版本适配:
- phys_footprint在iOS 9.0+可用
- 如果需支持更老版本,需要添加条件编译:
``` cpp
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
return taskInfo.phys_footprint;
#else
return taskInfo.internal + taskInfo.compressed;
#endif
```
这个实现方式是目前iOS平台最准确可靠的内存统计方案被主流性能监控工具广泛采用。
也可配合[[xcode性能检测工具]]确认性能数据,是否准确

View File

@ -0,0 +1,17 @@
#ios
性能检测工具,入口:
![[Pasted image 20250326153657.png]]
会出现xcode的性能监控窗体例如这里
![[Pasted image 20250326153819.png]]
直接点击运行,开启监听即可
![[Pasted image 20250326153912.png]]
可以看到对应app的内存占用等信息
![[Pasted image 20250326154017.png]]

View File

@ -30,7 +30,7 @@
- [ ] 完善一下 埋点中台 的笔记整理 - [ ] 完善一下 埋点中台 的笔记整理
- [x] 明天关注一下 adjust埋点数据的问题 - [x] 明天关注一下 adjust埋点数据的问题
- [x] 明天记得完善一下 OKR 任务目标 - [x] 明天记得完善一下 OKR 任务目标
- [ ] 明天记得完善一下 周报Q1的详细记录 - [x] 明天记得完善一下 周报Q1的详细记录
--- ---
[[git忽略文件夹]] [[git忽略文件夹]]
# Journal # Journal

View File

@ -24,8 +24,15 @@
- [ ] 家里下载一下Clion作为版本管理 - [ ] 家里下载一下Clion作为版本管理
- [ ] 今天挑一下衣服 - [ ] 今天挑一下衣服
- [x] 升级一下 中台库 震动插件 - [x] 升级一下 中台库 震动插件
- [ ] 记得提醒一下 X项目组 AndroidManifest把debug模式改掉
- [ ] 处理一下c编译生成.so和.a相关
--- ---
[[git排除 排除CRLF行尾差异]] [[git排除 排除CRLF行尾差异]]
[[gitattributes作用]] [[gitattributes作用]]
[[Clion版本参考]] [[Clion版本参考]]
[[C、C++打包成.dll .so .a 给Unity使用]]
[[ios获取内存相关]]
[[xcode性能检测工具]]
[[清空cmd指令 清屏]]
[[工作占比]]
# Journal # Journal

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 294 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 260 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 258 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 97 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 292 KiB