size of memory used by map incluing all the objects it is carrying - memory-size

Measure size of Map in memory including the size of all the objects it is holding.
It doesn't seem to be straight forward. Size of the object itself might return shallow size as oppose to the actual size.
Somebody suggested that we can make the object serializable to get the size. That might give the size. If the serializable object has only that Map, will it be then possible to get the count of the object in the map and the sizes of all the objects it is holding?
The profiler may be a way to go. Even if it may not show the size of objects while showing the size of object containing map. The size of the objects itself may be obtained separately and then added up. Any idea?

If this is in Java, and if the object is Serializable, use java.io.ByteArrayOutputStream and java.io.ObjectOutputStream
public static int measure(Object m) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(m);
return baos.size();
}

Related

Storing a List in a Blob with sqflite

I am looking for an example of storing a generic Dart object (typically a List<MyObject>) in a Blob column with sqflite (and retrieving it back please) assuming it will store it as binary data.
MyObject is an object used by my application, its content is not important here as I am looking for a generic/universal method.
Could anybody be kind enough to provide a snippet?
Documentation on this subject is rare with Dart. Closest I could find would include marshaling the object as Uint8List something like this or something like that but I could not really understand/apply them.
I have found this similar issue, where the developer ends up converting his lists as a string... We really cannot do better (ie more efficient) ?
Many thanks for your help.
Patrick
You need to convert MyObject into a serialized stream of bytes using BytesBuilder() and then insert into a sqflite BLOB as an Uint8List.
For a List there are several strategies:
If the size of MyObject is the same for every element in the list,
just store them one after another.
If the size is variable, store
first the number of bytes for each one and then the object itself,
repeating for all elements on the list.
Or store each MyObject
followed by a separator. The separator must be chosen to be unique:
no separator shall be part of any MyObject instances.
Normally, the second option is more robust and flexible. Example:
MyList = getTheListOfMyObject();
final bytes = BytesBuilder();
for(int i = 0; i < myList.length; i++) {
bytes.addByte(sizeOfMyObject[i]);
// Here starts the serialization of MyObject
// only you know how it is structured, but
// every member could be expressed as series of bytes
// and remember to think on how to deserialize them
// in order to recover MyObject from the sqflite BLOB
bytes.addByte(firstByteOfMyObject[i]);
bytes.addByte(secondByteOfMyObject[i]);
bytes.addByte(thirdByteOfMyObject[i]);
// and so on.
}
// Then retrieve the full stream as a Uint8List:
final blob = bytes.toBytes();
Map<String, dynamic> map = {
'id': 1,
'my_list': blob,
};
res = await _db.insert("my_table", map);
The table for the example: "CREATE TABLE my_table(id INTEGER, my_list BLOB)"
It is a better coding practice to implement the serialization/deserialization of MyObject inside of MyObject class. The database routines shall retrieve the Uint8List from a getter before storing it and shall pass the bytes to a setter for the class to reconstruct the object structure. That way you keep the implementation details for the object properly encapsulated.

Arraylist auto complete in eclipse

Im using eclipse and recently my auto complete for arraylist changed for some reason.
after typing: ArrayList <String> myArrayList = new, it usually auto completes the line to: ArrayList<String> myArrayList = new ArrayList<>();, but now when I do it, it auto completes to: ArrayList<String> myArrayList = new ArrayList();(Without the <>).
Does anyone know how to fix it? is it something in eclipse I need to change? or something in the jdk/something like that?
Thanks.
Edit - for some reason it doesn't display the full code, I did define the type of the arraylist to String, so that is not the problem.
Image
Image
ArrayList<> (<> is called Diamond), is valid when the compiled can "infer" this type.
Consider this
ArrayList<String> a = new ArrayList<>();
Here this "diamond" is known to be String.
But when you do a raw ArrayList, the compiler will NOT TYPE CHECK, this can cause serious bugs and crashes in big applications. Generics are meant to reduce bugs.
See
The diamond
Why use Generics?
Try initializing your ArrayList in your constructor (or as a class variable):
ArrayList myArrayList = new ArrayList()
When you need to add to this array list just use myArrayList.add()

How to know the used buffer size of a SourceQueue in akka?

So I'm a fan of using Source.queue and now I want to monitor it, to know how much of the buffer it's being used at certain point. Yet I haven't seen a way to know such info. Only thing which comes to mind is a workaround in which I have a mutable number to which I add when I offer an element, and subtract, when the operation has been completed.
You could change the QueueSource class to provide a materialized queue that exposes its internal buffer size. This can be done by creating a new trait - e.g.
trait SourceQueueWithCompleteAndSize[T] extends SourceQueueWithComplete[T] {
def size: Int
}
Then, in the final part of the stage (see the original code for reference), you need to provide your new trait implementation instead of the SourceQueueWithComplete. You should be able to access the size of the internal buffer with the used method.
(stageLogic, new SourceQueueWithCompleteAndSize[T] {
// other methods implementations
override def size: Int = stageLogic.buffer.used // NEW BIT
})
It's a fair amount of code to copy, but it might be better than adding an external counter around your stage. Might as well be a valid contribution to akka-stream-contrib.

content inserted into ghashtable being destroyed

I have a ghashtable object as member of my class. I have created new object of it at constructor. I am calling this function iteratively. When i checked the size of hashtable at each method call it's giving as 0, even if i eep on adding new key-value pairs.
void myFunction(string inString)
{
string val = "some value";
printf("Size:%d",g_hash_table_size(mTable));
g_hash_table_insert(mTable,(void*)inString.c_str(),(void*)val.c_str());
printf("Size:%d",g_hash_table_size(mTable));
}
What could be the reason behind this problem.
The C++ strings are going out of scope and getting destroyed, leaving the hash table with dangling pointers to invalid memory. I don't know if know if that's the only problem in your program but it's a problem visible from looking at the part you posted.

Returning Array Fragments

I need to get array fragments from an array. I'm sick of using Array.Copy().
new ArraySegment(..).Array returns the original [full] array. The one below is what I came up with but I feel it's pretty lame. Is there a better way to do this?
class Program
{
static void Main(string[] args)
{
var arr = new ArraySegment<byte>(new byte[5] { 5, 4, 3, 2, 1 }, 0, 2).ArrayFragment();
for (int i = 0; i < arr.Length; i++)
Console.WriteLine(i);
Console.Read();
}
}
static class Extensions
{
public static T[] ArrayFragment<T>(this ArraySegment<T> segment)
{
var arr = new T[segment.Count];
Array.Copy(segment.Array, segment.Offset, arr, 0, segment.Count);
return arr;
}
}
Thanks in advance.
Update:
The above was just an example.
I have a method: byte [] CalculateXXX(byte [] key, byte [] message);
I do array manipulations inside this method. I want to return portion of an array.
ArraySegment does not implement IEnumerable and it does NOT return an array with just the segment new ArraySegment(arr...).Array returns the complete original array.
var rval = new byte[4];
//new ArraySegment(finalOutputBuffer, 0, 4).SegmentedArray();
Array.Copy(finalOutputBuffer, 0, rval, 0, 4);
I find I had to do the above to return a array fragment. Was just wondering if there's a better way of returning fragments of an array [as new array].
Vyas, I am truly sorry for having posted this useless pile of ****. It's been ages since I've actually used ArraySegment and I simply assumed that it implemented a (more or less) consistent interface. Someone (Jon?) please tell me which drugs were used during the implementation of this useless struct.
Finally, to make a long story short, the best solution is probably to implement your own version of ArraySegment, only doing it right.
I don't understand your problem with using ArraySegment. There's no additional overhead involved here, if that's what you mean, since no copying of the original data is done. Rather, ArraySegment offers a lightweight view on the data.
Th make this work, change your (return) type from T[] to IEnumerable<T>, or, if you need indexed access, to IList<T>. Generally prefer using interface types in method signatures, and completely avoid array types. The rationale is very simple: it makes problems such as yours go away and makes the interface more flexible and future-proof. It also provides better information hiding since it hides parts of the implementation which aren't relevant for the consumer of this method.
Define better. What is the downside with ArraySegment? What problem are you having that it doesn't solve?
Edit: Ok, I now understand your point of view, but this is wrong. It might be a bug in the sense that you feel it should do more, but it does exactly what it is supposed to do. It allows you to pass information to a piece of code about an array you wish to use, and which portion of the array to use.
It doesn't provide IEnumerable or anything that gives you a narrow view of the array, ie. x[0] doesn't give you the first element of the segment, nor can you foreach over it.
So yes, it's rather useless on its own, but you wouldn't be able to get something that is an array at heart, and yet is also a segment of a larger array.
You could easily make your own collection-like class that references an array and uses the array as storage, and provides indexing, enumeration, etc.
But that's not what this structure does. I guess the main problem here is that they made you expect more from it through its name.
Just another side-effect of poor iterator design in C# btw. There are many instances similar (just as 'lame') where it would just plain good design to be able to pass or point or control segments (aka concept called range) without all the archane shickanery.. copy semantics or not, array is also a well-defined concept.
Just use C++. :-)