God*_*God 5 java multithreading android garbage-collection jsoup
我有2 Thread秒做一些网络计算.当我运行我的应用程序并开始我的第二个后,Thread我得到一个:
Suspending all threads took: ms
警告后跟:
Background sticky concurrent mark sweep GC freed 246745(21MB) AllocSpace objects, 169(6MB) LOS objects, 33% free, 31MB/47MB, paused 1.972ms total 127.267ms 警告.
有时候我会得到那两个警告,有时我会收到很多这两个警告,直到我决定终止应用程序运行.在这一点上,它只是运行主要Thread,基本上什么都不做.这是相关代码:
MainActivity.java:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting html page through a thread
this.getHtmlPageThread = new GetHtmlPageThread(URL_STRING);
this.getHtmlPageThread.start();
// The thread that will search the web for data
this.getDataFromTheWebThread = new GetDataFromTheWebThread();
// Search button click listener
searchButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// Get the searched lyrics
searchedLyrics = inputEditText.getText().toString();
informUserAboutConnectionToTheNet();
// Starting to search the web for data through a thread
getDataFromTheWebThread.start();
if (!getDataFromTheWebThread.isAlive())
{
printMap(MainActivity.matchResultMap);
}
}
}); // End of search button click listener
printMap(MainActivity.matchResultMap);
} // End of onCreate() method
protected void onStart()
{
super.onStart();
if (!this.isParseSucceeded()) // Connection to net failed
{
if (!getHtmlPageThread.isAlive()) // If the thread is not alive, start it.
{
getHtmlPageThread.start(); // Try to connect again
this.informUserAboutConnectionToTheNet();
}
}
if (!this.isParseSucceeded())
{
super.onStart(); // Call onStart() method
}
} // End of onStart() method
Run Code Online (Sandbox Code Playgroud)
GetHtmlPageThread.java:
public class GetHtmlPageThread extends Thread
{
private String url;
public GetHtmlPageThread(String url)
{
this.url = url;
}
@Override
public void run()
{
try
{
MainActivity.htmlPage.setHtmlDocument(this.getParsedDocument(this.url));
if (MainActivity.htmlPage.getHtmlDocument() != null)
{
MainActivity.parsedSucceeded = true; // Parsed succeeded
}
else
{
MainActivity.parsedSucceeded = false; // Parsed failed
}
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
/**
* Returns the document object of the url parameter.
* If the connection is failed , return null.
*
* @param url Url to parse
* @return The document of the url.
*
*/
public Document getParsedDocument(String url)
{
try
{
return Jsoup.connect(url).get();
}
catch (IOException e) // On error
{
e.printStackTrace();
}
return null; // Failed to connect to the url
}
}
Run Code Online (Sandbox Code Playgroud)
GetDataFromTheWeb.java:
public class GetDataFromTheWebThread extends Thread
{
public static boolean isFinished = false; // False - the thread is still running. True - the thread is dead
@Override
public void run()
{
GetDataFromTheWebThread.isFinished = false;
try
{
this.getLyricsPlanetDotComResults(MainActivity.searchedLyrics); // Method for internet computations
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
GetDataFromTheWebThread.isFinished = true;
}
...
}
Run Code Online (Sandbox Code Playgroud)
基本上,this.getLyricsPlanetDotComResults(MainActivity.searchedLyrics);第二种方法Thread是进行大量的互联网工作和计算.比准确的网络更多的计算.
所以我想我得到了那些警告,因为第二个Thread太"忙"了?或者也许只是我的活动生命周期与onCreate()方法和onStart()方法的实现是错误的?
毋庸置疑,我没有得到我想要的输出,虽然我调试了应用程序并逐步完成了第二次Thread,它完美地运行.所以,它必须与我Activity的实现有关.
Gab*_*han 10
第一行基本上是说垃圾收集器(GC)决定它需要暂停所有线程来完成它的一些工作(例如重定位变量),这需要一些时间.通常那里有一个数字.
第二行是收集的结果.它释放了相当多的内存,你现在有1/3的空闲.它要求你的应用程序暂停2毫秒,总共花费127毫秒收集垃圾.
GC本身并不坏.但是,如果你一直这样做,要么就是你需要大量的内存,或者你做的事情效率低下.重字符串解析,特别是像JSoup这样的东西,可以导致很多小对象(主要是字符串)真正需要在像手机这样的小型存储设备上快速清理,所以在这里看到它并不奇怪.
基本上,这只是一个问题,如果你从GC得到性能打嗝或者你在某个时候要去OOM.如果这些都没有发生,我也不会担心这一点.
| 归档时间: |
|
| 查看次数: |
20135 次 |
| 最近记录: |