Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible Memory Leak? #78

Closed
wonderfly opened this issue Jan 9, 2015 · 4 comments
Closed

Possible Memory Leak? #78

wonderfly opened this issue Jan 9, 2015 · 4 comments
Assignees
Labels
priority: p0 Highest priority. Critical issue. P0 implies highest priority. 🚨 This issue needs some love. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@wonderfly
Copy link
Contributor

From yan...@google.com on March 27, 2012 07:47:17

Reported from https://code.google.com/p/google-api-java-client/issues/detail?id=402 Reported by aalb...@google.com, Feb 3, 2012
Version of google-api-java-client 1.6?
Java environment Java 6, Android 2.3

From Calendar API generated code in Calendar.java:
public com.google.api.services.calendar.model.FreeBusyResponse execute() throws IOException {
HttpResponse response = executeUnparsed();

com.google.api.services.calendar.model.FreeBusyResponse result = response.parseAs(
    com.google.api.services.calendar.model.FreeBusyResponse.class);
result.setResponseHeaders(response.getHeaders());
return result;

}

We open an HttpResponse object but we never disconnect it.

Comment 1 by project member rmis...@google.com, Feb 3, 2012
(No comment was entered for this change.)
Status: Accepted
Comment 2 by aalb...@google.com, Feb 3, 2012
Yikes

Calling response.disconnect() throws a NPE because in HttpResponse.java:

public InputStream getContent() throws IOException {
LowLevelHttpResponse response = this.response;
if (response == null) {
return content;
}
InputStream content = this.response.getContent();
this.response = null;

We set the internal response to null.
Comment 3 by aalb...@google.com, Feb 3, 2012
calling disconnect() is not enough. There is also the content that needs to be closed.

I think that disconnect() should be renamed to close() and should look like this:

private void close() {
    try {
      final InputStream content = response.getContent();
      if (content != null) {
          content.close();
      }
      response.disconnect();
    } catch (IOException e) {
      // ignore
    }
}

Calling both disconnect and closing the content fixed my memory leak. (after fixing the NPE of course.

Comment 4 by project member yan...@google.com, Feb 16, 2012
+1 on calling disconnect. I highly recommend reading the JavaDoc from the Android SDK on this topic: http://developer.android.com/reference/java/net/HttpURLConnection.html However, please also read the section on Performance. We definitely ideally want to reuse Sockets, so perhaps we should set the "http.keepAlive" system property to "false". So this should all be investigated carefully.

We might also want to investigate what the correct behavior should be for ApacheHttpTransport and UrlFetchHttpTransport.

As far as closing the streams properly, I think this has already been resolved. Let me know if you don't think that's the case.

Original issue: http://code.google.com/p/google-http-java-client/issues/detail?id=78

@wonderfly wonderfly added type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. imported priority: p0 Highest priority. Critical issue. P0 implies highest priority. labels Jan 9, 2015
@wonderfly wonderfly self-assigned this Jan 9, 2015
@wonderfly
Copy link
Contributor Author

From rmis...@google.com on April 20, 2012 07:31:28

  • Yes I believe the closing streams properly has been resolved in previous CLs.
  • I do not think we should set the keep alive property, users can always configure that on the transport if that is what they need.
  • Calling disconnect is a hard problem, in HttpResponse#getContent the LowLevelHttpResponse is explicitly set to null to allow it to be GC'ed but the content is still alive and we cannot be sure when the content will be consumed.
    One possible solution could be to not set response to null and to explicitly disconnect it in parseAs / parseAsString when we are sure that the content is consumed. As part of this we can also add to the JavaDoc in HttpResponse saying "Consider calling {@link #disconnect} when the response is no longer needed. Also, consider setting keep an alive property on the {@link HttpTransport}.

Thoughts?

@wonderfly
Copy link
Contributor Author

From rmis...@google.com on May 14, 2012 07:14:07

Labels: -Milestone-Version1.9.0 Milestone-Version1.10.0

@wonderfly
Copy link
Contributor Author

From yan...@google.com on May 19, 2012 06:12:54

Labels: -Priority-High Priority-Critical

@wonderfly
Copy link
Contributor Author

From rmis...@google.com on May 31, 2012 08:10:09

http://codereview.appspot.com/6225045/

Status: Fixed

@yoshi-automation yoshi-automation added the 🚨 This issue needs some love. label Apr 6, 2020
clundin25 pushed a commit to clundin25/google-http-java-client that referenced this issue Aug 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p0 Highest priority. Critical issue. P0 implies highest priority. 🚨 This issue needs some love. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

2 participants