首页 \ 问答 \ 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)

页面加载后对象是否有资格进行垃圾回收?(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
更新时间:2023-05-26 10:05

最满意答案

客户端正在使用连接池来访问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();
        }
    }
}

相关问答

更多
  • 阅读strcmp的手册页: strcmp()函数比较两个字符串s1和s2。 如果找到s1,则它返回小于,等于或大于零的整数,小于,匹配或大于s2。 如果你有匹配, strcmp将返回0,如果两个字符串不匹配,它将返回一个非零值。 因此while(!strcmp(string, exit))真的在说, 当字符串匹配时 ,继续循环。 string也是未初始化的并且包含垃圾,导致未定义的行为。 如果你的循环必须至少执行一次,要么首先初始化它,要么使用do..while循环。 Read the man page f ...
  • 就是这样,我需要做的就是打破; 在我启动语音识别器之后的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 ...
  • 请像下面那样使用, 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 ...
  • 如果internet为null,则您的方法doinbackground返回null。 在你的方法onPostExecute中,“result”为null。 此代码抛出nullPointerException,因为您尝试访问null对象。 if(result.equals(null)) { ... } 用它替换它 if(result != null) { Title=result; } 如果要取消任务,可以在doInBackground方法(或您想要的位置)上调用cancel(true),并且不会调 ...
  • 如果验证失败,只需使doSomething返回false,然后检查该条件: public function addContent() { $imgName = $this->doSomething(); if ($imgName === false) { return "your error message"; } $this->doSomethingElse(); //save imageName to DB $this->output[' ...
  • 您的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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。