Discussion:
[dart-misc] Newbie question about memory management
Kovács Attila
2018-10-22 05:45:03 UTC
Permalink
Hi

Here is a linked list sample :

class p {
int i;
p prev;
p next;
}

main() {
p first;
p prev1;
p act;

for (int j = 1; j <= 10; j++) {
act = new p();
if (j != 1) {
act.prev = prev1;
prev1.next = act;
}
act.i = j;
if (j == 1) {
act.prev = null;
first = act;
}
prev1 = act;
}
act.next = null;


act = first;
for(;;) {
print(act.i);
act = act.next;
if (act.next == null) {
print(act.i);
break;
}
}

//manual mem free here

// I would like to free all memory after this manually, how can i do it ?
//Is it possible ?

}


Thank you in advance Attila
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/6a3003d0-2878-478b-a92f-ace8ee784ea7%40dartlang.org.
Jacob Bang
2018-10-22 06:20:16 UTC
Permalink
Dart use garbage collection to handle memory so you should just remove the
reference to you objects and Dart will handle it automatically.

There are no way to manually handle the memory (well, maybe there are some
obscure way to to it with native extensions?). Are there any reasons why
you want to handle the memory manually?
Post by Kovács Attila
Hi
class p {
int i;
p prev;
p next;
}
main() {
p first;
p prev1;
p act;
for (int j = 1; j <= 10; j++) {
act = new p();
if (j != 1) {
act.prev = prev1;
prev1.next = act;
}
act.i = j;
if (j == 1) {
act.prev = null;
first = act;
}
prev1 = act;
}
act.next = null;
act = first;
for(;;) {
print(act.i);
act = act.next;
if (act.next == null) {
print(act.i);
break;
}
}
//manual mem free here
// I would like to free all memory after this manually, how can i do it ?
//Is it possible ?
}
Thank you in advance Attila
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/dartlang.org/d/msgid/misc/6a3003d0-2878-478b-a92f-ace8ee784ea7%40dartlang.org
<https://groups.google.com/a/dartlang.org/d/msgid/misc/6a3003d0-2878-478b-a92f-ace8ee784ea7%40dartlang.org?utm_medium=email&utm_source=footer>
.
--
Jacob Bang / julemand101
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CADK_2AUNiE1xNmmE2x7VR%3D4jz%3D0q_%2BSDC-0BhxVMzHxGVgeNmw%40mail.gmail.com.
Kovács Attila
2018-10-22 06:37:53 UTC
Permalink
Thank you your answer.

But in this sample, after print, in every object has a reference to prev
and next object.
So Dart can't free all object.

So, Do i have to walk through all object to set prev and next pointer to
null ?
Or i don't have to do anything.
Post by Kovács Attila
Hi
class p {
int i;
p prev;
p next;
}
main() {
p first;
p prev1;
p act;
for (int j = 1; j <= 10; j++) {
act = new p();
if (j != 1) {
act.prev = prev1;
prev1.next = act;
}
act.i = j;
if (j == 1) {
act.prev = null;
first = act;
}
prev1 = act;
}
act.next = null;
act = first;
for(;;) {
print(act.i);
act = act.next;
if (act.next == null) {
print(act.i);
break;
}
}
//manual mem free here
// I would like to free all memory after this manually, how can i do it ?
//Is it possible ?
}
Thank you in advance Attila
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/47c2e55b-8e6c-4969-a008-bb1ddf273e09%40dartlang.org.
Kovács Attila
2018-10-22 07:08:00 UTC
Permalink
ok thank you i understand
it is easier and better

//print
act = first;
first = null;
prev1 = null;
while(act!=null) {
print(act.i);
act=act.next;
}
act=null;
Post by Kovács Attila
Hi
class p {
int i;
p prev;
p next;
}
main() {
p first;
p prev1;
p act;
for (int j = 1; j <= 10; j++) {
act = new p();
if (j != 1) {
act.prev = prev1;
prev1.next = act;
}
act.i = j;
if (j == 1) {
act.prev = null;
first = act;
}
prev1 = act;
}
act.next = null;
act = first;
for(;;) {
print(act.i);
act = act.next;
if (act.next == null) {
print(act.i);
break;
}
}
//manual mem free here
// I would like to free all memory after this manually, how can i do it ?
//Is it possible ?
}
Thank you in advance Attila
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/bd808c20-3fad-487b-81df-57edbb88f493%40dartlang.org.
Jacob Bang
2018-10-22 07:15:07 UTC
Permalink
You only need to do the following (or just leave the main method without
returning any reference to you linked list):

first = null;
prev1 = null;
act = null;

Most garbage collected languages (including Dart) is smart enough to
identify chain of objects even if this objects are linking to each other.
So you only need to remove the link to the object structure and the garbage
collector will do the rest of the work.
Post by Kovács Attila
ok thank you i understand
it is easier and better
//print
act = first;
first = null;
prev1 = null;
while(act!=null) {
print(act.i);
act=act.next;
}
act=null;
Post by Kovács Attila
Hi
class p {
int i;
p prev;
p next;
}
main() {
p first;
p prev1;
p act;
for (int j = 1; j <= 10; j++) {
act = new p();
if (j != 1) {
act.prev = prev1;
prev1.next = act;
}
act.i = j;
if (j == 1) {
act.prev = null;
first = act;
}
prev1 = act;
}
act.next = null;
act = first;
for(;;) {
print(act.i);
act = act.next;
if (act.next == null) {
print(act.i);
break;
}
}
//manual mem free here
// I would like to free all memory after this manually, how can i do it ?
//Is it possible ?
}
Thank you in advance Attila
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/dartlang.org/d/msgid/misc/bd808c20-3fad-487b-81df-57edbb88f493%40dartlang.org
<https://groups.google.com/a/dartlang.org/d/msgid/misc/bd808c20-3fad-487b-81df-57edbb88f493%40dartlang.org?utm_medium=email&utm_source=footer>
.
--
Jacob Bang / julemand101
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CADK_2AUTx3QDOo651pYGu95Xz1-aOKfO07DxGQR4MaPuFP-TpA%40mail.gmail.com.
Kovács Attila
2018-10-22 08:35:10 UTC
Permalink
Thank you jacob.
Post by Kovács Attila
Hi
class p {
int i;
p prev;
p next;
}
main() {
p first;
p prev1;
p act;
for (int j = 1; j <= 10; j++) {
act = new p();
if (j != 1) {
act.prev = prev1;
prev1.next = act;
}
act.i = j;
if (j == 1) {
act.prev = null;
first = act;
}
prev1 = act;
}
act.next = null;
act = first;
for(;;) {
print(act.i);
act = act.next;
if (act.next == null) {
print(act.i);
break;
}
}
//manual mem free here
// I would like to free all memory after this manually, how can i do it ?
//Is it possible ?
}
Thank you in advance Attila
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/5c6ccbec-07d6-4a3c-87bc-6c3447ca093b%40dartlang.org.
Continue reading on narkive:
Loading...