页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
我想知道什么时候引用(特别是在类级别定义的引用)超出.net Web应用程序的范围,以便更好地了解它们何时有资格进行垃圾回收。
目前我的代码如下所示:
public class SomeClass { Object object = new Object(); protected void Page_Load(object sender, EventArgs e) { // some code } }
页面加载后对象是否超出范围(并且有资格进行垃圾收集),还是在用户离开页面之后?
我通常使用PHP创建Web应用程序,所以我猜想在页面加载完成后该对象将符合GC的条件,因为它不知道用户何时离开页面。
I'm wondering when references (specifically ones defined at a class level) go out of scope in .net web applications to better understand when they'd be eligible for garbage collection.
Currently I have code which looks like this:
public class SomeClass { Object object = new Object(); protected void Page_Load(object sender, EventArgs e) { // some code } }
Would object go out of scope after the page loads (and be eligible for garbage collection) or would it be after the user navigates away from the page?
I typically create web applications in PHP so I'd guess that object would be eligible for GC after the page is done loading since it won't know when the user navigates away from the page.
原文:https://stackoverflow.com/questions/24806748
最满意答案
客户端正在使用连接池来访问Web服务器。 请参阅HttpClientBuilder#build() 。 当创建一个默认的httpclient并且没有指定任何东西时,它会创建一个大小为2的池。因此在使用2之后,它会无限期地等待尝试从池中获取第三个连接。
您必须读取响应或关闭连接,才能重新使用客户端对象。
查看更新的代码示例:
public static void main(String[] args) { BasicCookieStore cookieStore = null; HttpResponse httpResponse = null; HttpClient httpClient = HttpClients.createDefault(); while (true) { HttpUriRequest request = new HttpGet("http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/HttpClientBuilder.html"); try { httpResponse = httpClient.execute(request); httpResponse.getEntity().getContent().close(); System.out.println(httpResponse.getStatusLine().getStatusCode()); } catch (Exception e) { System.out.println(httpResponse.getStatusLine().getStatusCode()); e.printStackTrace(); } } }
The client is using a pool of connection to reach the web server. See HttpClientBuilder#build(). When creating a default httpclient and nothing is specified it creates a pool with size of 2. So after 2 is used, it waits indefinitely trying to get the third connection from the pool.
You must read the response or close the connection, in order to re-use the client object.
See updated code sample:
public static void main(String[] args) { BasicCookieStore cookieStore = null; HttpResponse httpResponse = null; HttpClient httpClient = HttpClients.createDefault(); while (true) { HttpUriRequest request = new HttpGet("http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/HttpClientBuilder.html"); try { httpResponse = httpClient.execute(request); httpResponse.getEntity().getContent().close(); System.out.println(httpResponse.getStatusLine().getStatusCode()); } catch (Exception e) { System.out.println(httpResponse.getStatusLine().getStatusCode()); e.printStackTrace(); } } }
相关问答
更多-
while循环没有执行(while loop not executing)[2023-06-09]
阅读strcmp的手册页: strcmp()函数比较两个字符串s1和s2。 如果找到s1,则它返回小于,等于或大于零的整数,小于,匹配或大于s2。 如果你有匹配, strcmp将返回0,如果两个字符串不匹配,它将返回一个非零值。 因此while(!strcmp(string, exit))真的在说, 当字符串匹配时 ,继续循环。 string也是未初始化的并且包含垃圾,导致未定义的行为。 如果你的循环必须至少执行一次,要么首先初始化它,要么使用do..while循环。 Read the man page f ... -
运行中的循环停止执行(While loop in Run stops executing)[2023-09-03]
就是这样,我需要做的就是打破; 在我启动语音识别器之后的while循环(我还必须在usb请求失败时突破while循环)。 而且,我不必在runOnUiThread()中启动语音识别器。 现在就像一个魅力。 对于这么多细节我很抱歉,但是我会让所有关注的人都考虑整个初学者的思考过程(除非你开始投票)。 PS这是一个多么神奇的调试环境!!!! That was it, all I needed to do was to break; out of the while loop after I started th ... -
循环不会执行多次(Loop not executing more than once)[2023-11-13]
请像下面那样使用, System.out.print("Any more items? (y/n) "); input = new Scanner(System.in); ans = input.nextLine().charAt(0); beacuse input.nextLine()返回空字符串,所以当试图获得第0个字符时,它会返回StringIndexOutOfBoundsException在调用input.nextLine()之前,您需要获取一个String。 Please use lik ... -
客户端正在使用连接池来访问Web服务器。 请参阅HttpClientBuilder#build() 。 当创建一个默认的httpclient并且没有指定任何东西时,它会创建一个大小为2的池。因此在使用2之后,它会无限期地等待尝试从池中获取第三个连接。 您必须读取响应或关闭连接,才能重新使用客户端对象。 查看更新的代码示例: public static void main(String[] args) { BasicCookieStore cookieStore = null; HttpRe ...
-
Httpget Null Error(Httpget Null Error)[2021-08-14]
如果internet为null,则您的方法doinbackground返回null。 在你的方法onPostExecute中,“result”为null。 此代码抛出nullPointerException,因为您尝试访问null对象。 if(result.equals(null)) { ... } 用它替换它 if(result != null) { Title=result; } 如果要取消任务,可以在doInBackground方法(或您想要的位置)上调用cancel(true),并且不会调 ... -
停止执行脚本?(Stop executing a script?)[2022-02-20]
如果验证失败,只需使doSomething返回false,然后检查该条件: public function addContent() { $imgName = $this->doSomething(); if ($imgName === false) { return "your error message"; } $this->doSomethingElse(); //save imageName to DB $this->output[' ... -
方法不执行?(Method not executing?)[2021-06-18]
您的toast未显示,因为服务无法直接操作UI,例如显示toasts。 要从服务执行此操作,您需要在主线程上运行Toast代码。 这可以这样做: Handler handler = new Handler(Looper.getMainLooper()); handler.post(new Runnable() { @Override public void run() { Toast.makeText(TwentySeconds.this.getApplicationCont ... -
bash shell有一个nullglob shell选项,可以使不匹配的shell globs扩展为空: #!/bin/bash source=$1 target=$2 shopt -s nullglob for name in "$source"/*.txt; do todos "$name" dest="$target/${name##*/}" printf 'Moving %s to %s' "$name" "$dest" mv -- "$name" "$de ...
-
对于仍然想知道的人,虽然HttpClient为您提供了添加参数的方法,但它不起作用。 获取参数必须在URL中传递,并且这些参数将被添加到消息正文中,如post请求。 此处讨论了解决方法: 如何将查询参数添加到GetMethod(使用Java commons-httpclient)? For anyone still wondering, although HttpClient gives you the methods for adding parameters, it won't work. Get pa ...
-
问题不是NullPointerException,但如果您调试应用程序,您会发现异常是: NetworkOnMainThreadException 。 很可能您是在较新的SDK(Honeycomb或更高版本)上运行您的应用程序,因此不鼓励这样做。 任何网络代码,你应该放入一个线程。 理想情况下使用AsyncTask 。 请查看以下链接: http : //www.techblogistech.com/2011/11/how-to-fix-the-android-networkonmainthreadexce ...