Python Forum
Getting rid of old string values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting rid of old string values
#1
Recently I posted something here, can't remember exactly which thread, in which I started with an empty string and kept adding bits to it.

deanhystad pointed out, because strings are immutable, I would end up with a lot of strings hanging around in memory. Python doesn't overwrite them. I don't know what happens to them or how Python deals with them.

I was thinking of that today. I get a date from Excel, like 2022-05-21 04:33:27 and put it in this function:

def string2Seconds(date):
    st = str(date)
    # need to specify the format of the string
    dt = datetime.strptime(st, "%Y-%m-%d %H:%M:%S")
    # convert dt to epoch seconds
    seconds = dt.timestamp()
    return seconds
Maybe I am looking for all rows in the Excel after 2022-05-21 04:33:27 or before 2022-05-21 04:33:27

That would leave a lot of old st variables hanging around in memory.

Is there anything I can do about that??
Reply
#2
I don't understand the problem. The st variables are local variables. They are returned to the garbage collector when the function exits. They wont "hang in memory".
Pedroski55 likes this post
Reply
#3
Carbage collector works based on reference count. So non-referenced objects (of string) will be destroyed.

The real problem with concatenating strings is it's quadratic runtime cost. From Python documentation:

Quote:Concatenating immutable sequences always results in a new object. This means that building up a sequence by repeated concatenation will have a quadratic runtime cost in the total sequence length. To get a linear runtime cost, you must switch to one of the alternatives below:

  • if concatenating str objects, you can build a list and use str.join() at the end or else write to an io.StringIO instance and retrieve its value when complete
  • if concatenating bytes objects, you can similarly use bytes.join() or io.BytesIO, or you can do in-place concatenation with a bytearray object. bytearray objects are mutable and have an efficient overallocation mechanism
  • if concatenating tuple objects, extend a list instead
  • for other types, investigate the relevant class documentation
Pedroski55 likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Thanks for your replies.

OK, I obviously misunderstood deanhystad.

Unreferenced variables will be trashed when a function exits. Use lists if you can.

Got it!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to compare string values in an if statement israelsattleen 1 568 Jul-08-2023, 03:49 PM
Last Post: deanhystad
  mutable values to string items? fozz 15 2,889 Aug-30-2022, 07:20 PM
Last Post: deanhystad
  Function parameters and values as string infobound 1 1,771 Jul-24-2020, 04:28 AM
Last Post: scidam
  xml.etree.ElementTree extract string values matthias100 2 5,002 Jul-12-2020, 06:02 PM
Last Post: snippsat
  How to get values from part of a string? mahi926 1 1,870 May-16-2019, 07:27 AM
Last Post: buran
  Change values in string multiline DavidFernandez 4 3,103 Aug-26-2018, 08:09 PM
Last Post: buran
  How to call the values at the end of a text string? Dieselkaine 2 2,985 Jul-02-2018, 08:47 PM
Last Post: Dieselkaine
  Search for string values in begood321 7 4,276 Mar-21-2018, 02:36 AM
Last Post: tannishpage

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020