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

Memory usage? #34

Open
AndroidDeveloperLB opened this issue Feb 26, 2017 · 6 comments
Open

Memory usage? #34

AndroidDeveloperLB opened this issue Feb 26, 2017 · 6 comments
Labels

Comments

@AndroidDeveloperLB
Copy link

Any information about memory usage of this library?
Is it efficient?
Suppose we take the sample of Cartman, How much does it take in heap memory, compared to the same sample without loading anything?

I tried to modify the sample, and I think it uses about 4MB :

@Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_svg_demo);

       Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
       setSupportActionBar(toolbar);

       mImageView = (ImageView) findViewById(R.id.iv_image);
       mButton = (Button) findViewById(R.id.bt_button);
       printMemStats();
       mSvg = Sharp.loadResource(getResources(), R.raw.cartman);
       // If you want to load typefaces from assets:
       //          .withAssets(getAssets());

       // If you want to load an SVG from assets:
       //mSvg = Sharp.loadAsset(getAssets(), "cartman.svg");
       mButton.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               reloadSvg(true);
           }
       });

       mAttacher = new PhotoViewAttacher(mImageView);
       mAttacher.setMaximumScale(10f);

       reloadSvg(false);
       new Handler().postDelayed(new Runnable() {
           @Override
           public void run() {
               printMemStats();
           }
       },2000);


   }

   private void printMemStats() {
       final long availableMemInBytes = getAvailableMemInBytes();
       final long maxMemInBytes = getMaxMemInBytes();
       final long usedMemInBytes = maxMemInBytes - availableMemInBytes;
       final long usedMemInPercentage = usedMemInBytes * 100 / maxMemInBytes;
       Log.d("AppLog", "used: " + Formatter.formatShortFileSize(this, usedMemInBytes) + " / " +
               Formatter.formatShortFileSize(this, maxMemInBytes) + " (" + usedMemInPercentage + "%)");
   }

   public static long getMaxMemInBytes() {
       return Runtime.getRuntime().maxMemory();
   }

   public static long getAvailableMemInBytes() {
       // find available memory
       final Runtime runtime = Runtime.getRuntime();
       final long usedMem = runtime.totalMemory() - runtime.freeMemory();
       final long maxHeapSize = runtime.maxMemory();
       final long result = maxHeapSize - usedMem;
       return result;
   }
@pflammertsma
Copy link
Collaborator

pflammertsma commented Feb 26, 2017 via email

@AndroidDeveloperLB
Copy link
Author

But why would it take so much ? I mean, shouldn't it be very low in memory usage, seeing that it's just drawing according to some rules (draw line here, draw gradient there, etc...) , instead of using pixels data?

@pflammertsma
Copy link
Collaborator

pflammertsma commented Feb 26, 2017 via email

@AndroidDeveloperLB
Copy link
Author

Oh, this probably explains why I got similar result on VectorDrawable.
But, on new Android versions (7.1.x), it seems VectorDrawable barely changes the memory usage (if at all). Maybe they've optimized it somehow?

@pflammertsma
Copy link
Collaborator

pflammertsma commented Feb 26, 2017 via email

@AndroidDeveloperLB
Copy link
Author

Sadly, it has its own disadvantages. Lack of ways to use it, and lack of features to draw (can't draw gradients and texts, for example).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants