Search code examples
androidgoogle-glass

Memory of Google glass


I used Bluetooth Client-Server communication to send data from Android mobile to Google Glass.After running the code many times, I had GC_FOR_ALLOC freed...the garbage collection problem and the memory of Google glass is almost full, and this is the while loop that I think the reason of the problem

private void manageConnectedSocket(BluetoothSocket socket) {


      while(socket.isConnected()) {
          try {
              BufferedReader   input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

              mResult = input.readLine();
              prev=mResult;
              if(!prev.equals(next)){
              System.out.println("result=" + mResult);
              /** PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
               //out.println(Integer.parseInt(mResult)+50);
               out.println(mResult);**/

              runOnUiThread(new Runnable() {
                  public void run() {
                      mTvInfo.setText(mResult);
                      next=prev;    }
              });
              }
              //  socket.close();
             // manageConnectedSocket(socket);
          } catch (IOException e) {
              final IOException ex = e;
              runOnUiThread(new Runnable() {
                  public void run() {
                      mTvInfo.setText(ex.getMessage());
                  }
              });
          }
      }

Please, I need a way to clean the memory of Google Glass


Solution

  • Since android uses Linux kernel you can stick to the traditional method of executing the commands in order to release some of the memory from the Google Glass.

    But after having to look at your code, you are executing all the background task in while loop and taking that and displaying it on UI thread in the same loop. This is really an expensive operation which may lead to heating of Google Glass too. I would suggest you changing the code so that all the background task takes place separately inside the Async Task thread and then displaying the data on the activity in the postexecute() method.

    Here is the example code.

    private class HttpGetter extends AsyncTask<URL, Void, Void> {
    
                    @Override
                    protected Void doInBackground(URL... urls) {
                            // TODO Auto-generated method stub
                            StringBuilder builder = new StringBuilder();
                            HttpClient client = new DefaultHttpClient();
                            HttpGet httpGet = new HttpGet(urls[0]);
    
                            try {
                                    HttpResponse response = client.execute(httpGet);
                                    StatusLine statusLine = response.getStatusLine();
                                    int statusCode = statusLine.getStatusCode();
                                    if (statusCode == 200) {
                                            HttpEntity entity = response.getEntity();
                                            InputStream content = entity.getContent();
                                            BufferedReader reader = new BufferedReader(
                                                            new InputStreamReader(content));
                                            String line;
                                            while ((line = reader.readLine()) != null) {
                                                    builder.append(line);
                                            }
                                            Log.v("Getter", "Your data: " + builder.toString()); //response data
                                    } else {
                                            Log.e("Getter", "Failed to download file");
                                    }
                            } catch (ClientProtocolException e) {
                                    e.printStackTrace();
                            } catch (IOException e) {
                                    e.printStackTrace();
                            }
    
                            return null;
                    }
        }
    
    HttpGetter get = new HttpGetter();
    get.execute("http://www.google.es");