1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/sys_info.h"
7 #include <sys/system_properties.h>
9 #include "base/logging.h"
10 #include "base/string_number_conversions.h"
11 #include "base/string_piece.h"
15 int ParseHeapSize(const base::StringPiece
& str
) {
16 const int64 KB
= 1024;
17 const int64 MB
= 1024 * KB
;
18 const int64 GB
= 1024 * MB
;
19 CHECK_GT(str
.size(), 0u);
21 size_t length
= str
.size();
22 if (str
[length
- 1] == 'k') {
25 } else if (str
[length
- 1] == 'm') {
28 } else if (str
[length
- 1] == 'g') {
32 CHECK('0' <= str
[length
- 1] && str
[length
- 1] <= '9');
35 bool parsed
= base::StringToInt64(str
.substr(0, length
), &result
);
37 result
= result
* factor
/ MB
;
38 // dalvik.vm.heapsize property is writable by user,
39 // truncate it to reasonable value to avoid overflows later.
40 result
= std::min
<int64
>(std::max
<int64
>(32, result
), 1024);
41 return static_cast<int>(result
);
44 int GetDalvikHeapSizeMB() {
45 char heap_size_str
[PROP_VALUE_MAX
];
46 __system_property_get("dalvik.vm.heapsize", heap_size_str
);
47 return ParseHeapSize(heap_size_str
);
50 } // anonymous namespace
54 int SysInfo::DalvikHeapSizeMB() {
55 static int heap_size
= GetDalvikHeapSizeMB();