How to Get Object Size in Memory

Getting size in memory of an object in PHP?

You can call memory_get_usage() before and after allocating your class as illustrated in this example from IBM. You could even create a wrapper to do this, possibly storing the result on a member variable of the complex class itself.

EDIT:

To clarify the part about storing the allocated memory size, you can do something like this:

class MyBigClass
{
var $allocatedSize;
var $allMyOtherStuff;
}

function AllocateMyBigClass()
{
$before = memory_get_usage();
$ret = new MyBigClass;
$after = memory_get_usage();
$ret->allocatedSize = ($after - $before);

return $ret;
}

At any point in the future, you could check allocatedSize to see how big that object was at time of allocation. If you add to it after allocating it, though, allocatedSize would no longer be accurate.

how to find size of object in memory?

The BinaryFormatter tells you how many bytes it needs to serialize the class to binary. This is very different from the size a class takes in memory. For instance, the BinaryFormatter writes information about the type into the stream.

The size that an object uses in memory is not defined at compile time but at runtime, since the JIT compiler decides on the final layout. Now comes the question how to find out the size. While I have a solution for structs (create an array, do pointer arithmetic), I am not sure how to find that out for classes. One solution would be to define your class MemberStateModel as a struct, measure it, then turn back to a class, assuming it will have the same size.

You can also estimate the size, by counting the size of the fields as a lower bound (since padding occurs). If your class has references to other class instances, then it gets nearly impossible to estimate.

Calculate size of Object in Java

You can use the java.lang.instrumentation package.

It has a method that can be used to get the implementation specific approximation of object size, as well as overhead associated with the object.

The answer that Sergey linked has a great example, which I'll repost here, but you should have already looked at from his comment:

import java.lang.instrument.Instrumentation;

public class ObjectSizeFetcher {
private static Instrumentation instrumentation;

public static void premain(String args, Instrumentation inst) {
instrumentation = inst;
}

public static long getObjectSize(Object o) {
return instrumentation.getObjectSize(o);
}
}

Use getObjectSize:

public class C {
private int x;
private int y;

public static void main(String [] args) {
System.out.println(ObjectSizeFetcher.getObjectSize(new C()));
}
}

Source

How to get the in-memory size of an object in Android, or performance benchmarks?

Are there any benchmarking tools out there for Android that I can use
to see which object is bigger in memory and/or takes more processing
time to store/retrieve values?

Yes, Android comes with a lot of great tools for developers, it's recommended to get to know them. Here you have official documentation link for a good start.

Switch to DDMS perspective, assuming you are in Eclipse.

Now, these views should be helpful to you in measuring memory:

  • Allocation tracker. You can see which objects take how much memory. During a run you have to press buttons "Start tracking" and later "Get Allocations".
  • Heap. You can see what amount of memory is taken from the heap.

To profile your application, see bottlenecks etc. use Traceview. To call it conveniently from Eclipse open Threads view and while running your program click the button with red circle, like "record button".

Find size of single object in memory

varinfo() accepts regular expressions to match object names, so you can use something like

x = rand(100, 100)
varinfo(r"x")

to get info on x. For the size in bytes use

Base.summarysize(x)

EDIT:
Originally this answer recommended whos(), however as @Plankalkül mentions whos() has been renamed to varinfo(), the answer was updated accordingly.

In Java, what is the best way to determine the size of an object?

You can use the java.lang.instrument package.

Compile and put this class in a JAR:

import java.lang.instrument.Instrumentation;

public class ObjectSizeFetcher {
private static Instrumentation instrumentation;

public static void premain(String args, Instrumentation inst) {
instrumentation = inst;
}

public static long getObjectSize(Object o) {
return instrumentation.getObjectSize(o);
}
}

Add the following to your MANIFEST.MF:

Premain-Class: ObjectSizeFetcher

Use the getObjectSize() method:

public class C {
private int x;
private int y;

public static void main(String [] args) {
System.out.println(ObjectSizeFetcher.getObjectSize(new C()));
}
}

Invoke with:

java -javaagent:ObjectSizeFetcherAgent.jar C


Related Topics



Leave a reply



Submit