javascript - 基本反应本机应用程序中的高内存消耗
问题描述
我正在使我的第一个反应是原生的,尽管这很容易,但事实证明我很难理解我可能做错了什么。
首先,我有一个非常简单的应用程序,它通过 redux 获取数据
componentWillMount() {
this.props.fetchCoin()
this.props.CurrencyRate()
}
然后在它的返回部分呈现它
return (
<ScrollView>
<Header />
{/* Custom Search Input */}
<View>
<TextInput
style={textInput}
placeholder="Search Coin"
onChangeText={(text) => this.onSearch(text)} />
</View>
<View>
<FlatList
data={this.state.searchCoin ? displaySearchCrypto : this.props.cryptoLoaded}
renderItem={({ item }) => (
<CoinCard
key={item["long"]}
coinShortName = {item["short"]}
coinName = {item["long"]}
coinPrice = {item["price"].toFixed(2)}
marketCap = {(item["mktcap"]/1000000000).toFixed(4)}
percentChange = {item["perc"].toFixed(2)}
vwapData={item["vwapData"].toFixed(2)}
coinImage={"https://coincap.io/images/coins/" + item["long"] + ".png"}
/>
)}
/>
</View>
</ScrollView>
)
}
}
即使在应用程序获取数据并且 UI 线程保持在 60 (fps) 之后,我仍然看到我的 ram 消耗大于 500 MB,我认为 UI 中没有发生任何事情
而不是分享我所有的代码:你可以在 github 上找到大部分的东西上面的代码片段是从这里分享的:https ://github.com/irohitb/Crypto/blob/master/src/container/cryptoContainer.js
上面代码中的 CoinCard 组件可以在这里看到:https ://github.com/irohitb/Crypto/blob/master/src/components/CoinCard.js
同样的操作在这里:https ://github.com/irohitb/Crypto/blob/master/src/actions/cryptoAction.js
[问题:]有人可以帮我找出我做错了什么,我该如何解决?如果有人想尝试一下,这个存储库应该可以在您的模拟器上运行而不会引发错误。
解决方案
FlatList
withImage
在 Android 上有一个未解决的问题:https ://github.com/facebook/react-native/issues/13413 。这里有一些关于性能的提示:https ://github.com/filipemerker/flatlist-performance-tips/
作为修复的第一次尝试,请尝试添加removeClippedSubviews
到您的FlatList
.
我还在屏幕截图中注意到,您会收到有关丢失密钥的警告。解决该问题并尝试keyExtractor
看看是否可以改善情况。
或者,使用除 之外的其他内容FlatList
,例如ScrollView
。您将失去一些功能,FlatList
但如果您获得可接受的内存使用和性能,这可能是值得的。
推荐阅读
- git - 可以有 git 提交,指向同一棵树吗?
- nginx - 是否可以使用 nginx 对 fastcgi 请求进行字节范围缓存?
- python - 从UDF(numpy)错误-pyspark返回DenseVector
- asp.net - 在另一台机器上运行相同的应用程序时出现以下错误
- c# - 使用依赖属性解决“无法在静态上下文中访问非静态属性”的另一种方法
- r - 如果变量名称出现在列表中,则进行变异
- vba - VBA可以加载插件的所有引用吗
- vb.net - 使用列表从数据表中获取列
- tomcat - Tomcat9 相对于上下文路径的重写规则
- java - 无法使用可打开的嵌入式 postgresql 运行 flyways