Memory usage of an empty List or Dictionary?
How much memory is used by an empty List or Dictionary? Such as:
List<double> list = new List<double>();
The pointer itself eats at least 32 bits on x86 and 64 of x64 OS, but what about the list itself? With 0 records.
The reason for asking is, can you save some bytes by setting lists to null
?
(Imagine you have a class that contains some List<T>
which in some cases is being used and in other case it is not, in that case having a boolean
like IsEmpty
and null
instead of empty list might save some operating memory. Especially in case you would have thousands of such classes in operating memory, every bit counts.)
c# memory
add a comment |
How much memory is used by an empty List or Dictionary? Such as:
List<double> list = new List<double>();
The pointer itself eats at least 32 bits on x86 and 64 of x64 OS, but what about the list itself? With 0 records.
The reason for asking is, can you save some bytes by setting lists to null
?
(Imagine you have a class that contains some List<T>
which in some cases is being used and in other case it is not, in that case having a boolean
like IsEmpty
and null
instead of empty list might save some operating memory. Especially in case you would have thousands of such classes in operating memory, every bit counts.)
c# memory
I think your best bet is to use a memory profiler like ANTS and look up these specific objects and their memory usage.
– Kippie
Apr 21 '13 at 13:34
I don't have money for ants and ATM I am on ubuntu
– Petr
Apr 21 '13 at 13:35
5
I think under almost all circumstances, the answer is “so little it doesn't actually matter”.
– svick
Apr 21 '13 at 13:55
it always matter to me when it comes to memory... If there were less programmers who don't care about memory usage, modern programs wouldn't be so resource expensive...
– Petr
Apr 21 '13 at 17:07
3
@Petr 1. How many empty lists do you have? Unless it's millions, this won't make a measurable difference. 2. If you want to decrease memory consumption of your application, you should use a memory profiler to find out what takes up most memory. Premature optimization is a waste of time (at best), and that applies to optimizing memory too.
– svick
Apr 22 '13 at 11:44
add a comment |
How much memory is used by an empty List or Dictionary? Such as:
List<double> list = new List<double>();
The pointer itself eats at least 32 bits on x86 and 64 of x64 OS, but what about the list itself? With 0 records.
The reason for asking is, can you save some bytes by setting lists to null
?
(Imagine you have a class that contains some List<T>
which in some cases is being used and in other case it is not, in that case having a boolean
like IsEmpty
and null
instead of empty list might save some operating memory. Especially in case you would have thousands of such classes in operating memory, every bit counts.)
c# memory
How much memory is used by an empty List or Dictionary? Such as:
List<double> list = new List<double>();
The pointer itself eats at least 32 bits on x86 and 64 of x64 OS, but what about the list itself? With 0 records.
The reason for asking is, can you save some bytes by setting lists to null
?
(Imagine you have a class that contains some List<T>
which in some cases is being used and in other case it is not, in that case having a boolean
like IsEmpty
and null
instead of empty list might save some operating memory. Especially in case you would have thousands of such classes in operating memory, every bit counts.)
c# memory
c# memory
edited Nov 24 '18 at 12:32
DaveInCaz
3,28631940
3,28631940
asked Apr 21 '13 at 13:31
PetrPetr
6,0741050104
6,0741050104
I think your best bet is to use a memory profiler like ANTS and look up these specific objects and their memory usage.
– Kippie
Apr 21 '13 at 13:34
I don't have money for ants and ATM I am on ubuntu
– Petr
Apr 21 '13 at 13:35
5
I think under almost all circumstances, the answer is “so little it doesn't actually matter”.
– svick
Apr 21 '13 at 13:55
it always matter to me when it comes to memory... If there were less programmers who don't care about memory usage, modern programs wouldn't be so resource expensive...
– Petr
Apr 21 '13 at 17:07
3
@Petr 1. How many empty lists do you have? Unless it's millions, this won't make a measurable difference. 2. If you want to decrease memory consumption of your application, you should use a memory profiler to find out what takes up most memory. Premature optimization is a waste of time (at best), and that applies to optimizing memory too.
– svick
Apr 22 '13 at 11:44
add a comment |
I think your best bet is to use a memory profiler like ANTS and look up these specific objects and their memory usage.
– Kippie
Apr 21 '13 at 13:34
I don't have money for ants and ATM I am on ubuntu
– Petr
Apr 21 '13 at 13:35
5
I think under almost all circumstances, the answer is “so little it doesn't actually matter”.
– svick
Apr 21 '13 at 13:55
it always matter to me when it comes to memory... If there were less programmers who don't care about memory usage, modern programs wouldn't be so resource expensive...
– Petr
Apr 21 '13 at 17:07
3
@Petr 1. How many empty lists do you have? Unless it's millions, this won't make a measurable difference. 2. If you want to decrease memory consumption of your application, you should use a memory profiler to find out what takes up most memory. Premature optimization is a waste of time (at best), and that applies to optimizing memory too.
– svick
Apr 22 '13 at 11:44
I think your best bet is to use a memory profiler like ANTS and look up these specific objects and their memory usage.
– Kippie
Apr 21 '13 at 13:34
I think your best bet is to use a memory profiler like ANTS and look up these specific objects and their memory usage.
– Kippie
Apr 21 '13 at 13:34
I don't have money for ants and ATM I am on ubuntu
– Petr
Apr 21 '13 at 13:35
I don't have money for ants and ATM I am on ubuntu
– Petr
Apr 21 '13 at 13:35
5
5
I think under almost all circumstances, the answer is “so little it doesn't actually matter”.
– svick
Apr 21 '13 at 13:55
I think under almost all circumstances, the answer is “so little it doesn't actually matter”.
– svick
Apr 21 '13 at 13:55
it always matter to me when it comes to memory... If there were less programmers who don't care about memory usage, modern programs wouldn't be so resource expensive...
– Petr
Apr 21 '13 at 17:07
it always matter to me when it comes to memory... If there were less programmers who don't care about memory usage, modern programs wouldn't be so resource expensive...
– Petr
Apr 21 '13 at 17:07
3
3
@Petr 1. How many empty lists do you have? Unless it's millions, this won't make a measurable difference. 2. If you want to decrease memory consumption of your application, you should use a memory profiler to find out what takes up most memory. Premature optimization is a waste of time (at best), and that applies to optimizing memory too.
– svick
Apr 22 '13 at 11:44
@Petr 1. How many empty lists do you have? Unless it's millions, this won't make a measurable difference. 2. If you want to decrease memory consumption of your application, you should use a memory profiler to find out what takes up most memory. Premature optimization is a waste of time (at best), and that applies to optimizing memory too.
– svick
Apr 22 '13 at 11:44
add a comment |
2 Answers
2
active
oldest
votes
Decompiled by dotPeek :
public class List<T> : IList<T>, ICollection<T>, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable
{
private T _items; //4 bytes for x86, 8 for x64
private int _size; //4 bytes
private int _version; //4 bytes
[NonSerialized]
private object _syncRoot; //4 bytes for x86, 8 for x64
private static readonly T _emptyArray; //one per type
private const int _defaultCapacity = 4; //one per type
...
}
you got total of 20 bytes on x86 (16 for List<T>
members and 4 for metadata reference overhead) and 32 on x64, including reffernce to type of the object, which each object in .net have. This calculation is done roughly not counting alligment.
public class Dictionary<TKey, TValue> : ...
{
private int buckets; //4 bytes for x86, 8 for x64
private Dictionary<TKey, TValue>.Entry entries; //4 bytes for x86, 8 for x64
private int count; //4 bytes
private int version; //4 bytes
private int freeList; //4 bytes
private int freeCount; //4 bytes
private IEqualityComparer<TKey> comparer; //4 bytes for x86, 8 for x64
private Dictionary<TKey, TValue>.KeyCollection keys; //4 bytes for x86, 8 for x64
private Dictionary<TKey, TValue>.ValueCollection values; //4 bytes for x86, 8 for x64
private object _syncRoot; //4 bytes for x86, 8 for x64
private const string VersionName = "Version"; //one per type
private const string HashSizeName = "HashSize"; //one per type
private const string KeyValuePairsName = "KeyValuePairs"; //one per type
private const string ComparerName = "Comparer"; //one per type
}
44 for x86 and 72 for x64. Again rough calculation, since instances of different objects are required.
What about the virtual method table?
– Pieter Geerkens
Apr 21 '13 at 13:41
@PieterGeerkens isn't it created one per type?List<T>
has also two static variables, but I didn't count them, because they don't affect the size of the object itself. As far as I remember, each object in .net has only one reference overhead.
– Ilya Ivanov
Apr 21 '13 at 13:42
1
@svick as OP stated in question: "How much memory eats empty List and Dictionary?".
– Ilya Ivanov
Apr 21 '13 at 14:04
1
@IlyaIvanov Yes, but are you sure emptyList
means the array is not actually allocated?
– svick
Apr 21 '13 at 14:30
1
@svick there is a static empty arrayT[0]
that is shared by all empty lists of typeT
. In the default constructor:_items = _emptyArray;
.
– Eren Ersönmez
Apr 22 '13 at 6:00
|
show 3 more comments
The reason for asking is, can you save some bytes by setting lists to null?
As a general rule (to which, like any rule, there are exceptions), you shouldn't set managed objects to null
to free memory. Instead, leave it to the garbage collector to detect when an object is no longer reachable. In fact, setting a reference to null
may be counter-productive as it can actually slightly extend the lifetime of the object.
An exception might be an edge case where it's worth setting a large object to null
(e.g. a very large list that is a property of another object which for some reason needs to remain reachable), but such cases will be rare and may indicate a "code smell".
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16131641%2fmemory-usage-of-an-empty-list-or-dictionary%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Decompiled by dotPeek :
public class List<T> : IList<T>, ICollection<T>, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable
{
private T _items; //4 bytes for x86, 8 for x64
private int _size; //4 bytes
private int _version; //4 bytes
[NonSerialized]
private object _syncRoot; //4 bytes for x86, 8 for x64
private static readonly T _emptyArray; //one per type
private const int _defaultCapacity = 4; //one per type
...
}
you got total of 20 bytes on x86 (16 for List<T>
members and 4 for metadata reference overhead) and 32 on x64, including reffernce to type of the object, which each object in .net have. This calculation is done roughly not counting alligment.
public class Dictionary<TKey, TValue> : ...
{
private int buckets; //4 bytes for x86, 8 for x64
private Dictionary<TKey, TValue>.Entry entries; //4 bytes for x86, 8 for x64
private int count; //4 bytes
private int version; //4 bytes
private int freeList; //4 bytes
private int freeCount; //4 bytes
private IEqualityComparer<TKey> comparer; //4 bytes for x86, 8 for x64
private Dictionary<TKey, TValue>.KeyCollection keys; //4 bytes for x86, 8 for x64
private Dictionary<TKey, TValue>.ValueCollection values; //4 bytes for x86, 8 for x64
private object _syncRoot; //4 bytes for x86, 8 for x64
private const string VersionName = "Version"; //one per type
private const string HashSizeName = "HashSize"; //one per type
private const string KeyValuePairsName = "KeyValuePairs"; //one per type
private const string ComparerName = "Comparer"; //one per type
}
44 for x86 and 72 for x64. Again rough calculation, since instances of different objects are required.
What about the virtual method table?
– Pieter Geerkens
Apr 21 '13 at 13:41
@PieterGeerkens isn't it created one per type?List<T>
has also two static variables, but I didn't count them, because they don't affect the size of the object itself. As far as I remember, each object in .net has only one reference overhead.
– Ilya Ivanov
Apr 21 '13 at 13:42
1
@svick as OP stated in question: "How much memory eats empty List and Dictionary?".
– Ilya Ivanov
Apr 21 '13 at 14:04
1
@IlyaIvanov Yes, but are you sure emptyList
means the array is not actually allocated?
– svick
Apr 21 '13 at 14:30
1
@svick there is a static empty arrayT[0]
that is shared by all empty lists of typeT
. In the default constructor:_items = _emptyArray;
.
– Eren Ersönmez
Apr 22 '13 at 6:00
|
show 3 more comments
Decompiled by dotPeek :
public class List<T> : IList<T>, ICollection<T>, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable
{
private T _items; //4 bytes for x86, 8 for x64
private int _size; //4 bytes
private int _version; //4 bytes
[NonSerialized]
private object _syncRoot; //4 bytes for x86, 8 for x64
private static readonly T _emptyArray; //one per type
private const int _defaultCapacity = 4; //one per type
...
}
you got total of 20 bytes on x86 (16 for List<T>
members and 4 for metadata reference overhead) and 32 on x64, including reffernce to type of the object, which each object in .net have. This calculation is done roughly not counting alligment.
public class Dictionary<TKey, TValue> : ...
{
private int buckets; //4 bytes for x86, 8 for x64
private Dictionary<TKey, TValue>.Entry entries; //4 bytes for x86, 8 for x64
private int count; //4 bytes
private int version; //4 bytes
private int freeList; //4 bytes
private int freeCount; //4 bytes
private IEqualityComparer<TKey> comparer; //4 bytes for x86, 8 for x64
private Dictionary<TKey, TValue>.KeyCollection keys; //4 bytes for x86, 8 for x64
private Dictionary<TKey, TValue>.ValueCollection values; //4 bytes for x86, 8 for x64
private object _syncRoot; //4 bytes for x86, 8 for x64
private const string VersionName = "Version"; //one per type
private const string HashSizeName = "HashSize"; //one per type
private const string KeyValuePairsName = "KeyValuePairs"; //one per type
private const string ComparerName = "Comparer"; //one per type
}
44 for x86 and 72 for x64. Again rough calculation, since instances of different objects are required.
What about the virtual method table?
– Pieter Geerkens
Apr 21 '13 at 13:41
@PieterGeerkens isn't it created one per type?List<T>
has also two static variables, but I didn't count them, because they don't affect the size of the object itself. As far as I remember, each object in .net has only one reference overhead.
– Ilya Ivanov
Apr 21 '13 at 13:42
1
@svick as OP stated in question: "How much memory eats empty List and Dictionary?".
– Ilya Ivanov
Apr 21 '13 at 14:04
1
@IlyaIvanov Yes, but are you sure emptyList
means the array is not actually allocated?
– svick
Apr 21 '13 at 14:30
1
@svick there is a static empty arrayT[0]
that is shared by all empty lists of typeT
. In the default constructor:_items = _emptyArray;
.
– Eren Ersönmez
Apr 22 '13 at 6:00
|
show 3 more comments
Decompiled by dotPeek :
public class List<T> : IList<T>, ICollection<T>, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable
{
private T _items; //4 bytes for x86, 8 for x64
private int _size; //4 bytes
private int _version; //4 bytes
[NonSerialized]
private object _syncRoot; //4 bytes for x86, 8 for x64
private static readonly T _emptyArray; //one per type
private const int _defaultCapacity = 4; //one per type
...
}
you got total of 20 bytes on x86 (16 for List<T>
members and 4 for metadata reference overhead) and 32 on x64, including reffernce to type of the object, which each object in .net have. This calculation is done roughly not counting alligment.
public class Dictionary<TKey, TValue> : ...
{
private int buckets; //4 bytes for x86, 8 for x64
private Dictionary<TKey, TValue>.Entry entries; //4 bytes for x86, 8 for x64
private int count; //4 bytes
private int version; //4 bytes
private int freeList; //4 bytes
private int freeCount; //4 bytes
private IEqualityComparer<TKey> comparer; //4 bytes for x86, 8 for x64
private Dictionary<TKey, TValue>.KeyCollection keys; //4 bytes for x86, 8 for x64
private Dictionary<TKey, TValue>.ValueCollection values; //4 bytes for x86, 8 for x64
private object _syncRoot; //4 bytes for x86, 8 for x64
private const string VersionName = "Version"; //one per type
private const string HashSizeName = "HashSize"; //one per type
private const string KeyValuePairsName = "KeyValuePairs"; //one per type
private const string ComparerName = "Comparer"; //one per type
}
44 for x86 and 72 for x64. Again rough calculation, since instances of different objects are required.
Decompiled by dotPeek :
public class List<T> : IList<T>, ICollection<T>, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable
{
private T _items; //4 bytes for x86, 8 for x64
private int _size; //4 bytes
private int _version; //4 bytes
[NonSerialized]
private object _syncRoot; //4 bytes for x86, 8 for x64
private static readonly T _emptyArray; //one per type
private const int _defaultCapacity = 4; //one per type
...
}
you got total of 20 bytes on x86 (16 for List<T>
members and 4 for metadata reference overhead) and 32 on x64, including reffernce to type of the object, which each object in .net have. This calculation is done roughly not counting alligment.
public class Dictionary<TKey, TValue> : ...
{
private int buckets; //4 bytes for x86, 8 for x64
private Dictionary<TKey, TValue>.Entry entries; //4 bytes for x86, 8 for x64
private int count; //4 bytes
private int version; //4 bytes
private int freeList; //4 bytes
private int freeCount; //4 bytes
private IEqualityComparer<TKey> comparer; //4 bytes for x86, 8 for x64
private Dictionary<TKey, TValue>.KeyCollection keys; //4 bytes for x86, 8 for x64
private Dictionary<TKey, TValue>.ValueCollection values; //4 bytes for x86, 8 for x64
private object _syncRoot; //4 bytes for x86, 8 for x64
private const string VersionName = "Version"; //one per type
private const string HashSizeName = "HashSize"; //one per type
private const string KeyValuePairsName = "KeyValuePairs"; //one per type
private const string ComparerName = "Comparer"; //one per type
}
44 for x86 and 72 for x64. Again rough calculation, since instances of different objects are required.
edited Apr 21 '13 at 14:01
answered Apr 21 '13 at 13:38
Ilya IvanovIlya Ivanov
19.6k44975
19.6k44975
What about the virtual method table?
– Pieter Geerkens
Apr 21 '13 at 13:41
@PieterGeerkens isn't it created one per type?List<T>
has also two static variables, but I didn't count them, because they don't affect the size of the object itself. As far as I remember, each object in .net has only one reference overhead.
– Ilya Ivanov
Apr 21 '13 at 13:42
1
@svick as OP stated in question: "How much memory eats empty List and Dictionary?".
– Ilya Ivanov
Apr 21 '13 at 14:04
1
@IlyaIvanov Yes, but are you sure emptyList
means the array is not actually allocated?
– svick
Apr 21 '13 at 14:30
1
@svick there is a static empty arrayT[0]
that is shared by all empty lists of typeT
. In the default constructor:_items = _emptyArray;
.
– Eren Ersönmez
Apr 22 '13 at 6:00
|
show 3 more comments
What about the virtual method table?
– Pieter Geerkens
Apr 21 '13 at 13:41
@PieterGeerkens isn't it created one per type?List<T>
has also two static variables, but I didn't count them, because they don't affect the size of the object itself. As far as I remember, each object in .net has only one reference overhead.
– Ilya Ivanov
Apr 21 '13 at 13:42
1
@svick as OP stated in question: "How much memory eats empty List and Dictionary?".
– Ilya Ivanov
Apr 21 '13 at 14:04
1
@IlyaIvanov Yes, but are you sure emptyList
means the array is not actually allocated?
– svick
Apr 21 '13 at 14:30
1
@svick there is a static empty arrayT[0]
that is shared by all empty lists of typeT
. In the default constructor:_items = _emptyArray;
.
– Eren Ersönmez
Apr 22 '13 at 6:00
What about the virtual method table?
– Pieter Geerkens
Apr 21 '13 at 13:41
What about the virtual method table?
– Pieter Geerkens
Apr 21 '13 at 13:41
@PieterGeerkens isn't it created one per type?
List<T>
has also two static variables, but I didn't count them, because they don't affect the size of the object itself. As far as I remember, each object in .net has only one reference overhead.– Ilya Ivanov
Apr 21 '13 at 13:42
@PieterGeerkens isn't it created one per type?
List<T>
has also two static variables, but I didn't count them, because they don't affect the size of the object itself. As far as I remember, each object in .net has only one reference overhead.– Ilya Ivanov
Apr 21 '13 at 13:42
1
1
@svick as OP stated in question: "How much memory eats empty List and Dictionary?".
– Ilya Ivanov
Apr 21 '13 at 14:04
@svick as OP stated in question: "How much memory eats empty List and Dictionary?".
– Ilya Ivanov
Apr 21 '13 at 14:04
1
1
@IlyaIvanov Yes, but are you sure empty
List
means the array is not actually allocated?– svick
Apr 21 '13 at 14:30
@IlyaIvanov Yes, but are you sure empty
List
means the array is not actually allocated?– svick
Apr 21 '13 at 14:30
1
1
@svick there is a static empty array
T[0]
that is shared by all empty lists of type T
. In the default constructor: _items = _emptyArray;
.– Eren Ersönmez
Apr 22 '13 at 6:00
@svick there is a static empty array
T[0]
that is shared by all empty lists of type T
. In the default constructor: _items = _emptyArray;
.– Eren Ersönmez
Apr 22 '13 at 6:00
|
show 3 more comments
The reason for asking is, can you save some bytes by setting lists to null?
As a general rule (to which, like any rule, there are exceptions), you shouldn't set managed objects to null
to free memory. Instead, leave it to the garbage collector to detect when an object is no longer reachable. In fact, setting a reference to null
may be counter-productive as it can actually slightly extend the lifetime of the object.
An exception might be an edge case where it's worth setting a large object to null
(e.g. a very large list that is a property of another object which for some reason needs to remain reachable), but such cases will be rare and may indicate a "code smell".
add a comment |
The reason for asking is, can you save some bytes by setting lists to null?
As a general rule (to which, like any rule, there are exceptions), you shouldn't set managed objects to null
to free memory. Instead, leave it to the garbage collector to detect when an object is no longer reachable. In fact, setting a reference to null
may be counter-productive as it can actually slightly extend the lifetime of the object.
An exception might be an edge case where it's worth setting a large object to null
(e.g. a very large list that is a property of another object which for some reason needs to remain reachable), but such cases will be rare and may indicate a "code smell".
add a comment |
The reason for asking is, can you save some bytes by setting lists to null?
As a general rule (to which, like any rule, there are exceptions), you shouldn't set managed objects to null
to free memory. Instead, leave it to the garbage collector to detect when an object is no longer reachable. In fact, setting a reference to null
may be counter-productive as it can actually slightly extend the lifetime of the object.
An exception might be an edge case where it's worth setting a large object to null
(e.g. a very large list that is a property of another object which for some reason needs to remain reachable), but such cases will be rare and may indicate a "code smell".
The reason for asking is, can you save some bytes by setting lists to null?
As a general rule (to which, like any rule, there are exceptions), you shouldn't set managed objects to null
to free memory. Instead, leave it to the garbage collector to detect when an object is no longer reachable. In fact, setting a reference to null
may be counter-productive as it can actually slightly extend the lifetime of the object.
An exception might be an edge case where it's worth setting a large object to null
(e.g. a very large list that is a property of another object which for some reason needs to remain reachable), but such cases will be rare and may indicate a "code smell".
answered Nov 24 '18 at 12:46
JoeJoe
101k24152283
101k24152283
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16131641%2fmemory-usage-of-an-empty-list-or-dictionary%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I think your best bet is to use a memory profiler like ANTS and look up these specific objects and their memory usage.
– Kippie
Apr 21 '13 at 13:34
I don't have money for ants and ATM I am on ubuntu
– Petr
Apr 21 '13 at 13:35
5
I think under almost all circumstances, the answer is “so little it doesn't actually matter”.
– svick
Apr 21 '13 at 13:55
it always matter to me when it comes to memory... If there were less programmers who don't care about memory usage, modern programs wouldn't be so resource expensive...
– Petr
Apr 21 '13 at 17:07
3
@Petr 1. How many empty lists do you have? Unless it's millions, this won't make a measurable difference. 2. If you want to decrease memory consumption of your application, you should use a memory profiler to find out what takes up most memory. Premature optimization is a waste of time (at best), and that applies to optimizing memory too.
– svick
Apr 22 '13 at 11:44