- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章超全的iOS各种设备信息获取方法总结(包括iPhone8/iPhone X)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
前言 。
在开发中,有的时候为了统计用户信息、下发广告,服务器端往往需要手机用户设备及app的各种信息,这些信息的获取可以根据不同的设备或者app、系统版本来提供不同的功能或更好的用户体验,或者让开发者能更好的分析用户的问题原因.
下面讲述一下各种信息的获取方式:
点击下载以上展示效果的github源码 。
一行代码就搞定的统一来! 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
// 这个方法后面会列出来
nsstring *devicename = [self getdevicename];
nslog(@
"设备型号-->%@"
, devicename);
nsstring *iphonename = [uidevice currentdevice].name;
nslog(@
"iphone名称-->%@"
, iphonename);
nsstring *appverion = [[[nsbundle mainbundle] infodictionary] objectforkey:@
"cfbundleshortversionstring"
];
nslog(@
"app版本号-->%@"
, appverion);
cgfloat batterylevel = [[uidevice currentdevice] batterylevel];
nslog(@
"电池电量-->%f"
, batterylevel);
nsstring *localizedmodel = [uidevice currentdevice].localizedmodel;
nslog(@
"localizedmodel-->%@"
, localizedmodel);
nsstring *systemname = [uidevice currentdevice].systemname;
nslog(@
"当前系统名称-->%@"
, systemname);
nsstring *systemversion = [uidevice currentdevice].systemversion;
nslog(@
"当前系统版本号-->%@"
, systemversion);
struct
utsname systeminfo;
uname(&systeminfo);
nsstring *device_model = [nsstring stringwithcstring:systeminfo.machine encoding:nsutf8stringencoding];
nslog(@
"device_model-->%@"
, device_model);
// 这个方法后面会单独列出
nsstring *macaddress = [self getmacaddress];
nslog(@
"macaddress-->%@"
, macaddress);
// 这个方法后面会单独列出
nsstring *deviceip = [self getdeviceipaddresses];
nslog(@
"deviceip-->%@"
, deviceip);
// 设备上次重启的时间
nstimeinterval
time
= [[nsprocessinfo processinfo] systemuptime];
nsdate *lastrestartdate = [[nsdate alloc] initwithtimeintervalsincenow:(0 -
time
)];
|
广告位标识符:在同一个设备上的所有app都会取到相同的值,是苹果专门给各广告提供商用来追踪用户而设的,用户可以在 设置|隐私|广告追踪里重置此id的值,或限制此id的使用,故此id有可能会取不到值,但好在apple默认是允许追踪的,而且一般用户都不知道有这么个设置,所以基本上用来监测推广效果,是戳戳有余了 。
1
2
|
nsstring *idfa = [[[asidentifiermanager sharedmanager] advertisingidentifier] uuidstring];
nslog(@
"广告位标识符idfa-->%@"
, idfa);
|
uuid是universally unique identifier的缩写,中文意思是通用唯一识别码。它是让分布式系统中的所有元素,都能有唯一的辨识资讯,而不需要透过中央控制端来做辨识资讯的指 定。这样,每个人都可以建立不与其它人冲突的 uuid。在此情况下,就不需考虑数据库建立时的名称重复问题。苹果公司建议使用uuid为应用生成唯一标识字符串.
1
2
|
nsstring *
uuid
= [[[uidevice currentdevice] identifierforvendor] uuidstring];
nslog(@
"唯一识别码uuid-->%@"
,
uuid
);
|
获取设备型号 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
// 获取设备型号然后手动转化为对应名称
- (nsstring *)getdevicename
{
// 需要#import "sys/utsname.h"
#warning 题主呕心沥血总结!!最全面!亲测!全网独此一份!!
struct
utsname systeminfo;
uname(&systeminfo);
nsstring *devicestring = [nsstring stringwithcstring:systeminfo.machine encoding:nsutf8stringencoding];
if
([devicestring isequaltostring:@
"iphone3,1"
])
return
@
"iphone 4"
;
if
([devicestring isequaltostring:@
"iphone3,2"
])
return
@
"iphone 4"
;
if
([devicestring isequaltostring:@
"iphone3,3"
])
return
@
"iphone 4"
;
if
([devicestring isequaltostring:@
"iphone4,1"
])
return
@
"iphone 4s"
;
if
([devicestring isequaltostring:@
"iphone5,1"
])
return
@
"iphone 5"
;
if
([devicestring isequaltostring:@
"iphone5,2"
])
return
@
"iphone 5 (gsm+cdma)"
;
if
([devicestring isequaltostring:@
"iphone5,3"
])
return
@
"iphone 5c (gsm)"
;
if
([devicestring isequaltostring:@
"iphone5,4"
])
return
@
"iphone 5c (gsm+cdma)"
;
if
([devicestring isequaltostring:@
"iphone6,1"
])
return
@
"iphone 5s (gsm)"
;
if
([devicestring isequaltostring:@
"iphone6,2"
])
return
@
"iphone 5s (gsm+cdma)"
;
if
([devicestring isequaltostring:@
"iphone7,1"
])
return
@
"iphone 6 plus"
;
if
([devicestring isequaltostring:@
"iphone7,2"
])
return
@
"iphone 6"
;
if
([devicestring isequaltostring:@
"iphone8,1"
])
return
@
"iphone 6s"
;
if
([devicestring isequaltostring:@
"iphone8,2"
])
return
@
"iphone 6s plus"
;
if
([devicestring isequaltostring:@
"iphone8,4"
])
return
@
"iphone se"
;
// 日行两款手机型号均为日本独占,可能使用索尼felica支付方案而不是苹果支付
if
([devicestring isequaltostring:@
"iphone9,1"
])
return
@
"国行、日版、港行iphone 7"
;
if
([devicestring isequaltostring:@
"iphone9,2"
])
return
@
"港行、国行iphone 7 plus"
;
if
([devicestring isequaltostring:@
"iphone9,3"
])
return
@
"美版、台版iphone 7"
;
if
([devicestring isequaltostring:@
"iphone9,4"
])
return
@
"美版、台版iphone 7 plus"
;
if
([devicestring isequaltostring:@
"iphone10,1"
])
return
@
"国行(a1863)、日行(a1906)iphone 8"
;
if
([devicestring isequaltostring:@
"iphone10,4"
])
return
@
"美版(global/a1905)iphone 8"
;
if
([devicestring isequaltostring:@
"iphone10,2"
])
return
@
"国行(a1864)、日行(a1898)iphone 8 plus"
;
if
([devicestring isequaltostring:@
"iphone10,5"
])
return
@
"美版(global/a1897)iphone 8 plus"
;
if
([devicestring isequaltostring:@
"iphone10,3"
])
return
@
"国行(a1865)、日行(a1902)iphone x"
;
if
([devicestring isequaltostring:@
"iphone10,6"
])
return
@
"美版(global/a1901)iphone x"
;
if
([devicestring isequaltostring:@
"ipod1,1"
])
return
@
"ipod touch 1g"
;
if
([devicestring isequaltostring:@
"ipod2,1"
])
return
@
"ipod touch 2g"
;
if
([devicestring isequaltostring:@
"ipod3,1"
])
return
@
"ipod touch 3g"
;
if
([devicestring isequaltostring:@
"ipod4,1"
])
return
@
"ipod touch 4g"
;
if
([devicestring isequaltostring:@
"ipod5,1"
])
return
@
"ipod touch (5 gen)"
;
if
([devicestring isequaltostring:@
"ipad1,1"
])
return
@
"ipad"
;
if
([devicestring isequaltostring:@
"ipad1,2"
])
return
@
"ipad 3g"
;
if
([devicestring isequaltostring:@
"ipad2,1"
])
return
@
"ipad 2 (wifi)"
;
if
([devicestring isequaltostring:@
"ipad2,2"
])
return
@
"ipad 2"
;
if
([devicestring isequaltostring:@
"ipad2,3"
])
return
@
"ipad 2 (cdma)"
;
if
([devicestring isequaltostring:@
"ipad2,4"
])
return
@
"ipad 2"
;
if
([devicestring isequaltostring:@
"ipad2,5"
])
return
@
"ipad mini (wifi)"
;
if
([devicestring isequaltostring:@
"ipad2,6"
])
return
@
"ipad mini"
;
if
([devicestring isequaltostring:@
"ipad2,7"
])
return
@
"ipad mini (gsm+cdma)"
;
if
([devicestring isequaltostring:@
"ipad3,1"
])
return
@
"ipad 3 (wifi)"
;
if
([devicestring isequaltostring:@
"ipad3,2"
])
return
@
"ipad 3 (gsm+cdma)"
;
if
([devicestring isequaltostring:@
"ipad3,3"
])
return
@
"ipad 3"
;
if
([devicestring isequaltostring:@
"ipad3,4"
])
return
@
"ipad 4 (wifi)"
;
if
([devicestring isequaltostring:@
"ipad3,5"
])
return
@
"ipad 4"
;
if
([devicestring isequaltostring:@
"ipad3,6"
])
return
@
"ipad 4 (gsm+cdma)"
;
if
([devicestring isequaltostring:@
"ipad4,1"
])
return
@
"ipad air (wifi)"
;
if
([devicestring isequaltostring:@
"ipad4,2"
])
return
@
"ipad air (cellular)"
;
if
([devicestring isequaltostring:@
"ipad4,4"
])
return
@
"ipad mini 2 (wifi)"
;
if
([devicestring isequaltostring:@
"ipad4,5"
])
return
@
"ipad mini 2 (cellular)"
;
if
([devicestring isequaltostring:@
"ipad4,6"
])
return
@
"ipad mini 2"
;
if
([devicestring isequaltostring:@
"ipad4,7"
])
return
@
"ipad mini 3"
;
if
([devicestring isequaltostring:@
"ipad4,8"
])
return
@
"ipad mini 3"
;
if
([devicestring isequaltostring:@
"ipad4,9"
])
return
@
"ipad mini 3"
;
if
([devicestring isequaltostring:@
"ipad5,1"
])
return
@
"ipad mini 4 (wifi)"
;
if
([devicestring isequaltostring:@
"ipad5,2"
])
return
@
"ipad mini 4 (lte)"
;
if
([devicestring isequaltostring:@
"ipad5,3"
])
return
@
"ipad air 2"
;
if
([devicestring isequaltostring:@
"ipad5,4"
])
return
@
"ipad air 2"
;
if
([devicestring isequaltostring:@
"ipad6,3"
])
return
@
"ipad pro 9.7"
;
if
([devicestring isequaltostring:@
"ipad6,4"
])
return
@
"ipad pro 9.7"
;
if
([devicestring isequaltostring:@
"ipad6,7"
])
return
@
"ipad pro 12.9"
;
if
([devicestring isequaltostring:@
"ipad6,8"
])
return
@
"ipad pro 12.9"
;
if
([machinestring isequaltostring:@
"ipad6,11"
])
return
@
"ipad 5 (wifi)"
;
if
([machinestring isequaltostring:@
"ipad6,12"
])
return
@
"ipad 5 (cellular)"
;
if
([machinestring isequaltostring:@
"ipad7,1"
])
return
@
"ipad pro 12.9 inch 2nd gen (wifi)"
;
if
([machinestring isequaltostring:@
"ipad7,2"
])
return
@
"ipad pro 12.9 inch 2nd gen (cellular)"
;
if
([machinestring isequaltostring:@
"ipad7,3"
])
return
@
"ipad pro 10.5 inch (wifi)"
;
if
([machinestring isequaltostring:@
"ipad7,4"
])
return
@
"ipad pro 10.5 inch (cellular)"
;
if
([devicestring isequaltostring:@
"appletv2,1"
])
return
@
"apple tv 2"
;
if
([devicestring isequaltostring:@
"appletv3,1"
])
return
@
"apple tv 3"
;
if
([devicestring isequaltostring:@
"appletv3,2"
])
return
@
"apple tv 3"
;
if
([devicestring isequaltostring:@
"appletv5,3"
])
return
@
"apple tv 4"
;
if
([devicestring isequaltostring:@
"i386"
])
return
@
"simulator"
;
if
([devicestring isequaltostring:@
"x86_64"
])
return
@
"simulator"
;
return
devicestring;
}
|
获取 iphone 设备颜色/外壳颜色 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#warning 该方法是私有api,上线会被拒
// 由于每款手机同一颜色具体色号不一样,如 iphone 6和 iphone se粉色的两个具体色值是不一样的,因此在这里只能返回一个色号
uidevice *device = [uidevice currentdevice];
sel selector = nsselectorfromstring(@
"deviceinfoforkey:"
);
if
(![device respondstoselector:selector]) {
selector = nsselectorfromstring(@
"_deviceinfoforkey:"
);
}
if
([device respondstoselector:selector]) {
// 消除警告“performselector may cause a leak because its selector is unknown”
imp imp = [device methodforselector:selector];
nsstring * (*func)(id, sel, nsstring *) = (
void
*)imp;
nsstring *devicecolor = func(device, selector, @
"devicecolor"
);
nsstring *deviceenclosurecolor = func(device, selector, @
"deviceenclosurecolor"
);
nslog(@
"devicecolor --> %@ \n @"
deviceenclosurecolor --> %@ ", devicecolor, deviceenclosurecolor);
}
|
这里是一些已知的设备颜色 。
mac 地址 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
- (nsstring *)getmacaddress {
int
mib[6];
size_t
len;
char
*buf;
unsigned
char
*ptr;
struct
if_msghdr *ifm;
struct
sockaddr_dl *sdl;
mib[0] = ctl_net;
mib[1] = af_route;
mib[2] = 0;
mib[3] = af_link;
mib[4] = net_rt_iflist;
if
((mib[5] = if_nametoindex(
"en0"
)) == 0) {
printf
(
"error: if_nametoindex error/n"
);
return
null;
}
if
(sysctl(mib, 6, null, &len, null, 0) < 0) {
printf
(
"error: sysctl, take 1/n"
);
return
null;
}
if
((buf =
malloc
(len)) == null) {
printf
(
"could not allocate memory. error!/n"
);
return
null;
}
if
(sysctl(mib, 6, buf, &len, null, 0) < 0) {
printf
(
"error: sysctl, take 2"
);
return
null;
}
ifm = (
struct
if_msghdr *)buf;
sdl = (
struct
sockaddr_dl *)(ifm + 1);
ptr = (unsigned
char
*)lladdr(sdl);
nsstring *outstring = [nsstring stringwithformat:@
"%02x%02x%02x%02x%02x%02x"
, *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
free
(buf);
return
[outstring uppercasestring];
}
|
ip地址 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
- (nsstring *)getdeviceipaddresses {
int
sockfd = socket(af_inet, sock_dgram, 0);
nsmutablearray *ips = [nsmutablearray array];
int
buffersize = 4096;
struct
ifconf ifc;
char
buffer[buffersize], *ptr, lastname[ifnamsiz], *cptr;
struct
ifreq *ifr, ifrcopy;
ifc.ifc_len = buffersize;
ifc.ifc_buf = buffer;
if
(ioctl(sockfd, siocgifconf, &ifc) >= 0){
for
(ptr = buffer; ptr < buffer + ifc.ifc_len; ){
ifr = (
struct
ifreq *)ptr;
int
len =
sizeof
(
struct
sockaddr);
if
(ifr->ifr_addr.sa_len > len) {
len = ifr->ifr_addr.sa_len;
}
ptr +=
sizeof
(ifr->ifr_name) + len;
if
(ifr->ifr_addr.sa_family != af_inet)
continue
;
if
((cptr = (
char
*)
strchr
(ifr->ifr_name,
':'
)) != null) *cptr = 0;
if
(
strncmp
(lastname, ifr->ifr_name, ifnamsiz) == 0)
continue
;
memcpy
(lastname, ifr->ifr_name, ifnamsiz);
ifrcopy = *ifr;
ioctl(sockfd, siocgifflags, &ifrcopy);
if
((ifrcopy.ifr_flags & iff_up) == 0)
continue
;
nsstring *ip = [nsstring stringwithformat:@
"%s"
, inet_ntoa(((
struct
sockaddr_in *)&ifr->ifr_addr)->sin_addr)];
[ips addobject:ip];
}
}
close(sockfd);
nsstring *deviceip = @
""
;
for
(
int
i=0; i < ips.count; i++) {
if
(ips.count > 0) {
deviceip = [nsstring stringwithformat:@
"%@"
,ips.lastobject];
}
}
return
deviceip;
}
|
cpu 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
// cpu总数目
- (nsuinteger)getcpucount {
return
[nsprocessinfo processinfo].activeprocessorcount;
}
// 已使用的cpu比例
- (
float
)getcpuusage {
float
cpu = 0;
nsarray *cpus = [self getpercpuusage];
if
(cpus.count == 0)
return
-1;
for
(nsnumber *n in cpus) {
cpu += n.floatvalue;
}
return
cpu;
}
// 获取每个cpu的使用比例
- (nsarray *)getpercpuusage {
processor_info_array_t _cpuinfo, _prevcpuinfo = nil;
mach_msg_type_number_t _numcpuinfo, _numprevcpuinfo = 0;
unsigned _numcpus;
nslock *_cpuusagelock;
int
_mib[2u] = { ctl_hw, hw_ncpu };
size_t
_sizeofnumcpus =
sizeof
(_numcpus);
int
_status = sysctl(_mib, 2u, &_numcpus, &_sizeofnumcpus, null, 0u);
if
(_status)
_numcpus = 1;
_cpuusagelock = [[nslock alloc] init];
natural_t _numcpusu = 0u;
kern_return_t err = host_processor_info(mach_host_self(), processor_cpu_load_info, &_numcpusu, &_cpuinfo, &_numcpuinfo);
if
(err == kern_success) {
[_cpuusagelock lock];
nsmutablearray *cpus = [nsmutablearray
new
];
for
(unsigned i = 0u; i < _numcpus; ++i) {
float32 _inuse, _total;
if
(_prevcpuinfo) {
_inuse = (
(_cpuinfo[(cpu_state_max * i) + cpu_state_user] - _prevcpuinfo[(cpu_state_max * i) + cpu_state_user])
+ (_cpuinfo[(cpu_state_max * i) + cpu_state_system] - _prevcpuinfo[(cpu_state_max * i) + cpu_state_system])
+ (_cpuinfo[(cpu_state_max * i) + cpu_state_nice] - _prevcpuinfo[(cpu_state_max * i) + cpu_state_nice])
);
_total = _inuse + (_cpuinfo[(cpu_state_max * i) + cpu_state_idle] - _prevcpuinfo[(cpu_state_max * i) + cpu_state_idle]);
}
else
{
_inuse = _cpuinfo[(cpu_state_max * i) + cpu_state_user] + _cpuinfo[(cpu_state_max * i) + cpu_state_system] + _cpuinfo[(cpu_state_max * i) + cpu_state_nice];
_total = _inuse + _cpuinfo[(cpu_state_max * i) + cpu_state_idle];
}
[cpus addobject:@(_inuse / _total)];
}
[_cpuusagelock unlock];
if
(_prevcpuinfo) {
size_t
prevcpuinfosize =
sizeof
(integer_t) * _numprevcpuinfo;
vm_deallocate(mach_task_self(), (vm_address_t)_prevcpuinfo, prevcpuinfosize);
}
return
cpus;
}
else
{
return
nil;
}
}
|
disk磁盘空间 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
// 获取磁盘总空间
- (int64_t)gettotaldiskspace {
nserror *error = nil;
nsdictionary *attrs = [[nsfilemanager defaultmanager] attributesoffilesystemforpath:nshomedirectory() error:&error];
if
(error)
return
-1;
int64_t space = [[attrs objectforkey:nsfilesystemsize] longlongvalue];
if
(space < 0) space = -1;
return
space;
}
// 获取未使用的磁盘空间
- (int64_t)getfreediskspace {
nserror *error = nil;
nsdictionary *attrs = [[nsfilemanager defaultmanager] attributesoffilesystemforpath:nshomedirectory() error:&error];
if
(error)
return
-1;
int64_t space = [[attrs objectforkey:nsfilesystemfreesize] longlongvalue];
if
(space < 0) space = -1;
return
space;
}
// 获取已使用的磁盘空间
- (int64_t)getuseddiskspace {
int64_t totaldisk = [self gettotaldiskspace];
int64_t freedisk = [self getfreediskspace];
if
(totaldisk < 0 || freedisk < 0)
return
-1;
int64_t useddisk = totaldisk - freedisk;
if
(useddisk < 0) useddisk = -1;
return
useddisk;
}
|
memory内存相关数据 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
// 系统总内存空间
- (int64_t)gettotalmemory {
int64_t totalmemory = [[nsprocessinfo processinfo] physicalmemory];
if
(totalmemory < -1) totalmemory = -1;
return
totalmemory;
}
// 活跃的内存,正在使用或者很短时间内被使用过
- (int64_t)getactivememory {
mach_port_t host_port = mach_host_self();
mach_msg_type_number_t host_size =
sizeof
(vm_statistics_data_t) /
sizeof
(integer_t);
vm_size_t page_size;
vm_statistics_data_t vm_stat;
kern_return_t kern;
kern = host_page_size(host_port, &page_size);
if
(kern != kern_success)
return
-1;
kern = host_statistics(host_port, host_vm_info, (host_info_t)&vm_stat, &host_size);
if
(kern != kern_success)
return
-1;
return
vm_stat.active_count * page_size;
}
// 最近使用过,但是目前处于不活跃状态的内存
- (int64_t)getinactivememory {
mach_port_t host_port = mach_host_self();
mach_msg_type_number_t host_size =
sizeof
(vm_statistics_data_t) /
sizeof
(integer_t);
vm_size_t page_size;
vm_statistics_data_t vm_stat;
kern_return_t kern;
kern = host_page_size(host_port, &page_size);
if
(kern != kern_success)
return
-1;
kern = host_statistics(host_port, host_vm_info, (host_info_t)&vm_stat, &host_size);
if
(kern != kern_success)
return
-1;
return
vm_stat.inactive_count * page_size;
}
// 空闲的内存空间
- (int64_t)getfreememory {
mach_port_t host_port = mach_host_self();
mach_msg_type_number_t host_size =
sizeof
(vm_statistics_data_t) /
sizeof
(integer_t);
vm_size_t page_size;
vm_statistics_data_t vm_stat;
kern_return_t kern;
kern = host_page_size(host_port, &page_size);
if
(kern != kern_success)
return
-1;
kern = host_statistics(host_port, host_vm_info, (host_info_t)&vm_stat, &host_size);
if
(kern != kern_success)
return
-1;
return
vm_stat.free_count * page_size;
}
// 已使用的内存空间
- (int64_t)getusedmemory {
mach_port_t host_port = mach_host_self();
mach_msg_type_number_t host_size =
sizeof
(vm_statistics_data_t) /
sizeof
(integer_t);
vm_size_t page_size;
vm_statistics_data_t vm_stat;
kern_return_t kern;
kern = host_page_size(host_port, &page_size);
if
(kern != kern_success)
return
-1;
kern = host_statistics(host_port, host_vm_info, (host_info_t)&vm_stat, &host_size);
if
(kern != kern_success)
return
-1;
return
page_size * (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count);
}
// 用来存放内核和数据结构的内存,framework、用户级别的应用无法分配
- (int64_t)getwiredmemory {
mach_port_t host_port = mach_host_self();
mach_msg_type_number_t host_size =
sizeof
(vm_statistics_data_t) /
sizeof
(integer_t);
vm_size_t page_size;
vm_statistics_data_t vm_stat;
kern_return_t kern;
kern = host_page_size(host_port, &page_size);
if
(kern != kern_success)
return
-1;
kern = host_statistics(host_port, host_vm_info, (host_info_t)&vm_stat, &host_size);
if
(kern != kern_success)
return
-1;
return
vm_stat.wire_count * page_size;
}
// 可释放的内存空间:内存吃紧自动释放,针对大对象存放所需的大块内存空间
- (int64_t)getpurgablememory {
mach_port_t host_port = mach_host_self();
mach_msg_type_number_t host_size =
sizeof
(vm_statistics_data_t) /
sizeof
(integer_t);
vm_size_t page_size;
vm_statistics_data_t vm_stat;
kern_return_t kern;
kern = host_page_size(host_port, &page_size);
if
(kern != kern_success)
return
-1;
kern = host_statistics(host_port, host_vm_info, (host_info_t)&vm_stat, &host_size);
if
(kern != kern_success)
return
-1;
return
vm_stat.purgeable_count * page_size;
}
|
总结 。
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对我的支持.
原文链接:http://www.jianshu.com/p/b23016bb97af 。
最后此篇关于超全的iOS各种设备信息获取方法总结(包括iPhone8/iPhone X)的文章就讲到这里了,如果你想了解更多关于超全的iOS各种设备信息获取方法总结(包括iPhone8/iPhone X)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
1、定义 设 \(u\) 和 \(v\) 为一张图上的任意两个节点。令 \(c(u, v)\) 为它们之间的边的容量, \(f(u, v)\) 为它们之间的流量,则需要满足以
1、前言 工作中涉及到文件系统,有时候需要判断文件和目录是否存在。我结合apue第四章文件和目录,总结一下如何正确判断文件和目录是否存在,方便以后查询。 2、stat系列函数 stat函数用来
并查集(Union-Find Set): 一种用于管理分组的数据结构。它具备两个操作:(1)查询元素a和元素b是否为同一组 (2) 将元素a和b合并为同一组。 注意:并查集不能将在同一组的元素拆
当下,注解非常流行,以前很长篇的代码,现在基本上一个注解就能搞定。 那,在Mybatis中又有哪些注解呢? Mybatis中的注解基本上都在org.apache.ibatis.annotat
指针操作数组,方法一是p+index,方法二是p[index],第二种方法跟数组访问方法是一样的。 数组引用返回的是数组的第一个元素的指针地址。 可以将指针指向数组的任意元素,然后从那里开始访问
通常部署完php环境后会进行一些安全设置,除了熟悉各种php漏洞外,还可以通过配置php.ini来加固PHP的运行环境,PHP官方也曾经多次修改php.ini的默认设置。 下面对php.ini中一
在JavaScript中,使用typeof可以检测基本数据类型,使用instanceof可以检测引用数据类型。在PHP中,也有检测数据类型的方法,具体如下: 1、输出变量的数据类型(gettype
把图片缓存到本地,在很多场景都会用到,如果只是存储文件信息,那建一个plist文件,或者数据库就能很方便的解决问题,但是如果存储图片到沙盒就没那么方便了。这里简单介绍两种保存图片到沙盒的方法。
(1)需要安装docker容器,在docker容器内安装jenkins,gogs,tomcat。 新建maven项目,添加findbugs plugin。 使用docker
今天主题是实现并发服务器,实现方法有多种版本,先从简单的单进程代码实现到多进程,多线程的实现,最终引入一些高级模块来实现并发TCP服务器。 说到TCP,想起吐槽大会有个段子提到三次握手,也只有程序
如下所示: Ctrl+1或F2快速修复 Ctrl+D快捷删除行 Shift+Enter 快速切换到下一行,在本行的任何位置都可 Ctrl+F11快速运行代码 Alt+上下键 快速移动行(可
JSP是Servlet技术的扩展,本质上是Servlet的简易方式,更强调应用的外表表达。 JSP编译后是”类servlet”。 Servlet和JSP最主要的不同点在于,Servlet的应用逻辑
Java中的Runable,Callable,Future,FutureTask,ExecutorService,Excetor,Excutors,ThreadPoolExcetor在这里对这些关键
读取Java文件到byte数组的三种方法(总结) ? 1
用java实现的数组创建二叉树以及递归先序遍历,递归中序遍历,递归后序遍历,非递归前序遍历,非递归中序遍历,非递归后序遍历,深度优先遍历,广度优先遍历8种遍历方式:
1、简明总结 ASCII(char) 返回字符的ASCII码值 BIT_LENGTH(str) 返回字符串的比特长度 CONCAT(s1,s2…,sn)
java应用服务器(web server),是指运行java程序的web应用服务器软件,不包括nginx、Apache等通用web服务器软件。 一、Tomcat Tomcat是Apache 软件基
事务作为抽象层,允许应用忽略DB 内部一些复杂并发问题和某些硬件、软件故障,简化应用层的处理逻辑:事务中止(transaction abort),而应用仅需重试。对复杂访问模式,事务可大大减少需要考虑
我们在本教程学习了如何描述 XML 文档的结构 我们学习到了如何使用 DTD 来定义一个 XML 文档的合法元素,以及如何在我们的 XML 内部或者作为一个外部引用来声明 DTD 我们学习了如何为
在这个XPath 基础教程中我们讲解了如何在 XML 文档中查找信息 我们可以使用 XPath 的元素和属性在 XML 文档中进行导航 我们也学习了如何使用 XPath 中内建的某些标准函数 如
我是一名优秀的程序员,十分优秀!