我正在查看写入套接字的应用程序的内存分配,我发现很多分配都是在public class MainActivity extends AppCompatActivity {
private Button btn;
private ImageView iv;
private final int WORDNUM = 35; //转化成图片时 每行显示的字数
private final int WIDTH = 450; //设置图片的宽度
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.imageView);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int x=5,y=10;
try {
TextProperty tp = new TextProperty(WORDNUM, new InputStreamReader(getResources().getAssets().open("1.txt")));
Bitmap bitmap = Bitmap.createBitmap(WIDTH, 20*tp.getHeigt(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextAlign(Paint.Align.LEFT);
paint.setTextSize(20f);
String [] ss = tp.getContext();
for(int i=0;i<tp.getHeigt();i++){
canvas.drawText(ss[i], x, y, paint);
y=y+20;
}
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
String path = Environment.getExternalStorageDirectory() + "/image.png";
System.out.println(path);
FileOutputStream os = new FileOutputStream(new File(path));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
//Display the image on ImageView.
iv.setImageBitmap(bitmap);
iv.setBackgroundColor(Color.BLUE);
os.flush();
os.close();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
SocketOutputStream.socketWrite
中完成的:
有没有办法摆脱这些分配?
我使用Java 1.8.0_40在Windows上进行性能分析。
以下是来自jdk 1.8的SocketOutputStream的源代码,我没有看到可以分配这些对象的代码
InetAddress