【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android Web3j OOM解決詳解_第1頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android Web3j OOM解決詳解_第2頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android Web3j OOM解決詳解_第3頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android Web3j OOM解決詳解_第4頁
免費(fèi)預(yù)覽已結(jié)束,剩余1頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

【移動(dòng)應(yīng)用開發(fā)技術(shù)】AndroidWeb3jOOM解決詳解

在Android客戶端使用Web3j創(chuàng)建錢包、導(dǎo)入錢包時(shí)都可能會(huì)產(chǎn)生OOM,相關(guān)issue在Github上已經(jīng)有所提及:/web3j/web3j/issues/299。這個(gè)問題在Web3j3.0版本以前是沒有的,由于新版的Web3j使用spongycastle庫替換了lambdaworks庫,雖然在效率上提升了速度,但存在Android端的兼容性問題。本項(xiàng)目代碼地址:/uncleleonfan/WalletOOM.git創(chuàng)建錢包OOM解決

在創(chuàng)建錢包時(shí),如果創(chuàng)建一個(gè)FullWallet,則會(huì)導(dǎo)致OOM:Log如下:

"Causedby:java.lang.OutOfMemoryError:Failedtoallocatea1036byteallocationwith16777216freebytesand48MBuntilOOM;failedduetofragmentation(requiredcontinguousfree16384bytesforanewbufferwherelargestcontiguousfree8192bytes)",

"\tatorg.spongycastle.util.Arrays.clone(Arrays.java:602)",

"\tatorg.spongycastle.crypto.generators.SCrypt.SMix(SCrypt.java:126)",

"\tatorg.spongycastle.crypto.generators.SCrypt.MFcrypt(SCrypt.java:87)",

"\tatorg.spongycastle.crypto.generators.SCrypt.generate(SCrypt.java:66)",

"\tatorg.web3j.crypto.Wallet.generateDerivedScryptKey(Wallet.java:136)",

"\tatorg.web3j.crypto.Wallet.create(Wallet.java:74)",

"\tatorg.web3j.crypto.Wallet.createStandard(Wallet.java:93)",

"\tatorg.web3j.crypto.WalletUtils.generateWalletFile(WalletUtils.java:61)"

"Causedby:java.lang.OutOfMemoryError:Failedtoallocatea1036byteallocationwith16777216freebytesand48MBuntilOOM;failedduetofragmentation(requiredcontinguousfree16384bytesforanewbufferwherelargestcontiguousfree8192bytes)",

"\tatorg.spongycastle.util.Arrays.clone(Arrays.java:602)",

"\tatorg.spongycastle.crypto.generators.SCrypt.SMix(SCrypt.java:126)",

"\tatorg.spongycastle.crypto.generators.SCrypt.MFcrypt(SCrypt.java:87)",

"\tatorg.spongycastle.crypto.generators.SCrypt.generate(SCrypt.java:66)",

"\tatorg.web3j.crypto.Wallet.generateDerivedScryptKey(Wallet.java:136)",

"\tatorg.web3j.crypto.Wallet.create(Wallet.java:74)",

"\tatorg.web3j.crypto.Wallet.createStandard(Wallet.java:93)",

"\tatorg.web3j.crypto.WalletUtils.generateWalletFile(WalletUtils.java:61)"

generateFullNewWalletFile里面會(huì)調(diào)用createStandard創(chuàng)建錢包,使用N_STANDARD,P_STANDARD來配置加密強(qiáng)度,直接影響需使用的內(nèi)存大小,最終導(dǎo)致OOM的發(fā)生。解決方法非常簡單,創(chuàng)建一個(gè)LightWallet即可:generateLightNewWalletFile會(huì)調(diào)用createLight來創(chuàng)建一個(gè)輕錢包,使用N_LIGHT,P_LIGHT,他們在數(shù)值上相對較小,所以不會(huì)OOM。我們可以對比一下N_STANDARD和P_STANDARD,N_LIGHT和P_LIGHT的大小:導(dǎo)入錢包OOM解決

當(dāng)我們導(dǎo)入一個(gè)輕錢包時(shí),不會(huì)產(chǎn)生OOM,但導(dǎo)入不是一個(gè)輕錢包時(shí),則有可能產(chǎn)生OOM。例如,我們使用Imtoken創(chuàng)建一個(gè)錢包并導(dǎo)出Keystore,Keystore如下:

{"address":"9a2e2419f3af050d4730f80e7a65b9f8deb5e16f","crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"eaccea79c27a91e307f24988186ef21a"},"ciphertext":"a163e532edf2d76beaee5c26fd2c2fab071a9cb37627aa185ac89e223e41ab97","kdf":"scrypt","kdfparams":{"dklen":32,"n":65536,"p":1,"r":8,"salt":"6a847392a029553f4152dea7bb0b6fb0ac9eec29f55e572fe94603182f5ed7f1"},"mac":"3fad2a31e18c611b10df84db9ae368ce2e189b5c35e9f111e40ca4b4bfb02491"},"id":"032c47c2-c7b7-46f8-a3f7-f526580f6f09","version":3}

{"address":"9a2e2419f3af050d4730f80e7a65b9f8deb5e16f","crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"eaccea79c27a91e307f24988186ef21a"},"ciphertext":"a163e532edf2d76beaee5c26fd2c2fab071a9cb37627aa185ac89e223e41ab97","kdf":"scrypt","kdfparams":{"dklen":32,"n":65536,"p":1,"r":8,"salt":"6a847392a029553f4152dea7bb0b6fb0ac9eec29f55e572fe94603182f5ed7f1"},"mac":"3fad2a31e18c611b10df84db9ae368ce2e189b5c35e9f111e40ca4b4bfb02491"},"id":"032c47c2-c7b7-46f8-a3f7-f526580f6f09","version":3}

可以看到,其中n為65536,p為1,而輕錢包的n為1<<12,即2的12次方,4096,所以這不是一個(gè)輕錢包。我們將該Keystore作為一個(gè)json文件push到SD卡中,然后使用Web3j進(jìn)行導(dǎo)入:發(fā)現(xiàn)同樣會(huì)OOM:

Causedby:java.lang.OutOfMemoryError:Failedtoallocatea1036byteallocationwith13588800freebytesand12MBuntilOOM;failedduetofragmentation(requiredcontinguousfree16384bytesforanewbufferwherelargestcontiguousfree12288bytes)

atorg.spongycastle.util.Arrays.clone(Arrays.java:602)

atorg.spongycastle.crypto.generators.SCrypt.SMix(SCrypt.java:126)

atorg.spongycastle.crypto.generators.SCrypt.MFcrypt(SCrypt.java:87)

atorg.spongycastle.crypto.generators.SCrypt.generate(SCrypt.java:66)

atorg.web3j.crypto.Wallet.generateDerivedScryptKey(Wallet.java:136)

atorg.web3j.crypto.Wallet.decrypt(Wallet.java:214)

atorg.web3j.crypto.WalletUtils.loadCredentials(WalletUtils.java:112)

Causedby:java.lang.OutOfMemoryError:Failedtoallocatea1036byteallocationwith13588800freebytesand12MBuntilOOM;failedduetofragmentation(requiredcontinguousfree16384bytesforanewbufferwherelargestcontiguousfree12288bytes)

atorg.spongycastle.util.Arrays.clone(Arrays.java:602)

atorg.spongycastle.crypto.generators.SCrypt.SMix(SCrypt.java:126)

atorg.spongycastle.crypto.generators.SCrypt.MFcrypt(SCrypt.java:87)

atorg.spongycastle.crypto.generators.SCrypt.generate(SCrypt.java:66)

atorg.web3j.crypto.Wallet.generateDerivedScryptKey(Wallet.java:136)

atorg.web3j.crypto.Wallet.decrypt(Wallet.java:214)

atorg.web3j.crypto.WalletUtils.loadCredentials(WalletUtils.java:112)

通過log可以看出來,這里和創(chuàng)建錢包的OOM是一樣的,都是最后調(diào)用generateDerivedScryptKey后導(dǎo)致:創(chuàng)建錢包可以創(chuàng)建一

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論